最近在做web 邮箱方面的功能,在网上也找了些资料,实现了基本功能,但最近遇到一个需求:发送邮件不成功时回置邮件,但是发送邮件的邮箱[email protected]和 [email protected] 不是同一个地址
不知道 可以实现否
自己测试过不论下面代码的[email protected] 地址怎么写都是回置到发送邮件的[email protected]地址
不知为何,特来此向高手虚心请教message.setHeader( "Return-Receipt-To", "[email protected]");

解决方案 »

  1.   

    javamail里面有个属性可以设置. 具体方法名称忘记了.email.setReplyTo还是什么的. 可以设置回执邮件地址.
      

  2.   

    MimeMessage对象有个setReplyTo方法,试试看行不行
      

  3.   

    我的测试代码public void SendMail2(String title, String attachment){        // 获得属性,并生成Session对象 
            Properties props = new Properties();
            Session sendsession;
            Transport transport;
            MimeMessage message = null;
            BodyPart messageBodyPart = new MimeBodyPart();
            Multipart multipart = new MimeMultipart();
            String from = "[email protected]";
            String to[]={"[email protected]"};
            String cc[] =null;//{"[email protected]"};
            String bcc[] =null;// {"[email protected]"};
            String content = "<font style=\"BACKGROUND-COLOR: #666699\" color=\"#ff0000\" size=\"12px\">测试格式化内容测试<a href=\"\">格式化内容</a>测试格<em>式化</em>内容</font>";
            content ="测试";
            
            try{            sendsession = Session.getInstance(props, null);
                //向属性中写入SMTP服务器的地址
                props.put("mail.smtp.host", "smtp.139.com");
                //设置SMTP服务器需要权限认证
                props.put("mail.smtp.auth", "true");
                //设置输出调试信息
                // sendsession.setDebug(true);
                //根据Session生成Message对象
                message = new MimeMessage(sendsession);
                //设置发信人地址
                message.setFrom(new InternetAddress(from));
                //设置收信人地址
                String toList = getMailList(to);
                InternetAddress[] iaToList = new InternetAddress().parse(toList);
                message.setRecipients(Message.RecipientType.TO,iaToList);
                message.setHeader( "Disposition-Notification-To", "[email protected]");
                message.setHeader( "Return-Receipt-To", "[email protected]");            if (cc != null){
                    String ccList = this.getMailList(cc);
                        InternetAddress[] iaCCList = new InternetAddress().parse(ccList);
                    message.setRecipients(Message.RecipientType.CC,iaCCList);
                     }
                if (bcc != null){
                    String bccList = this.getMailList(bcc);
                        InternetAddress[] iaBCCList = new InternetAddress().parse(bccList);
                    message.setRecipients(Message.RecipientType.BCC,iaBCCList);
                     }
                //设置e-mail标题 
                message.setSubject(title);
                //设置e-mail发送时间
                message.setSentDate(new Date());
                //设置e-mail内容
                message.setText(content);
                //建立第一部分:文本正文
                messageBodyPart.setContent(content, "text/html;charset=UTF-8");//给BodyPart对象设置内容和格式/编码方式    
                multipart.addBodyPart(messageBodyPart);
                if (!attachment.equals("")){
                    // 建立第二部分:附件     
                    messageBodyPart = new MimeBodyPart();
                    // 获得附件
                    DataSource source = new FileDataSource(attachment);
                    //设置附件的数据处理器
                    messageBodyPart.setDataHandler(new DataHandler(source));
                    // 设置附件文件名
                    messageBodyPart.setFileName(attachment);
                    // 加入第二部分
                    multipart.addBodyPart(messageBodyPart);
                }
                // 将多部分内容放到e-mail中
                message.setContent(multipart);            //保存对于e-mail的修改
                message.saveChanges();
                //根据Session生成Transport对象
                transport = sendsession.getTransport("smtp");
                //连接到SMTP服务器
                transport.connect("mail.服务器地址.com", "账号", "密码");
                //发送e-mail
                transport.sendMessage(message, message.getAllRecipients());
                //关闭Transport连接
                transport.close();
            } catch (MessagingException m){
                System.out.println(m.toString());
            } catch (Exception e){
                e.printStackTrace();
            } 
        }
        
        //获取收件人地址
        public String getMailList(String[] mailArray){
             
            StringBuffer toList = new StringBuffer();
        int length = mailArray.length;
            if(mailArray!=null && length <2){
                 toList.append(mailArray[0]);
            }else{
                 for(int i=0;i<length;i++){
                         toList.append(mailArray[i]);
                         if(i!=(length-1)){
                             toList.append(",");
                         }
                 }
             }
         return toList.toString();
        }    public static void main(String args[]){
         SandMail m = new SandMail();
            //m.SendMail2("fenglingcompany", "D:\\test0622.html");
            m.SendMail2("测试", "");
        }
    }
      

  4.   

    MimeMessage的话, 使用MimeMessage的setReplyTo方法可以设置回复地址.
    然后设置property mail.smtp.from可以设置当邮件发送失败时退回的邮箱.
    你的代码中, 建议将setHead部分的代码改为放入到property中, 然后再生成MimeMessage对象.
      

  5.   

    刚刚测试一下,如果property mail.smtp.from 的地址和 发送邮件不一致就会报错:
    550 5.7.1 Client does not have permissions to send as this sender
    com.sun.mail.smtp.SMTPSendFailedException: 550 5.7.1 Client does not have permissions to send as this sender at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1333)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:906)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:535)
    at com.eve.test.SandMail.SendMail2(SandMail.java:118)
    at com.eve.test.SandMail.main(SandMail.java:149)
    com.sun.mail.smtp.SMTPSendFailedException: 550 5.7.1 Client does not have permissions to send as this sender问题研究中... 感谢各位关注
      

  6.   

    感谢lz贴出来的代码,我也要研究一下,没想到java还能干这个