Java Mail:将退回的邮件发送到与发件人不同的地址 [英] Java Mail: Getting Bounced Messages To Go To An Address Different From The Sender

查看:83
本文介绍了Java Mail:将退回的邮件发送到与发件人不同的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图让Java邮件将退回的电子邮件发送到与发件人地址不同的地址,而根本不将退回的邮件发送给发件人.

I'm trying to get Java mail to send bounced email to a different address than the sender's address and not send the bounce message to the sender at all.

到目前为止,我无法在测试程序中进行任何操作(如下所示).

So far I can't do either in a test program ( below ).

发件人为"joe@acme.com". 我希望发送退回邮件,并且只发送到"bounce@acme.com"

The sender is "joe@acme.com". I want bounce messages to go and only go to "bounce@acme.com"

我正在尝试设置回复地址和Return-Path:标头,但退回邮件不会发送到bounce@acme.com,而只会发送到joe@acme.com

I'm trying setting both the reply-to address and the Return-Path: header, but the bounces do not go to bounce@acme.com, only to joe@acme.com

查看退回邮件的标头时,Return-Path:标头已设置为发件人joe@acme.com,而不是按我希望的方式设置为bounce@acme.com.

When looking at the header of the bounce message the Return-Path: header is getting set to the sender, joe@acme.com, not to bounce@acme.com the way I want it to be.

我正在使用javamail 1.4

I'm using javamail 1.4

在此先感谢您的帮助或提示

Thanks in advance for any help or tips

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

推荐答案

此stackoverflow

This stackoverflow post explains that you need to set the sender's from using MimeMessage.addFrom() and that you need to set the "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

这篇关于Java Mail:将退回的邮件发送到与发件人不同的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆