Renderizar hiperlinks com estilo personalizado
Contents
[
Hide
]
Pode haver momentos em que você precise gerar hiperlinks com um estilo específico com base nos requisitos de sua aplicação. Para isso, o Aspose.Email fornece o HyperlinkRenderingCallback. Você pode passar o HyperlinkRenderingCallback como um parâmetro de MailMessage.GetHtmlBodyText.
O seguinte trecho de código mostra como usar o HyperlinkRenderingCallback para gerar hiperlinks usando seu próprio estilo personalizado.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Os métodos RenderHyperlinkWithHref e RenderHyperlinkWithoutHref têm o propósito de demonstrar a renderização de hiperlinks e não são destinados para uso em produção.