Javamail 邮件回复

邮件的回复的代码很简单,在迭代邮件的信息的时候调用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);
 
     }
     
 }

你可能感兴趣的:(MimeMessage,setreply,reply)