Java Mail:获取退回邮件以转到与发件人不同的地址

时间:2013-08-15 21:33:18

标签: javamail bounce

我正在尝试让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

2 个答案:

答案 0 :(得分:1)

您需要设置“信封来自”地址。有关可用于设置它的属性,请参阅com.sun.mail.smtp包的javadocs,或使用SMTPMessage类进行设置。

然后希望邮件服务器弹出消息是正确的,并遵循规范......

答案 1 :(得分:0)

此stackoverflow post说明您需要设置发件人使用MimeMessage。 addFrom()并且您需要设置“mail.smtp.host”

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 bounceAddr  = "bounce@acme.com";
            String body        = "Test: get message to bounce to a separate email address";

            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);
            props.put("mail.smtp.from", bounceAddr);

            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.addFrom(InternetAddress.parse(from)); 
            message.setRecipients(Message.RecipientType.TO, to);
            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()

    }// end class SendEmail