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 = "[email protected]";
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 (i.e. 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).