使用自定义样式渲染超链接
Contents
[
Hide
]
有时您需要根据应用程序的需求使用特定样式输出超链接。为此,Aspose.Email 提供了 HyperlinkRenderingCallback。您可以将 HyperlinkRenderingCallback 作为参数传递给 MailMessage.GetHtmlBodyText.
以下代码片段展示了如何使用 HyperlinkRenderingCallback 以自定义样式输出超链接。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public static void main(String[] args) {
String dataDir = "data/";
MailMessage msg = MailMessage.load(dataDir + "LinksSample.eml");
String htmlTextHref = msg.getHtmlBodyText(new HyperlinkRenderingCallback() {
@Override
public String invoke(String source) {
return renderHyperlinkWithHref(source);
}
});
System.out.println(htmlTextHref);
String htmlTextHrefLess = msg.getHtmlBodyText(new HyperlinkRenderingCallback() {
@Override
public String invoke(String source) {
return renderHyperlinkWithoutHref(source);
}
});
System.out.println(htmlTextHrefLess);
}
// Renders the link as "text <href>"
private static String renderHyperlinkWithHref(String source) {
int start = source.indexOf("href=\"") + "href=\"".length();
int end = source.indexOf("\"", start);
String href = source.substring(start, end);
start = source.indexOf(">") + 1;
end = source.indexOf("<", start);
String text = source.substring(start, end);
return text + " <" + href + ">";
}
// Renders the link text only, dropping the target
private static String renderHyperlinkWithoutHref(String source) {
int start = source.indexOf(">") + 1;
int end = source.indexOf("<", start);
return source.substring(start, end);
}
RenderHyperlinkWithHref 和 RenderHyperlinkWithoutHref 方法旨在演示超链接渲染,并不适用于生产环境。