java 如何使用 JavaMail 将返回路径设置为发件人地址以外的电子邮件地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1782659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:57:46  来源:igfitidea点击:

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

javaemailjavamail

提问by Ricardo

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

如何使用 JavaMail 将返回路径设置为发件人地址以外的电子邮件地址?

回答by jitter

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.

来自:RFC2821:4.4 跟踪信息

当传递 SMTP 服务器对邮件进行“最终传递”时,它会在邮件数据的开头插入一个返回路径行。需要使用返回路径;邮件系统必须支持它。return-path 行保留来自 MAIL 命令的信息。此处,最终传递意味着邮件已离开 SMTP 环境。通常,这意味着它已被投递到目标用户或相关的邮件投递处,但在某些情况下,它可能会被另一个邮件系统进一步处理和传输。

and a few lines later.

几行之后。

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

邮件发起 SMTP 系统不应该发送已经包含返回路径标头的邮件。

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

如果您仔细阅读本文,您就会明白只有最终的 smtp-server/delivery agent 才应该添加Return-Path标头。这不是您作为客户(尝试发送邮件)应该做的事情。最终的 smtp-server 将Return-Path根据信封(SMTP MAIL FROM部分)的发件人地址确定标头。

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

所以设置mail.smtp.from是告诉java信封发件人地址应该与from部分不同的正确方法。

If you have troubles understanding what the different from's are just take a look at a telnet smtp-session. Where [email protected]should correspond to smtp.mail.fromand [email protected]to m.addFrom(...);

如果您在理解不同from的内容时遇到困难,请查看 telnet smtp-session。哪里[email protected]应该对应smtp.mail.from[email protected]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:<[email protected]>
250 <[email protected]> is syntactically correct

rcpt to:<[email protected]>
250 <[email protected]> verified

data
354 Enter message, ending with "." on a line by itself
To: Joey <[email protected]>
From: Joey <[email protected]> 
Subject: Joey

Hey Joey!

.
250 OK id=....

Quit


props.put("mail.smtp.from", "[email protected]");
Session session = Session.getDefaultInstance(props, null);
MimeMessage m = new MimeMessage(session);
m.addFrom(InternetAddress.parse("[email protected]"));

回答by GiorgosDev

I've experienced the same issue and found the only solution discussed putting property "mail.smtp.from" props.put("mail.smtp.from", "[email protected]");

我遇到了同样的问题,并找到了讨论放置属性“mail.smtp.from” props.put("mail.smtp.from", "[email protected]"); 的唯一解决方案。

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:

所以我在阅读 JavaMail 源代码后找到了另一个解决方案:

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

1) 使用 SMTPMessage(extends MimeMessage) 而不是 MimeMessage。

2) Use setEnvelopeFrom(String) method.

2) 使用 setEnvelopeFrom(String) 方法。

3) Use SMTPTransport to send e-mail (I didn't try with others).

3)使用SMTPTransport发送邮件(我没试过用其他的)。

Here is a code example:

这是一个代码示例:

SMTPMessage message = new SMTPMessage(session);
message.setEnvelopeFrom("[email protected]");
...
transport.sendMessage(message, message.getAllRecipients());

回答by davepletcher

I found that if the 'mail.protocol' property is set to something other than 'smtp' (like 'smtps'), then only the following would work:

我发现如果 'mail.protocol' 属性设置为 'smtp' 以外的值(如 'smtps'),那么只有以下内容有效:

props.put("mail.smtps.from", "[email protected]");

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).

这使我可以避免使用 GiorgosDev 的回答中描述的 SMTPMessage 类('com.sun' 包中的类不是公共 API)。