【问题标题】:Configure Return Path with Gmail and Java使用 Gmail 和 Java 配置返回路径
【发布时间】:2013-10-30 19:29:18
【问题描述】:

我有一个正常工作的电子邮件服务,通常通过默认地址发送电子邮件,比如说admin@mycomp.com

现在我正在尝试在电子邮件中添加return-path,这样每当我收到电子邮件时,我就可以直接回复发件人的电子邮件。以下是我配置属性的方式:

private void sendOut() {
        props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.from", "abc@google.com");
        setJavaMailProperties(props);
        Message message = new MimeMessage(getSession());
        message.setFrom(new InternetAddress("admin@mycomp.com"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("admin@mycomp.com"));
        message.setSubject("subject");
        message.setText("content");
        Transport.send(message);

    }

但是在发出电子邮件后,我仍然看到电子邮件是从我自己的电子邮件admin@mycomp.com 发出的。我根据How to set the Return-Path to an email address other than Sender address using JavaMail? 此处的答案添加了mail.smtp.from。我在这里错过了什么?

【问题讨论】:

    标签: java email jakarta-mail


    【解决方案1】:

    从您链接的同一个答案看来,必须从 smtp 服务器的服务器端允许此类服务才能工作。

    在最后一个实例中,SMTP 服务器将在正在发送的消息中写入返回路径标头,并决定将回复发送到哪个地址。

    1. 我尝试了答案中解释的相同方法(设置 props.put("mail.smtp.from", "abc@google.com")),但在多个 SMTP 客户端上均未成功。

    2. 我尝试使用 SMTPMessage 而不是 MimeMessage,如另一个答案中所述:

      SMTPMessage 消息 = 新的 SMTPMessage(会话); message.setEnvelopeFrom("returnpath@hotmail.com"); ... transport.sendMessage(message, message.getAllRecipients());

    这是我的 smtp 服务器回复我的内容:

    EHLO frankieta
    250-smtpcmd04.ad.aruba.it hello frankieta, pleased to meet you
    250-HELP
    250-AUTH LOGIN PLAIN
    250-SIZE 524288000
    250-ENHANCEDSTATUSCODES
    250-8BITMIME
    250-STARTTLS
    250 OK
    DEBUG SMTP: Found extension "HELP", arg ""
    DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
    DEBUG SMTP: Found extension "SIZE", arg "524288000"
    DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
    DEBUG SMTP: Found extension "8BITMIME", arg ""
    DEBUG SMTP: Found extension "STARTTLS", arg ""
    DEBUG SMTP: Found extension "OK", arg ""
    DEBUG SMTP: Attempt to authenticate
    DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
    DEBUG SMTP: AUTH LOGIN command trace suppressed
    DEBUG SMTP: AUTH LOGIN succeeded
    DEBUG SMTP: use8bit false
    MAIL FROM:<reply_to@mail.com>
    250 2.1.0 <reply_to@mail.com> sender ok
    RCPT TO:<receiver@mail.it>
    250 2.1.5 <original_sender@mail.it> recipient ok
    DEBUG SMTP: Verified Addresses
    DEBUG SMTP:   original_sender@mail.it
    DATA
    354 enter mail, end with "." on a line by itself
    From: original_sender@mail.it
    To: receiver@mail.it
    

    从receiver@mail.it 收到的邮件显示了这个标题:

    Return-Path: <reply_to@mail.com>
    Delivered-To: receiver@mail.it
    
    Received: from frankieta by smtp.server.com with bizsmtp
    From: original_sender@mail.it
    To: receiver@mail.it
    

    因此,即使在收到的邮件中将返回路径正确设置为 reply_to@mail.com,尝试回复此邮件仍需要 original_sender@mail.it > 作为收件人。

    来自的信封通常用于退回错误邮件(例如错误的收件人)。如果您尝试将邮件发送给错误的收件人,它将退回到 reply_to@mail.com 地址。 所以我猜这是smtp服务器手中的东西。

    希望对你有所帮助。

    【讨论】:

    • 感谢您的回答。我用 Gmail 和 Zoho 都试过了,但是它们给出了相同的结果。似乎这些问题发生在最广泛使用的电子邮件服务器上。
    • 您可能需要按照说明 here 设置与您的 Gmail 地址不同的信封。另外,请参阅common JavaMail mistakes 列表。
    • 似乎大约 0.5% 的电子邮件服务器需要 Return-path 以匹配 From。仍然很烦人,具体取决于您的听众。
    猜你喜欢
    • 1970-01-01
    • 2013-07-21
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 2018-02-07
    • 2013-01-13
    • 1970-01-01
    相关资源
    最近更新 更多