3

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

2 回答 2

1

您需要设置“信封发件人”地址。请参阅 com.sun.mail.smtp 包的 javadocs 以了解您可以用来设置它的属性,或者使用 SMTPMessage 类来设置它。

然后希望退回邮件的邮件服务器做正确的事情并遵循规范......

于 2013-08-16T07:09:47.423 回答
1

这篇 stackoverflow帖子解释说,您需要使用 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
于 2013-08-16T14:27:03.513 回答