JavaMail未在Jetty:run-war下发送主题或发件人 [英] JavaMail not sending Subject or From under jetty:run-war

查看:79
本文介绍了JavaMail未在Jetty:run-war下发送主题或发件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人看到JavaMail不能根据JVM的启动方式将适当的MimeMessages发送到SMTP服务器?最终,我无法使用Subject:或From:字段发送JavaMail SMTP消息,并且只有在以战争方式运行该应用程序时,其他标头似乎丢失了.

Has anyone seen JavaMail not sending proper MimeMessages to an SMTP server, depending on how the JVM in started? At the end of the day, I can't send JavaMail SMTP messages with Subject: or From: fields, and it appears other headers are missing, only when running the app as a war.

该Web项目是使用Maven构建的,我正在测试使用浏览器和简单的mail.jsp发送JavaMail进行调试,并通过以下方式启动应用程序时看到不同的行为:

The web project is built with Maven and I'm testing sending JavaMail using a browser and a simple mail.jsp to debug and see different behavior when launching the app with:

1)mvn jetty:run(邮件发送正常,带有正确的主题"和发件人"字段)

1) mvn jetty:run (mail sends fine, with proper Subject and From fields)

2)mvn jetty:run-war(邮件发送正常,但缺少主题,发件人和其他字段)

2) mvn jetty:run-war (mail sends fine, but missing Subject, From, and other fields)

我已经在(详细的)Maven调试输出(-X)上精心运行了diff,并且两者之间的运行时依赖项之间的差异为零.我还比较了系统属性,它们是相同的.发生了另外一些事情,改变了JavaMail的行为方式的jetty:run-war案例.还有哪些石头需要转弯?

I've meticulously run diff on the (verbose) Maven debug output (-X), and there are zero differences in the runtime dependencies between the two. I've also compared System properties, and they are identical. Something else is happening the jetty:run-war case that changes the way JavaMail behaves. What other stones need turning?

奇怪的是,我在两种情况下都尝试了调试器,发现javax.mail.internet.MimeMessage实例的创建方式有所不同.该Web应用程序正在使用Spring发送从Apache ActiveMQ队列中挑选的电子邮件.当以mvn jetty:run身份运行应用程序时,MimeMessage.contentStream变量用于消息内容.当以mvn jetty:run-war身份运行时,MimeMessage.content变量用于消息内容,而 内容= ASCIIUtility.getBytes(is); 调用将从解析的内容中删除所有标头数据.既然这看起来很奇怪,并且调试Spring/ActiveMQ的工作很深入,所以我创建了一个简化的测试,没有任何基础结构:只是使用mail-1.4.2.jar的JSP,但缺少相同的标头.

Curiously, I've tried a debugger in both situations and found that the javax.mail.internet.MimeMessage instance is getting created differently. The webapp is using Spring to send email picked off of an Apache ActiveMQ queue. When running the app as mvn jetty:run the MimeMessage.contentStream variable is used for message content. When running as mvn jetty:run-war, the MimeMessage.content variable is used for the message contents, and the content = ASCIIUtility.getBytes(is); call removes all of the header data from the parsed content. Since this seemed very odd, and debugging Spring/ActiveMQ is a deep dive, I created a simplified test without any of that infrastructure: just a JSP using mail-1.4.2.jar, yet the same headers are missing.

还要注意,在Tomcat 5.5.27下运行WAR文件时,缺少这些标头.在运行WAR时,Tomcat的行为就像Jetty,但缺少相同的标头.

Also of note, these headers are missing when running the WAR file under Tomcat 5.5.27. Tomcat behaves just like Jetty when running the WAR, with the same missing headers.

打开JavaMail调试后,我清楚地看到了不同的输出.

With JavaMail debugging turned on, I clearly see different output.

好的情况:在码头:运行(非战争)中,日志输出为:

GOOD CASE: In the jetty:run (non-WAR) the log output is:

DEBUG: JavaMail version 1.4.2
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.authsmtp.com", port 465, isSSL false
220 mail.authsmtp.com ESMTP Sendmail 8.14.2/8.14.2/Kp; Thu, 18 Jun 2009 01:35:24 +0100 (BST)
DEBUG SMTP: connected to host "mail.authsmtp.com", port: 465

EHLO jmac.local
250-mail.authsmtp.com Hello sul-pubs-3a.Stanford.EDU [171.66.201.2], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE 52428800
250-AUTH CRAM-MD5 DIGEST-MD5 LOGIN PLAIN
250-DELIVERBY
250 HELP
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "AUTH", arg "CRAM-MD5 DIGEST-MD5 LOGIN PLAIN"
DEBUG SMTP: Found extension "DELIVERBY", arg ""
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 
AUTH LOGIN
334 VXNlcm5hjbt7
YWM0MDkwhi==
334 UGFzc3dvjbt7
YXV0aHNtdHAydog3
235 2.0.0 OK Authenticated
DEBUG SMTP: use8bit false
MAIL FROM:<webmaster@mydomain.org>
250 2.1.0 <webmaster@mydomain.org>... Sender ok
RCPT TO:<jason@mydomain.org>
250 2.1.5 <jason@mydomain.org>... Recipient ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   Jason Thrasher <jason@mydomain.org>
DATA
354 Enter mail, end with "." on a line by itself
From: Webmaster <webmaster@mydomain.org>
To: Jason Thrasher <jason@mydomain.org>
Message-ID: <5158456.0.1245285323633.JavaMail.jason@mail.authsmtp.com>
Subject: non-Spring: Hello World
MIME-Version: 1.0
Content-Type: text/plain;charset=UTF-8
Content-Transfer-Encoding: 7bit

Hello World: message body here
.
250 2.0.0 n5I0ZOkD085654 Message accepted for delivery
QUIT
221 2.0.0 mail.authsmtp.com closing connection

坏的情况:当作为WAR运行时,缺少标题的日志输出是完全不同的:

BAD CASE: The log output when running as a WAR, with missing headers, is quite different:

Loading javamail.default.providers from jar:file:/Users/jason/.m2/repository/javax/mail/mail/1.4.2/mail-1.4.2.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null
Loading javamail.default.providers from jar:file:/Users/jason/Documents/dev/subscribeatron/software/trunk/web/struts/target/work/webapp/WEB-INF/lib/mail-1.4.2.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: getProvider() returning provider protocol=smtp; type=javax.mail.Provider$Type@98203f; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "mail.authsmtp.com", port 465, isSSL false
220 mail.authsmtp.com ESMTP Sendmail 8.14.2/8.14.2/Kp; Thu, 18 Jun 2009 01:51:46 +0100 (BST)
DEBUG SMTP: connected to host "mail.authsmtp.com", port: 465

EHLO jmac.local
250-mail.authsmtp.com Hello sul-pubs-3a.Stanford.EDU [171.66.201.2], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE 52428800
250-AUTH CRAM-MD5 DIGEST-MD5 LOGIN PLAIN
250-DELIVERBY
250 HELP
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "AUTH", arg "CRAM-MD5 DIGEST-MD5 LOGIN PLAIN"
DEBUG SMTP: Found extension "DELIVERBY", arg ""
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 
AUTH LOGIN
334 VXNlcm5hjbt7
YWM0MDkwhi==
334 UGFzc3dvjbt7
YXV0aHNtdHAydog3
235 2.0.0 OK Authenticated
DEBUG SMTP: use8bit false
MAIL FROM:<webmaster@mydomain.org>
250 2.1.0 <webmaster@mydomain.org>... Sender ok
RCPT TO:<jason@mydomain.org>
250 2.1.5 <jason@mydomain.org>... Recipient ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   Jason Thrasher <jason@mydomain.org>
DATA
354 Enter mail, end with "." on a line by itself

Hello World: message body here
.
250 2.0.0 n5I0pkSc090137 Message accepted for delivery
QUIT
221 2.0.0 mail.authsmtp.com closing connection

这是我用来测试战争/非战争的实际mail.jsp.

Here's the actual mail.jsp that I'm testing war/non-war with.

<%@page import="java.util.*"%>
<%@page import="javax.mail.internet.*"%>
<%@page import="javax.mail.*"%>

<%
    InternetAddress from = new InternetAddress("webmaster@mydomain.org", "Webmaster");
    InternetAddress to = new InternetAddress("jason@mydomain.org", "Jason Thrasher");
    String subject = "non-Spring: Hello World";
    String content = "Hello World: message body here";

    final Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.host", "mail.authsmtp.com");
    props.setProperty("mail.port", "465");
    props.setProperty("mail.username", "myusername");
    props.setProperty("mail.password", "secret");
    props.setProperty("mail.debug", "true");
    props.setProperty("mail.smtp.auth", "true");
    props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.setProperty("mail.smtp.socketFactory.fallback", "false");

    Session mailSession = Session.getDefaultInstance(props);

    Message message = new MimeMessage(mailSession);
    message.setFrom(from);
    message.setRecipient(Message.RecipientType.TO, to);
    message.setSubject(subject);
    message.setContent(content, "text/plain;charset=UTF-8");

    Transport trans = mailSession.getTransport();
    trans.connect(props.getProperty("mail.host"), Integer
            .parseInt(props.getProperty("mail.port")), props
            .getProperty("mail.username"), props
            .getProperty("mail.password"));
    trans.sendMessage(message, message
            .getRecipients(Message.RecipientType.TO));
    trans.close();
%>

email was sent

解决方案:

是的,问题是Apache CXF 2的可传递依赖关系.我不得不从构建中排除geronimo-javamail_1.4_spec,而仅依靠javax的mail-1.4.jar.

Yes, the problem was transitive dependencies of Apache CXF 2. I had to exclude geronimo-javamail_1.4_spec from the build, and just rely on javax's mail-1.4.jar.

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>2.2.6</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-javamail_1.4_spec</artifactId>
        </exclusion>
    </exclusions>
</dependency>

感谢所有答案.

推荐答案

在将其构建为WAR时是否有可能引入了其他依赖项?似乎其他人也遇到了此问题,并且原因是geronimo中的错误(有关更多信息,请参见

Is it possible that when you build it as a WAR that other dependencies are being pulled in? It seems others have encountered this problem, and the cause was a bug in geronimo (for more information, see http://mail-archives.apache.org/mod_mbox/geronimo-user/200902.mbox/%3C21943498.post@talk.nabble.com%3E ).

我们的产品中还包含了geronimo-javamail_1.4_spec-1.2,并且该软件包存在带有标头的错误.我们从依赖项中排除了此问题(以及geronimo-activation_1.1_spec),并纠正了问题.

We also had geronimo-javamail_1.4_spec-1.2 included in our product and that package has a bug with headers. We excluded this (along with geronimo-activation_1.1_spec) from our dependencies and it corrected the problem.

这篇关于JavaMail未在Jetty:run-war下发送主题或发件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆