Render Hyperlinks with custom style

Contents
[ ]

There may be times when you need to output hyperlinks with some specific style based on the requirements of your application. For that, Aspose.Email provides HyperlinkRenderingCallback. You can pass the HyperlinkRenderingCallback as a parameter of MailMessage.GetHtmlBodyText.

The following code snippet shows you how to use HyperlinkRenderingCallback to output hyperlinks using your own custom style.

// 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 = Utils.getSharedDataDir(CustomHyperlinkRendering.class) + "email/";
String fileName = dataDir + "LinksSample.eml";
MailMessage msg = MailMessage.load(fileName);
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);
}
private static String RenderHyperlinkWithHref(String source){
int start = source.indexOf("href=\"") + "href=\"".length();
int end = source.indexOf("\"", start + "href=\"".length());
String href = source.substring(start, end);
start = source.indexOf(">") + 1;
end = source.indexOf("<", start);
String text = source.substring(start, end);
String link = text + "<" + href + ">";
return link;
}
private static String RenderHyperlinkWithoutHref(String source){
int start = source.indexOf(">") + 1;
int end = source.indexOf("<", start);
String text = source.substring(start, end);
return text;
}