javamail - How to get HTML text / plain text from java.mail -
when i'm reading email body java.mail in contenttext first plain text , after html text. i.e. if send message
<div><b>mock</b><br />mock 2</div>
contenttext contains:
mock mock <div><b>mock</b><br />mock 2</div>
below code load contenttext:
public void setcontenttext(multipart multipart) throws messagingexception, ioexception { contenttext =""; (int = 0; < 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(); (int j = 0; j < mimemultipart.getcount(); j++) { getbodytostringpart(mimemultipart.getbodypart(j)); } } else { this.contenttext += bodypart.getcontent() + ""; } } else { // todo: need attachments ? } }
this javamail faq entry might help.
Comments
Post a Comment