JavaMail详解(六)——邮件回复 - 高飞网
105 人阅读

JavaMail详解(六)——邮件回复

2016-11-22 18:27:37

    本节将学习如何使用JavaMail API回复邮件。基本的步骤包括:

  1. 获取带有POP和SMTP服务细节属性的会话Session对象。我们将用到POP服务查询邮件,并使用SMTP服务发送邮件。
  2. 创建POP3 Store对象并连接。
  3. 创建Folder对象并打开邮箱中相应的文件夹
  4. 查询消息
  5. 遍历消息并将想要回复的邮件设置上Y或y
  6. 获取消息的所有信息(如To,From,Subject,Content)
  7. 构造回复消息,使用Meesage.reply()方法。该方法将通过正确的收件人和标题配置一个新的Message对象。可以通过一个boolean参数来指定是只发送给发送者(false)还是回复给所有人(true)。
  8. 设置发送者,发送内容和Reply-to消息头,并用Transport实例对象发送邮件。
  9. 关闭Transport,folder和store对象。


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class ReplyToEmail {
    public static void main(String args[]) {
        Date date = null;

        Properties properties = new Properties();
        properties.put("mail.store.protocol", "pop3");
        properties.put("mail.pop3s.host", "pop.163.com");
        properties.put("mail.pop3s.port", "995");
        properties.put("mail.pop3.starttls.enable", "true");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.163.com");
        properties.put("mail.smtp.port", "25");
        Session session = Session.getDefaultInstance(properties);

        // session.setDebug(true);
        Store store = null;
        Folder folder = null;

        try {
            // 获取Store对象并连接到指定的主机
            store = session.getStore("pop3s");
            store.connect("pop.163.com", "xxxxxx@163.com", "xxxxxx");

            folder = store.getFolder("inbox");
            if (!folder.exists()) {
                System.out.println("inbox not found");
                System.exit(0);
            }
            folder.open(Folder.READ_ONLY);

            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

            Message[] messages = folder.getMessages();
            if (messages.length != 0) {

                for (int i = 0, n = messages.length; i < n; i++) {
                    Message message = messages[i];
                    date = message.getSentDate();
                    // 从消息中获取所有的信息
                    String from = InternetAddress.toString(message.getFrom());
                    if (from != null) {
                        System.out.println("From: " + from);
                    }
                    String replyTo = InternetAddress.toString(message.getReplyTo());
                    if (replyTo != null) {
                        System.out.println("Reply-to: " + replyTo);
                    }
                    String to = InternetAddress.toString(message.getRecipients(Message.RecipientType.TO));
                    if (to != null) {
                        System.out.println("To: " + to);
                    }

                    String subject = message.getSubject();
                    if (subject != null) {
                        System.out.println("Subject: " + subject);
                    }
                    Date sent = message.getSentDate();
                    if (sent != null) {
                        System.out.println("Sent: " + sent);
                    }

                    System.out.print("Do you want to reply [y/n] : ");
                    String ans = reader.readLine();
                    if ("Y".equals(ans) || "y".equals(ans)) {

                        Message replyMessage = new MimeMessage(session);
                        replyMessage = (MimeMessage) message.reply(false);
                        replyMessage.setFrom(new InternetAddress(to));
                        replyMessage.setText("Thanks");
                        replyMessage.setReplyTo(message.getReplyTo());

                        // 通过验证SMTP服务发送消息
                        // 创建一个Transport实例并调用发送消息方法
                        Transport t = session.getTransport("smtp");
                        try {
                            // 使用transport实例连接到smpt服务
                            t.connect("xxxxxx@163.com", "xxxxxx");
                            t.sendMessage(replyMessage, replyMessage.getAllRecipients());
                        } finally {
                            t.close();
                        }
                        System.out.println("message replied successfully ....");

                    } else if ("n".equals(ans)) {
                        break;
                    }
                }

            } else {
                System.out.println("There is no msg....");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (folder != null && store != null) {
                // 关闭sotre和folder对象
                try {
                    folder.close(false);
                    store.close();
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

回复的邮件:


还没有评论!
54.197.104.221