[ACCEPTED]-How to set the Return-Path to an email address other than Sender address using JavaMail?-jakarta-mail
The code below does what you want, and does 30 it in the correct way. Reread what you yourself 29 posted in the comment
From: RFC2821: 4.4 Trace Information
When the delivery 28 SMTP server makes the "final delivery" of 27 a message, it inserts a return-path line 26 at the beginning of the mail data. This 25 use of return-path is required; mail systems 24 MUST support it. The return-path line 23 preserves the information in the from 22 the MAIL command. Here, final delivery 21 means the message has left the SMTP environment. Normally, this would 20 mean it had been delivered to the destination 19 user or an associated mail drop, but in 18 some cases it may be further processed 17 and transmitted by another mail system.
and 16 a few lines later.
A message-originating 15 SMTP system SHOULD NOT send a message 14 that already contains a Return-path header.
If 13 you carefully read this you will understand 12 that only the final smtp-server/delivery 11 agent is supposed to add the Return-Path
header. It 10 is not something you as client (trying to 9 send a mail) should do. The final smtp-server 8 will base the Return-Path
header on the sender address 7 of the envelope (SMTP MAIL FROM
part).
So setting mail.smtp.from
is the 6 correct way to tell java that the envelope 5 sender address should be different from 4 the from
part.
If you have troubles understanding 3 what the different from
's are just take a look 2 at a telnet smtp-session. Where replyto@example.com
should 1 correspond to smtp.mail.from
and from@example.com
to m.addFrom(...);
telnet smtp.example.com 25
220 smtp.example.com ESMTP .....
helo computername
250 smtp.example.com Hello computername [123.123.123.123]
mail from:<replyto@example.com>
250 <replyto@example.com> is syntactically correct
rcpt to:<rcpt@foo.com>
250 <rcpt@foo.com> verified
data
354 Enter message, ending with "." on a line by itself
To: Joey <to@joey.com>
From: Joey <from@example.com>
Subject: Joey
Hey Joey!
.
250 OK id=....
Quit
props.put("mail.smtp.from", "replyto@example.com");
Session session = Session.getDefaultInstance(props, null);
MimeMessage m = new MimeMessage(session);
m.addFrom(InternetAddress.parse("from@example.com"));
I've experienced the same issue and found 11 the only solution discussed putting property 10 "mail.smtp.from" props.put("mail.smtp.from", "replyto@example.com");
Still 9 this solution was not suitable for me because 8 I'm sending lot's of e-mails from different 7 users, so recreating session for each e-mail 6 would be horrible for prodictivity.
So I 5 found another solution after reading JavaMail 4 sources:
1) Use SMTPMessage(extends MimeMessage) instead 3 of MimeMessage.
2) Use setEnvelopeFrom(String) method.
3) Use 2 SMTPTransport to send e-mail (I didn't try 1 with others).
Here is a code example:
SMTPMessage message = new SMTPMessage(session);
message.setEnvelopeFrom("returnpath@hotmail.com");
...
transport.sendMessage(message, message.getAllRecipients());
I found that if the 'mail.protocol' property 6 is set to something other than 'smtp' (like 5 'smtps'), then only the following would 4 work:
props.put("mail.smtps.from", "replyto@example.com");
This allowed me to avoid using the 3 SMTPMessage class as described in GiorgosDev's 2 answer (classes in the 'com.sun' package 1 aren't intended to be public API).
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.