1

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?

Community
  • 1
  • 1
dur
  • 15,689
  • 25
  • 79
  • 125
  • Hello! Did you find solution for this problem? – mohax Oct 12 '19 at 20:35
  • @mohax It is too long ago, I can't remember. But I didn't write an answer, so I guess, I didn't find a way. – dur Oct 12 '19 at 21:01
  • OK, thanks. Maybe there is issue with DKIM. Tomorrow I'll try to sign email as described [here](https://stackoverflow.com/q/13833098/3212712) and write about results) – mohax Oct 12 '19 at 21:03

1 Answers1

0

From this link: 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.

I search and found those. which one you try it :

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");
vahid
  • 446
  • 6
  • 21
  • Your update sets the `From` header on the message, that's not what I want, I want to use the `From` header configured in mail session (don't call a setter on the message with the value). – dur Dec 12 '16 at 22:12
  • @dur ok. so this code not working : JavaMailSenderImpl sender = new JavaMailSenderImpl(); Properties properties = new Properties(); properties.put("From", "a@b.com"); sender.setJavaMailProperties(properties); – vahid Dec 12 '16 at 22:17
  • It creates a new session instead of using the existing one. The session is in JNDI, my implementation doesn't know the properties. – dur Dec 12 '16 at 22:21
  • @dur I don't know much about JNDI but you can't get session from JNDI and set properties and back again to JNDI? – vahid Dec 12 '16 at 22:25
  • I could do that, but it is real ugly. – dur Dec 12 '16 at 22:28