JavaMailSenderImpl没有使用" mail.from"来自邮件会话的财产

时间:2016-12-12 21:15:40

标签: java spring email

我想发送一封带有From标题的邮件,但我不想在SimpleMailMessage中设置From标题。我想使用mail.fromjavax.mail.Session属性配置的默认值(在context.xml中定义)。

Java的MimeMessage#setFrom方法支持该功能:

  

设置RFC 822" From"标头字段使用InternetAddress.getLocalAddress方法的值。

JavaMailSenderImpl并未使用该属性。

Tomcat' context.xml

<Resource name="mail" type="javax.mail.Session" mail.debug="true"
        mail.smtp.host="mail.mycompany.com" mail.from="no-reply@mycompany.com"/>

Spring beans.xml

<bean id="mailSession" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/mail" />
</bean>

<bean id="JavaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="session" ref="mailSession" />
</bean>

我的服务:

@Named
public class MailService {

    @Inject
    private JavaMailSender javaMailSender;

    public void send() {

        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo("customer@company.com");
        message.setSubject("Test");
        message.setText("Test message body");

        javaMailSender().send(message);
    }
}

记录:

DEBUG: JavaMail version 1.5.5
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,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "mail.mycompany.com", port 25, isSSL false
220 mail.mycompany.com ESMTP ready.
DEBUG SMTP: connected to host "mail.mycompany.com", port: 25

EHLO dev.mycompany.com
250-mail.mycompany.com Hello dev.mycompany.com [x.x.x.x]
250-SIZE 41943040
250-8BITMIME
250-PIPELINING
250-STARTTLS
250 HELP
DEBUG SMTP: Found extension "SIZE", arg "41943040"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<no-reply@mycompany.com>
250 OK
RCPT TO:<customer.company.com>
250 Accepted
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   customer@company.com
DATA
354 Enter message, ending with "." on a line by itself
Date: Mon, 12 Dec 2016 21:45:10 +0100 (CET)
To: customer@company.com
Message-ID: <1838762963.0.1481575510043@mycompany.com>
Subject: Test
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Test message body
.
250 OK id=1cGXTB-0007La-35
DEBUG SMTP: message successfully delivered to mail server
QUIT
221 mail.mycompany.com closing connection

邮件没有From标头。

我的代码有什么问题吗?为什么Spring不使用邮件会话中的From标头?

1 个答案:

答案 0 :(得分:0)

从此链接:http://springframework-user.narkive.com/wbF105Ma/simplemailmessage-and-date-header

  

SimpleMailMessage或多或少是COS'和JavaMail邮件发送功能的最小公分母。实际上,应该可以用COS设置日期标题;它只是在COS MailMessage类中没有明确的方法。因此,我们可能会在以后的版本中为SimpleMailMessage提供日期标头支持。

     

目前,您必须使用JavaMailSender接口和JavaMail的MimeMessage类。使用Spring提供的MimeMessageHelper类,应该很容易以SimpleMailMessage样式的方式填充MimeMessage。 (有关详细信息,请参阅MimeMessageHelper的javadoc。)当然,MimeMessage功能更强大:它支持附件,文本编码等。

我搜索并找到了那些。你尝试哪一个:

1:

MimeMessage mimeMessage = //get message from wherever
mimeMessage.addHeader("From", "me@mail.com");

2:

MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
 message.setFrom("me@mail.com");