Comment obtenir de texte HTML / texte brut à partir de java.mail
Quand je suis en train de lire corps du message électronique à partir de java.mail dans contentText je reçois d'abord le texte en clair et après ce texte HTML. I. e. si l'envoi du message est
<div><b>Mock</b><br />se Moquer de 2</div>
contentText contient:
Maquette Maquette
<div><b>Mock</b><br />se Moquer de 2</div>
Ci-dessous mon code pour charger contentText:
public void setContentText(Multipart multipart) throws MessagingException, IOException {
contentText ="";
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
getBodyToStringPart(bodyPart);
}
}
protected void getBodyToStringPart(BodyPart bodyPart) throws MessagingException, IOException {
String disposition = bodyPart.getDisposition();
if (!StringUtils.equalsIgnoreCase(disposition, "ATTACHMENT")) {
if (bodyPart.getContent() instanceof BASE64DecoderStream
&& bodyPart.getHeader("Content-ID") != null) {
BASE64DecoderStream base64DecoderStream = (BASE64DecoderStream) bodyPart
.getContent();
byte[] byteArray = IOUtils.toByteArray(base64DecoderStream);
byte[] encodeBase64 = Base64.encodeBase64(byteArray);
this.contentText = this.contentText.replaceAll(
"cid:"
+ bodyPart.getHeader("Content-ID")[0].replaceAll(">", "")
.replaceAll("<", ""), "data:" + bodyPart.getContentType()
+ ";base64," + new String(encodeBase64, "UTF-8"));
} else if (bodyPart.getContent() instanceof MimeMultipart) {
MimeMultipart mimeMultipart = (MimeMultipart) bodyPart.getContent();
for (int j = 0; j < mimeMultipart.getCount(); j++) {
getBodyToStringPart(mimeMultipart.getBodyPart(j));
}
} else {
this.contentText += bodyPart.getContent() + "";
}
} else {
//TODO: Do we need attachments ?
}
}
OriginalL'auteur user2365209 | 2013-09-24
Vous devez vous connecter pour publier un commentaire.
Cette JavaMail entrée de la FAQ pourrait aider.
OriginalL'auteur Bill Shannon