2

出于某种原因,当我在 MimeMessage 上调用 Transport.send() 时,我得到了 javax.mail.internet.ParseException。这在它只是纯文本电子邮件之前有效,但是当我将它更改为同时具有文本和 html 时,它开始爆炸。任何想法我做错了什么?

@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 htmlTextPart = 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) {
        }
    }

这是堆栈跟踪。

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)
4

2 回答 2

5

看着

htmlTextPart.setContent(content, "CONTENT_TYPE_HTML;charset=UTF-8");

在其中引用 CONTENT_TYPE_HTML,但将字符串放入 contenttype 参数中。CONTENT_TYPE_HTML当然不是有效的内容类型声明。CONTENT_TYPE_HTML 我猜可能是“SomeClass”的静态最终字符串成员,它等于“text/html”。

而是使用

htmlTextPart.setContent(content, SomeClass.CONTENT_TYPE_HTML+";charset=UTF-8");

将内容类型设置为text/html;charset=utf-8。当然,将 SomeClass 替换CONTENT_TYPE_HTML为定义该成员的真实类。

于 2011-03-03T09:24:30.577 回答
0

尝试使用以下内容,Email.setContent(Object o, String contentType)

于 2011-03-03T06:04:17.280 回答