gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:56 25 4
gpt4 key购买 nike

我想发送一封带有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com