如何使用JavaMail将Return-Path设置为与发件人地址不同的电子邮件地址?
如何使用JavaMail将Return-Path设置为与发件人地址不同的电子邮件地址?
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"));
我曾经遇到过相同的问题,并发现唯一讨论的解决方案是将属性"mail.smtp.from"放在props.put("mail.smtp.from", "replyto@example.com")中。
尽管如此,这个解决方案对我来说并不适用,因为我要从不同用户那里发送很多电子邮件,为每个电子邮件重新创建会话会对生产力造成可怕的影响。
因此,在阅读JavaMail源代码后,我找到了另一个解决方案:
1)使用SMTPMessage(扩展MimeMessage)而不是MimeMessage。
2)使用setEnvelopeFrom(String)方法。
3)使用SMTPTransport发送电子邮件(我没有尝试其他方法)。
以下是代码示例:
SMTPMessage message = new SMTPMessage(session);
message.setEnvelopeFrom("returnpath@hotmail.com");
...
transport.sendMessage(message, message.getAllRecipients());
props.put("mail.smtps.from", "replyto@example.com");