I want to send a mail with a From
header, but I don't want to set the From
header in the SimpleMailMessage
. I want to use the default configured with mail.from
property for the javax.mail.Session
(defined in context.xml
).
Java's MimeMessage#setFrom
method supports that feature:
Set the RFC 822 "From" header field using the value of the InternetAddress.getLocalAddress method.
But JavaMailSenderImpl
doesn't use the property.
Tomcat's 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's 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>
My service:
@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);
}
}
Log:
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
The message has no From
header.
Is there anything wrong with my code? Why Spring doesn't use the From
header from mail session?