English 中文(简体)
How to set the Return-Path to an email address other than Sender address using JavaMail?
原标题:

How to set the Return-Path to an email address other than Sender address using JavaMail?

最佳回答

The code below does what you want, and does it in the correct way. Reread what you yourself posted in the comment

From: RFC2821: 4.4 Trace Information

When the delivery SMTP server makes the "final delivery" of a message, it inserts a return-path line at the beginning of the mail data. This use of return-path is required; mail systems MUST support it. The return-path line preserves the information in the from the MAIL command. Here, final delivery means the message has left the SMTP environment. Normally, this would mean it had been delivered to the destination user or an associated mail drop, but in some cases it may be further processed and transmitted by another mail system.

and a few lines later.

A message-originating SMTP system SHOULD NOT send a message that already contains a Return-path header.

If you carefully read this you will understand that only the final smtp-server/delivery agent is supposed to add the Return-Path header. It is not something you as client (trying to send a mail) should do. The final smtp-server will base the Return-Path header on the sender address of the envelope (SMTP MAIL FROM part).

So setting mail.smtp.from is the correct way to tell java that the envelope sender address should be different from the from part.

If you have troubles understanding what the different from s are just take a look at a telnet smtp-session. Where replyto@example.com should correspond to mail.smtp.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 the only solution discussed putting property "mail.smtp.from" props.put("mail.smtp.from", "replyto@example.com");

Still this solution was not suitable for me because I m sending lot s of e-mails from different users, so recreating session for each e-mail would be horrible for prodictivity.

So I found another solution after reading JavaMail sources:

1) Use SMTPMessage(extends MimeMessage) instead of MimeMessage.

2) Use setEnvelopeFrom(String) method.

3) Use SMTPTransport to send e-mail (I didn t try 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 is set to something other than smtp (like smtps ), then only the following would work:

props.put("mail.smtps.from", "replyto@example.com");

This allowed me to avoid using the SMTPMessage class as described in GiorgosDev s answer (classes in the com.sun package aren t intended to be public API).





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签