JavaMailSenderImpl 不使用邮件 session 中的 "mail.from"属性

标签 java spring email

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

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

Set the RFC 822 "From" header field using the value of the InternetAddress.getLocalAddress method.

但是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 header 。

我的代码有什么问题吗?为什么 Spring 不使用邮件 session 中的 From header ?

最佳答案

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

SimpleMailMessage is more or less the least common denominator of COS' and JavaMail's mail sending capabilities. Actually, it should be possible to set the date header with COS too; it's just that there is no explicit method for it in the COS MailMessage class. So we might provide date header support for SimpleMailMessage in a future point release.

For the time being, you'll have to work with the JavaMailSender interface and JavaMail's MimeMessage class. With the MimeMessageHelper class that Spring provides, it should be easy enough to populate a MimeMessage in a SimpleMailMessage-style fashion. (See MimeMessageHelper's javadoc for details.) Of course, MimeMessage is much more powerful: It supports attachements, text encoding, etc.

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

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");

关于JavaMailSenderImpl 不使用邮件 session 中的 "mail.from"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41109709/

相关文章:

java - 涉及继承和泛型的 Java 泛型的用处是什么 extends self

java - 传递一个字符串作为构造函数参数并获取 "illegal start of type"

java - 带有 JPA 存储库的 Spring Boot 静态 where 子句

java - Controller 类中的 Spring MVC 本地化不起作用

vb.net - SmtpMail - 将 "From Address"更改为名称

java - mac、java 1.5 : javax. 邮件无法识别为 jar 文件

email - 第 697 行上的 fatal error : Class 'PEAR' not found in/home/user1/public_html/go-pear. php

java - 实例化实现接口(interface)的给定类的对象,并使用反射将其添加到 List<InterfaceX>

java - 您如何在 Clean Architecture 中实现一对多关系

java - 将spring持久化存储库注入(inject)到服务层