第六讲我将会讲解邮件的回复,邮件的回复的代码很简单,在迭代邮件的信息的时候调用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、代码虽然很简单,但是程序难免会出现各种异常。如果大家在调试程序的时候出现错误,请给我留言。
给一个成功例子大家参考:
1 import java.io.*;
2 import java.util.*;
3
4 import javax.mail.*;
5 import javax.mail.internet.*;
6
7 public class ReplyMail {
8
9 private static final String HOST_NAME = "pop.qq.com";
10 private static final String SEND_HOST_NAME = "smtp.qq.com";
11 private static final String PASSWORD = "*******";
12 private static final String EMAIL_FROM = "
[email protected]";
13 private static final String USER_NAME = "
[email protected]";
14 private static final String PROTOCOL = "pop3";
15 private static final String SEND_PROTOCOL = "smtp";
16
17 public static void listMail() throws Exception {
18 Properties props = new Properties();
19 props.put("mail.smtp.host", SEND_HOST_NAME);
20 props.put("mail.pop.host", HOST_NAME);
21 props.put("mail.transport.protocol","smtp");
22 props.put("mail.smtp.auth", "true");
23
24
25 Session session = Session.getDefaultInstance(props, new SimpleAuthenticator(USER_NAME, PASSWORD));
26 Store store = session.getStore(ReplyMail.PROTOCOL);
27 store.connect(HOST_NAME, USER_NAME, PASSWORD);
28 Folder folder = store.getFolder("INBOX");
29 folder.open(Folder.READ_ONLY);
30 Message[] messages = folder.getMessages();
31 InternetAddress address;
32 for (int i = messages.length-1; i >= 0; i--) {
33 address = (InternetAddress)messages[i].getFrom()[0];
34 if(address != null) {
35 System.out.println(address.getPersonal());
36 }
37 if (null != address && "秋天的童话".equals(address.getPersonal())) {
38 System.out.println("第" + i + "个:" + messages[i].getSubject());
39 MimeMessage replyMessage = (MimeMessage) messages[i].reply(false);
40 // replyMessage.setFrom(new InternetAddress(EMAIL_FROM));
41
42 replyMessage.setRecipients(MimeMessage.RecipientType.TO, address.getAddress());
43 replyMessage.setText("这是回复邮件,不知道能否成功!");
44 replyMessage.saveChanges();
45 Transport transport = session.getTransport("smtp");
46 transport.connect(SEND_HOST_NAME, USER_NAME, PASSWORD);
47 transport.send(replyMessage);
48 System.out.println("回复成功");
49 }
50 }
51 folder.close(true);
52 store.close();
53 }
54
55 public static void main(String[] args) {
56 try {
57 ReplyMail.listMail();
58 } catch (Exception e) {
59 e.printStackTrace();
60 }
61 }
62
63 }
64
类SimpleAuthenticator的代码:
1 public class SimpleAuthenticator extends Authenticator {
2
3 private String username;
4
5 private String password;
6
7 public SimpleAuthenticator(String username, String password) {
8 this.username = username;
9 this.password = password;
10 }
11
12 public PasswordAuthentication getPasswordAuthentication() {
13 return new PasswordAuthentication(this.username, this.password);
14
15 }
16
17 }
总结:邮件回复就那么几行代码,不过在写程序的时候出现的问题接二连三,一个问题解决了,又出现另外一个问题。一直在寻找答案,在国内各大技术论坛、社区都找过了,可是没有我想要的答案,最后决定去外国google搜一下,看看有没有外国朋友遇到过这个问题,可是在google搜到的英文网站的资料都读了,问题依然没有解决,最后去查找java mail的源代码,读了这个类库的一些源代码理解了背后原理,最后将问题解决了。其实遇到问题不难解决,难就难有没有好的解决问题的思路,如果遇到问题就扔掉或者坐在一边等人家给答案,那是永远学不会东西的。经过这次的我收获很大!