简体   繁体   中英

How to format bodyPart of javax.mail message

I am using the javax.mail API to send error messages from a Java cron job to my private inbox. Upon error, I pass the stacktrace to my email body. The problem is that the Java code structure is not preserved by this process. The line breaks are apparently ignored. When the emails arrive, the body looks like this:

java.lang.Exception: BackUp folder for BatchClass D_DE_KABI_DELIVERY_NOTES_V03 not found. at FncApplicationLevelScript.importFromMultiFolders(FncApplicationLevelScript.java:378) at FncApplicationLevelScript.execute(FncApplicationLevelScript.java:151) at sun.reflect.GeneratedMethodAccessor431.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.ephesoft.dcma.script.compiler.DynamicCodeCompiler$MyInvocationHandler.invoke(DynamicCodeCompiler.java:364) at com.sun.proxy.$Proxy256.execute(Unknown Source) at com.ephesoft.dcma.script.ScriptExecutor.executeApplicationScript(ScriptExecutor.java:305) at com.ephesoft.dcma.script.service.ScriptServiceImpl.executeApplicationScript(ScriptServiceImpl.java:115) at sun.reflect.GeneratedMethodAccessor427.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy175.executeApplicationScript(Unknown Source) at sun.reflect.GeneratedMethodAccessor424.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:269) at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:321) at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:111) at org.quartz.core.JobRunShell.run(JobRunShell.java:216) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549) 

whereas I would like it to look like this:

...
Caused by: java.io.FileNotFoundException: 
C:\Ephesoft\SharedFolders\BC4E47\script- 
config\BC4E47_batchClassProperties.properties (The system cannot find the 
file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at fncBatchUtils.FncBatchUtils.loadPropertyFile(FncBatchUtils.java:140)
at fncBatchUtils.FncBatchUtils.<init>(FncBatchUtils.java:94)
at fncBatchUtils.FncMail.<init>(FncMail.java:247)
at ScriptAutomaticValidation.execute(ScriptAutomaticValidation.java:79)
at sun.reflect.GeneratedMethodAccessor910.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.ephesoft.dcma.script.compiler.DynamicCodeCompiler$MyInvocationHandler.invoke(DynamicCodeCompiler.java:364)
at com.sun.proxy.$Proxy258.execute(Unknown Source)
at com.ephesoft.dcma.script.ScriptExecutor.extractBatch(ScriptExecutor.java:455)
... 122 more

For better readability, I would like the Java code structure and especially the line breaks to be preserved.

I currently extract the error message like this:

try { ...
} catch (Exception e) {
    StringWriter sw = new StringWriter();
    e.printStackTrace(new PrintWriter(sw));
    String errorMessage = "Error occured...";
    String bodyText = sw.toString();
    String from = "capturing-development@xxx.com";
    sendMail(errorMessage, bodyText , from);
}

Inside the sendMail method, the bodyPart is set to contain the error stacktrace (contained in bodyText ) like this:

Multipart multipart = new MimeMultipart();
MimeBodyPart messagePart = new MimeBodyPart();
String htmlMessage = "<p> " + bodyText + " </p><br>";
messagePart.setContent(htmlMessage, "text/html; charset=utf-8");
multipart.addBodyPart(messagePart);
message.setContent(multipart);

Is there any way I can tell my message object to format the bodyPart in a readable way (ie Java code format)?

Your problem is that in HTML whitespace and linebreaks are ignored. You should try replacing newline characters with <br/> tags before adding to the message body, or insert <br/> after each closing parenthesis ) .

尝试从Apache Commons lang使用StringEscapeUtils.escapeHtml() ,它可以处理相邻的空格并避免其他特殊字符

Why not use HtmlEmail email api - use StringBuffer for HTML sample from here

HtmlEmail he = new HtmlEmail();
 File img = new File("my/image.gif");
 PNGDataSource png = new PNGDataSource(decodedPNGOutputStream); // a custom class
 StringBuffer msg = new StringBuffer();
 msg.append("<html><body>");
 msg.append("<img src=cid:").append(he.embed(img)).append(">");
 msg.append("<img src=cid:").append(he.embed(png)).append(">");
 msg.append("</body></html>");
 he.setHtmlMsg(msg.toString());

Here is an example you can add html as Body Content, attach files also.

        String text = "html tags";

        Multipart mainMultiPart = new MimeMultipart();  
        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeBodyPart.setText(text);
        mimeBodyPart.setContent(text, "text/html; charset=iso-8859-1""text/html; charset=iso-8859-1");
        mainMultiPart.addBodyPart(mimeBodyPart);

        java.io.File file = null;

        MimeBodyPart messageBodyPartFile = new MimeBodyPart();
        FileDataSource fds = new FileDataSource(file);      
        messageBodyPartFile.setDataHandler(new DataHandler(fds));       
        String fileName = fds.getName();        
        messageBodyPartFile.setFileName(fileName);      
        mainMultiPart.addBodyPart(messageBodyPartFile);     

        javax.mail.Message message = null;      
        message.setContent(mainMultiPart);      

我只是通过使用以下标签解决了这个问题:

String htmlMessage = "<p><pre><code> " + bodyText + " </pre></code></p><br>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM