java - Convert content-type from text/plain to text/html in an email with attachment -
i'm trying display email messages gotten server. prior letting know issue, take @ code:
public filemailextended getsinglemessage(integer filemailid) throws mailsystemexception, ioexception, messagingexception, boserviceexception { filemail email = retrieve(filemailid.intvalue()); mailerscopedef scope = email.gettype(); boolean outbound = email.getdirection() == emailconstants.direction_sent; long uid = long.valueof(email.getuid()); string folder = null; if (outbound) { folder = configurationservice.getsentfolder(scope); } else { folder = configurationservice.getreadfolder(scope); } filemailconfig filemailconfig = getfilemailconfig(folder); long uidvalidity = 0; if (filemailconfig != null) { uidvalidity = filemailconfig.getversionid(); } imapfoldermanager ifm; if (outbound) { ifm = ((imapsender) configurationservice.getmailsender(scope)).getimapfoldermger(); } else { ifm = configurationservice.getimapfoldermanagerreader(scope); } filemailextended emailextended = new filemailextended(); mapfilemailtofilemailextended(email, emailextended); try { imapmessage message = (imapmessage) ifm.getmessage(uid, uidvalidity); if (message == null) { logger.error("could not read message uid:" + uid + " server."); return null; } internetaddress intadress = (internetaddress) message.getsender(); string unicodestring = intadress.tounicodestring().replace("\"", ""); emailextended.setfrom(unicodestring); emailextended.setto(appendaddresses(message.getrecipients(recipienttype.to))); emailextended.setcc(appendaddresses(message.getrecipients(recipienttype.cc))); emailextended.setcci(appendaddresses(message.getrecipients(recipienttype.bcc))); emailextended.setsubject(message.getsubject()); string content = ""; if (message.getcontent() instanceof string) { content += (string) message.getcontent(); } else if (message.getcontent() instanceof multipart) { multipart multipart = (multipart) message.getcontent(); stringbuffer buf = new stringbuffer(); (int x = 0; x < multipart.getcount(); x++) { bodypart bodypart = multipart.getbodypart(x); if (bodypart == null || bodypart.getcontenttype() == null) { continue; } if (bodypart.ismimetype(text_html)) { buf.append((string) bodypart.getcontent()); } else if (bodypart.getcontent() instanceof multipart) { multipart multipart2 = (multipart) bodypart.getcontent(); (int xx = 0; xx < multipart2.getcount(); xx++) { bodypart bodypart2 = multipart2.getbodypart(xx); if (bodypart2 != null && bodypart2.ismimetype("text/html") { buf.append((string) bodypart2.getcontent()); } } } } content = buf.tostring(); } emailextended.setcontent(content); if (message.getcontent() instanceof multipart) { multipart multipart = (multipart) message.getcontent(); (int = 0; < multipart.getcount(); i++) { bodypart bodypart = multipart.getbodypart(i); if (!part.attachment.equalsignorecase(bodypart.getdisposition())) { // dealing attachments continue; } emailextended.addattachmentnames(bodypart.getfilename()); } } } { closefoldermanager(ifm); } return emailextended; }
this code works fine when email doesn't have attachments. emailcontent, formatted in html, readable without efforts.
problem is, once email contains attachment file, nothing displayed.
discovered because of content-type, "text/plain" when sending message attachments when "text/html" simple mail. changed condition take in account content-type, result displayed is..uh..just raw text.
planned force content-type "text/html" code bodypart2.setheader("content-type", "text/html");
, ended exception. clue why content-type of email attachments has "text/plain"? googled it, no significant answers.
how can change it?
many thanks.
whether content text/plain or text/html has nothing whether or not message has attachments.
in cases, messages have multipart/alternative content contains both text/plain , text/html parts. in case can choose text/html part. this example code in javamail faq might help.
if message has text/plain part, it's format , display think best.
Comments
Post a Comment