Java mail 邮件回复 六

第六讲我将会讲解邮件的回复,邮件的回复的代码很简单,在迭代邮件的信息的时候调用MimeMessage reply = (MimeMessage) messages[i].reply(Boolean )  TRUE 是邮件回复给发送者,FALSE恢复给所有人。如果要回复到新的收件人请使用SetReply-to。

同时可以使用 reply.setSubject(“***”)设置回复的标题,不设置的话默认是Re: 加上接收到邮件的标题。同理,也可以自己设置正文内容。

这里一定要设置发送邮件的地址,而且必须要和邮件认证的用户名一致,不然会报错。

最后调用Transport.send(reply)将邮件发出去

注意:1、reply.setFrom() 必须要设置而且要和邮件认证的用户名一致,不然会报以下错误:

com.sun.mail.smtp.SMTPSendFailedException: 501 mail from address must be same as authorization user

2、建议使用邮件认证登陆邮件服务器,不然会出现以下错误:

javax.mail.AuthenticationFailedException: failed to connect, no password specified?

3、代码虽然很简单,但是程序难免会出现各种异常。如果大家在调试程序的时候出现错误,请给我留言。

 

 

给一个成功例子大家参考:

import java.io.*;
  import java.util.*;
 
  import javax.mail.*;
  import javax.mail.internet.*;
 
  public class ReplyMail {
 
     private static final String HOST_NAME = "pop.qq.com";
     private static final String SEND_HOST_NAME = "smtp.qq.com";
     private static final String PASSWORD = "*******";
     private static final String EMAIL_FROM = "[email protected]";
     private static final String USER_NAME = "[email protected]";
     private static final String PROTOCOL = "pop3";
     private static final String SEND_PROTOCOL = "smtp";
    
     public static void listMail() throws Exception {
         Properties props = new Properties();
         props.put("mail.smtp.host", SEND_HOST_NAME);
         props.put("mail.pop.host", HOST_NAME);
         props.put("mail.transport.protocol","smtp");
         props.put("mail.smtp.auth", "true");
 
        
         Session session = Session.getDefaultInstance(props, new SimpleAuthenticator(USER_NAME, PASSWORD));    
         Store store = session.getStore(ReplyMail.PROTOCOL);  
         store.connect(HOST_NAME, USER_NAME, PASSWORD);
         Folder folder = store.getFolder("INBOX");
         folder.open(Folder.READ_ONLY); 
         Message[] messages = folder.getMessages();  
         InternetAddress address;  
         for (int i = messages.length-1; i >= 0; i--) {  
             address = (InternetAddress)messages[i].getFrom()[0];  
             if(address != null) {  
                 System.out.println(address.getPersonal());  
             }
             if (null != address && "秋天的童话".equals(address.getPersonal())) {  
                 System.out.println("第" + i + "个:" + messages[i].getSubject());  
                 MimeMessage replyMessage = (MimeMessage) messages[i].reply(false);  
  //                replyMessage.setFrom(new InternetAddress(EMAIL_FROM));
 
                 replyMessage.setRecipients(MimeMessage.RecipientType.TO, address.getAddress());
                 replyMessage.setText("这是回复邮件,不知道能否成功!"); 
                 replyMessage.saveChanges();
                 Transport transport = session.getTransport("smtp");
                 transport.connect(SEND_HOST_NAME,  USER_NAME, PASSWORD);
                 transport.send(replyMessage);
                 System.out.println("回复成功");               
             }  
         }
         folder.close(true);  
         store.close();
     }
    
     public static void main(String[] args) {
         try {
             ReplyMail.listMail();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 
 }

 

类SimpleAuthenticator的代码:

public class SimpleAuthenticator extends Authenticator {
    
     private String username;
    
     private String password;
    
     public SimpleAuthenticator(String username, String password) {
         this.username = username;
         this.password = password;
     }
 
     public PasswordAuthentication getPasswordAuthentication() {   
         return new PasswordAuthentication(this.username, this.password);
 
     }
    
 }

 

总结:邮件回复就那么几行代码,不过在写程序的时候出现的问题接二连三,一个问题解决了,又出现另外一个问题。一直在寻找答案,在国内各大技术论坛、社区都找过了,可是没有我想要的答案,最后决定去外国google搜一下,看看有没有外国朋友遇到过这个问题,可是在google搜到的英文网站的资料都读了,问题依然没有解决,最后去查找java mail的源代码,读了这个类库的一些源代码理解了背后原理,最后将问题解决了。其实遇到问题不难解决,难就难有没有好的解决问题的思路,如果遇到问题就扔掉或者坐在一边等人家给答案,那是永远学不会东西的。经过这次的我收获很大!

 

你可能感兴趣的:(Java mail 邮件回复 六)