For some reason I'm getting a javax.mail.internet.ParseException when I call Transport.send() on a MimeMessage. This worked before when it was only a plain text email, but when I changed it to have both text and html it started blowing up. Any ideas what I'm doing wrong?
@Resource(mappedName = "java:/Mail")
private Session mailer;
public void sendMessage(String toEmailAddress, String subject, String content, String text) throws Exception {
try {
Message message = new MimeMessage(mailer);
MimeMultipart rootMixedMultipart = new MimeMultipart("mixed");
message.setContent(rootMixedMultipart);
MimeMultipart nestedRelatedMultipart = new MimeMultipart("related");
MimeBodyPart relatedBodyPart = new MimeBodyPart();
relatedBodyPart.setContent(nestedRelatedMultipart);
rootMixedMultipart.addBodyPart(relatedBodyPart);
MimeMultipart messageBody = new MimeMultipart("alternative");
MimeBodyPart bodyPart = null;
for (int i = 0; i < nestedRelatedMultipart.getCount(); i++) {
BodyPart bp = nestedRelatedMultipart.getBodyPart(i);
if (bp.getFileName() == null) {
bodyPart = (MimeBodyPart) bp;
}
}
if (bodyPart == null) {
MimeBodyPart mimeBodyPart = new MimeBodyPart();
nestedRelatedMultipart.addBodyPart(mimeBodyPart);
bodyPart = mimeBodyPart;
}
bodyPart.setContent(messageBody, "text/alternative");
// Create the plain text part of the message.
MimeBodyPart plainTextPart = new MimeBodyPart();
plainTextPart.setText(text, "UTF-8");
messageBody.addBodyPart(plainTextPart);
// Create the HTML text part of the message.
MimeBodyPart htmlText开发者_高级运维Part = new MimeBodyPart();
htmlTextPart.setContent(content, "CONTENT_TYPE_HTML;charset=UTF-8");
messageBody.addBodyPart(htmlTextPart);
message.setFrom(new InternetAddress(NO_REPLY_EMAIL_ADDRESS, PERSONAL));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmailAddress));
message.setSubject(subject);
message.setHeader("Precedence", "bulk");
Transport.send(message);
}
catch (Exception e) {
}
}
Here's the stack trace.
2011-03-03 00:20:05,896 ERROR [STDERR] javax.mail.internet.ParseException
2011-03-03 00:20:05,896 ERROR [STDERR] at javax.mail.internet.ContentType.<init>(ContentType.java:89)
2011-03-03 00:20:05,896 ERROR [STDERR] at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1249)
2011-03-03 00:20:05,896 ERROR [STDERR] at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1001)
2011-03-03 00:20:05,896 ERROR [STDERR] at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:333)
2011-03-03 00:20:05,896 ERROR [STDERR] at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1255)
2011-03-03 00:20:05,896 ERROR [STDERR] at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1001)
2011-03-03 00:20:05,896 ERROR [STDERR] at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:333)
2011-03-03 00:20:05,896 ERROR [STDERR] at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1255)
2011-03-03 00:20:05,896 ERROR [STDERR] at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1001)
2011-03-03 00:20:05,896 ERROR [STDERR] at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:333)
2011-03-03 00:20:05,897 ERROR [STDERR] at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1255)
2011-03-03 00:20:05,897 ERROR [STDERR] at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2012)
2011-03-03 00:20:05,897 ERROR [STDERR] at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:1980)
2011-03-03 00:20:05,897 ERROR [STDERR] at javax.mail.Transport.send(Transport.java:97)
2011-03-03 00:20:05,897 ERROR [STDERR] at com.lawless.manager.NotificationManagerBean.sendMessage(NotificationManagerBean.java:69)
2011-03-03 00:20:05,897 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2011-03-03 00:20:05,897 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
2011-03-03 00:20:05,897 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
2011-03-03 00:20:05,897 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:597)
2011-03-03 00:20:05,897 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
2011-03-03 00:20:05,897 ERROR [STDERR] at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
2011-03-03 00:20:05,897 ERROR [STDERR] at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
2011-03-03 00:20:05,897 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,897 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
2011-03-03 00:20:05,897 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,897 ERROR [STDERR] at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
2011-03-03 00:20:05,897 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,897 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
2011-03-03 00:20:05,897 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:240)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:210)
2011-03-03 00:20:05,898 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:84)
2011-03-03 00:20:05,898 ERROR [STDERR] at $Proxy97.sendMessage(Unknown Source)
2011-03-03 00:20:05,898 ERROR [STDERR] at com.lawless.manager.CommentManagerBean.sendCommentNotification(CommentManagerBean.java:85)
2011-03-03 00:20:05,898 ERROR [STDERR] at com.lawless.manager.CommentManagerBean.addComment(CommentManagerBean.java:44)
2011-03-03 00:20:05,898 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2011-03-03 00:20:05,899 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
2011-03-03 00:20:05,899 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
2011-03-03 00:20:05,899 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:597)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,899 ERROR [STDERR] at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
2011-03-03 00:20:05,900 ERROR [STDERR] at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
2011-03-03 00:20:05,900 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,900 ERROR [STDERR] at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
2011-03-03 00:20:05,900 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,900 ERROR [STDERR] at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
2011-03-03 00:20:05,900 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
2011-03-03 00:20:05,900 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:240)
2011-03-03 00:20:05,900 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:210)
2011-03-03 00:20:05,900 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:84)
2011-03-03 00:20:05,900 ERROR [STDERR] at $Proxy100.addComment(Unknown Source)
2011-03-03 00:20:05,900 ERROR [STDERR] at com.lawless.web.ListingAction.addComment(ListingAction.java:97)
2011-03-03 00:20:05,900 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2011-03-03 00:20:05,900 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
2011-03-03 00:20:05,900 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
2011-03-03 00:20:05,900 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:597)
2011-03-03 00:20:05,900 ERROR [STDERR] at net.sourceforge.stripes.controller.DispatcherHelper$6.intercept(DispatcherHelper.java:442)
2011-03-03 00:20:05,900 ERROR [STDERR] at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:158)
2011-03-03 00:20:05,900 ERROR [STDERR] at org.stripesstuff.plugin.security.SecurityInterceptor.interceptEventHandling(SecurityInterceptor.java:188)
2011-03-03 00:20:05,900 ERROR [STDERR] at org.stripesstuff.plugin.security.SecurityInterceptor.intercept(SecurityInterceptor.java:120)
2011-03-03 00:20:05,901 ERROR [STDERR] at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155)
2011-03-03 00:20:05,901 ERROR [STDERR] at net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor.intercept(BeforeAfterMethodInterceptor.java:113)
2011-03-03 00:20:05,901 ERROR [STDERR] at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155)
2011-03-03 00:20:05,901 ERROR [STDERR] at net.sourceforge.stripes.controller.ExecutionContext.wrap(ExecutionContext.java:74)
2011-03-03 00:20:05,901 ERROR [STDERR] at net.sourceforge.stripes.controller.DispatcherHelper.invokeEventHandler(DispatcherHelper.java:440)
2011-03-03 00:20:05,901 ERROR [STDERR] at net.sourceforge.stripes.controller.DispatcherServlet.invokeEventHandler(DispatcherServlet.java:278)
2011-03-03 00:20:05,901 ERROR [STDERR] at net.sourceforge.stripes.controller.DispatcherServlet.service(DispatcherServlet.java:160)
2011-03-03 00:20:05,901 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
2011-03-03 00:20:05,901 ERROR [STDERR] at net.sourceforge.stripes.controller.DynamicMappingFilter$2.doFilter(DynamicMappingFilter.java:364)
2011-03-03 00:20:05,901 ERROR [STDERR] at net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:247)
2011-03-03 00:20:05,901 ERROR [STDERR] at net.sourceforge.stripes.controller.DynamicMappingFilter.doFilter(DynamicMappingFilter.java:351)
2011-03-03 00:20:05,901 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
2011-03-03 00:20:05,901 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
2011-03-03 00:20:05,901 ERROR [STDERR] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
2011-03-03 00:20:05,901 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
2011-03-03 00:20:05,901 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
2011-03-03 00:20:05,901 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
2011-03-03 00:20:05,901 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
2011-03-03 00:20:05,901 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
2011-03-03 00:20:05,902 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
2011-03-03 00:20:05,902 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
2011-03-03 00:20:05,902 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
2011-03-03 00:20:05,902 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
2011-03-03 00:20:05,902 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
2011-03-03 00:20:05,902 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
2011-03-03 00:20:05,902 ERROR [STDERR] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
2011-03-03 00:20:05,902 ERROR [STDERR] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
2011-03-03 00:20:05,902 ERROR [STDERR] at java.lang.Thread.run(Thread.java:619)
Look at
htmlTextPart.setContent(content, "CONTENT_TYPE_HTML;charset=UTF-8");
where you want to reference to a CONTENT_TYPE_HTML, but put is a String into the contenttype parameter. CONTENT_TYPE_HTML
is of course not a valid content-type declaration. CONTENT_TYPE_HTML
is probably a static final String member of "SomeClass", which equals "text/html", I guess.
Instead use
htmlTextPart.setContent(content, SomeClass.CONTENT_TYPE_HTML+";charset=UTF-8");
to set the content-type to text/html;charset=utf-8
. Of course replace SomeClass with the real class where the member CONTENT_TYPE_HTML
was defined.
try using the following, Email.setContent(Object o, String contentType)
精彩评论