使用 JavaMail API 重定向无法使用 mail.smtp.host 的退回电子邮件

Redirecting Bounced Emails not working with mail.smtp.host using JavaMail API

即使我尝试了各种端口(587、465 和 25)、SMTP 服务器(smtp.gmail.com、smtp.live.com 和 smtp.mail.yahoo.com,重定向退回的电子邮件对我也不起作用)、消息对象(MimeMessage、Message 和 SMTPMessage)和 JavaMail API 方法(message.addFrom、message、setReplyTo 和 message.setHeader)。我还尝试更改 javax.mail jar 版本 1.4、1.5 等。他们在 Whosebug 中到处都在说,将另一封电子邮件设置为 mail.smtp.from 应该可以。但是我所有被退回的电子邮件都会返回给发件人。不发送到指定的电子邮件地址。有人可以帮我将它重定向到一个新的电子邮件地址吗? 这是我使用的代码

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;

import com.sun.mail.smtp.SMTPMessage;

public class SendEmail {
   public static void main(String[] args) throws Exception {
     Properties properties=new Properties();
     InputStream input=new FileInputStream("SendEmail.properties");
     properties.load(input);
       // String smtpServer = "smtp.gmail.com";
      String smtpServer = "smtp.live.com";
      int port = 587;
      final String userid = "myemail@hotmail.com";//change accordingly
      final String password = properties.getProperty("EMAIL_PASSWORD1");
      String contentType = "text/html";
      String subject = "test: bounce an email to a different address " +
                "from the sender";
      String to = "bounceee@fauxmail.com";//some invalid address
      String bounceAddr = "bounceremail@gmail.com";//change accordingly
      String body = "Test: get message to bounce to a separate email address";

      Properties props = new Properties();

      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", smtpServer);
      props.put("mail.smtp.port", "465");
      props.put("mail.transport.protocol", "smtp");
      props.put("mail.smtp.from", bounceAddr);

      Session mailSession = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(userid, password);
            }
         });

     // MimeMessage message = new MimeMessage(mailSession);
      SMTPMessage message=new SMTPMessage(mailSession);
      message.addFrom(InternetAddress.parse(userid));
      message.setRecipients(Message.RecipientType.TO, to);
      message.setHeader("Return-path", bounceAddr);
      message.setSubject(subject);
      message.setContent(body, contentType);


      Transport transport = mailSession.getTransport();
      try {
         System.out.println("Sending ....");
         transport.connect(smtpServer, port, userid, password);
         transport.sendMessage(message,
            message.getRecipients(Message.RecipientType.TO));
         System.out.println("Sending done ...");
      } catch (Exception e) {
         System.err.println("Error Sending: ");
         e.printStackTrace();

      }
      transport.close();
   }// end function main()
}

Public 服务器不太可能允许您将信封发件人地址设置为他们不知道您拥有的地址,以防止您以退回邮件的形式间接发送垃圾邮件.如果服务器不允许您将其用作常规发件人地址,您可能也不能将其用作信封发件人地址。对于 Gmail,this page 会告诉您如何使用不同的地址。

请注意,您可以通过打开 JavaMail debug output 并查找 MAIL FROM 行来查看正在使用的地址信封。