我正在尝试让 Java 邮件将退回的电子邮件发送到与发件人地址不同的地址,并且根本不将退回邮件发送给发件人。
到目前为止,我不能在测试程序中做任何一个(下面)。
发件人是“joe@acme.com”。我希望退回邮件只发送到“bounce@acme.com”
我正在尝试设置回复地址和 Return-Path: 标头,但退回邮件不会发送到bounce@acme.com,只会发送到 joe@acme.com
在查看退回邮件的标头时,Return-Path: 标头设置为发件人 joe@acme.com,而不是按照我希望的方式设置为bounce@acme.com。
我正在使用 javamail 1.4
提前感谢您的任何帮助或提示
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;
public class SendEmail {
public static void main(String[] args) throws Exception{
String smtpServer = "msg.abc.acme.cp,";
int port = 25;
String userid = "authorized.person";
String password = "password";
String contentType = "text/html";
String subject = "test: bounce an email to a different address from the sender";
String from = "joe@acme.com";
String to = "bogus@fauxmail.com";
String replyto = "bounce@acme.com";
String body = "Test: get message to bounce to a separate email address";
InternetAddress[] arrayReplyTo = new InternetAddress[1];
arrayReplyTo[0] = new InternetAddress(replyto);
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", smtpServer);
Session mailSession = Session.getInstance(props);
// Get runtime more runtime output when attempting to send an email
//mailSession.setDebug(true);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.setReplyTo(arrayReplyTo);
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject(subject);
message.setContent(body,contentType);
message.setHeader("Return-Path:","<bounce@acme.com>");
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()
}// end class SendEmail