java - Getting email text from ImageHtmlEmail -


we using apache commons mail, imagehtmlemail. log every email sent - sent - in perfect world paste sendmail - headers , other information included.

this troubleshoot problems we've been having turning text/plain rather text/html - because nice have record of system sent out stored in our logs.

so - dream function take imagehtmlemail , return string - as sent. know render string myself, i'm bypassing whatever being done in library function, want capture. tried buildmimemessage , getmimemessage, think correct first step - leaves me question of how turn mimemessage string.

i have sort of solution - love better one:

/**  * add content of type  *  * @param builder  * @param content  */ private static void addcontent(final stringbuilder builder, final object content) {     try     {         if (content instanceof mimemultipart)         {             final mimemultipart multi = (mimemultipart) content;             (int = 0; < multi.getcount(); i++)             {                 addcontent(builder, ((mimemultipart) content).getbodypart(i));             }         }         else if (content instanceof mimebodypart)         {              final mimebodypart message = (mimebodypart) content;             final enumeration<?> headers = message.getallheaderlines();             while (headers.hasmoreelements())             {                 final string line = (string) headers.nextelement();                 builder.append(line).append("\n");             }             addcontent(builder, message.getcontent());         }         else if (content instanceof string)         {             builder.append((string) content).append("\n");         }         else         {             system.out.println(content.getclass().getname());             throw commonexception.notimplementedyet();         }     }     catch (final exception theexception)     {         throw commonexception.insteadof(theexception);     }  }  /**  * string email  *  * @param email  * @return  */ public static string fromhtmlemail(final imagehtmlemail email) {     return frommimemessage(email.getmimemessage()); }  /**  * @param message  * @return string mime message  */ private static string frommimemessage(final mimemessage message) {     try     {         message.savechanges();         final stringbuilder output = new stringbuilder();         final enumeration<?> headers = message.getallheaderlines();         while (headers.hasmoreelements())         {             final string line = (string) headers.nextelement();             output.append(line).append("\n");         }         addcontent(output, message.getcontent());         return output.tostring();     }     catch (final exception theexception)     {         throw commonexception.insteadof(theexception);     } } 

}


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -