我正在使用javax.mail API将错误消息从Java cron作业发送到我的私人收件箱。出错时,我将堆栈跟踪传递给我的电子邮件正文。问题是此过程不保留Java代码结构。换行显然被忽略了。当电子邮件到达时,正文如下:
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)
虽然我希望它看起来像这样:
...
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
为了更好的可读性,我希望保留Java代码结构,特别是换行符。
我目前提取的错误信息如下:
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);
}
在sendMail
方法中,bodyPart
设置为包含错误堆栈跟踪(包含在bodyText
中),如下所示:
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);
有什么方法可以告诉我的消息对象以可读的方式格式化bodyPart(即Java代码格式)?
答案 0 :(得分:0)
您的问题是在HTML空格和换行符被忽略。在添加到邮件正文之前,您应尝试使用<br/>
标记替换换行符,或在每个右括号<br/>
后插入)
。
答案 1 :(得分:0)
尝试使用apache commons lang中的StringEscapeUtils.escapeHtml()
,它可以处理相邻的空格和其他特殊字符
答案 2 :(得分:0)
为什么不使用HtmlEmail电子邮件api - 将StringBuffer用于来自here的HTML示例
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());
答案 3 :(得分:0)
以下是一个示例,您可以将html添加为正文内容,也可以附加文件。
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);
答案 4 :(得分:0)
我刚刚使用这些标签解决了这个问题:
String htmlMessage = "<p><pre><code> " + bodyText + " </pre></code></p><br>";