[Java] JavaMail 查询邮件

上两篇提到如何发送邮件:JavaMail 发送 html 格式、带附件的邮件JavaMail 简单案例。本文再演示下,如何读取邮箱上的邮件。

邮件内容的数据格式种类比较多,用户直接阅读邮件比较常见,通过程序解读邮件比较少见,所以,本文的例子只演示读取邮件信封上的内容,例如邮件主题、发件人。若邮件内容是纯文本格式,也能直接读取。若邮件内容是非文本格式,可以通过分解获取到的 Message 继续解读,不过这种情况不在本文演示范围内。

 

CheckingMail, 查询邮件,根据提供的 pop3 服务器域名,邮箱账号,密码,查询该邮箱账号里面的邮件。只查看邮件主题,发件人,邮件内容类型,如果是纯文本内容,也会显示出现。注意点:如果 InternetAddress 的 personName 包含中文,直接调用 InternetAddress.toString() 会显示不正常,需要单独调用 InternetAddress 的 getPersonal() 提取发件人姓名,才能正常显示发件人姓名。

CheckingMailDemo, 演示邮件查询。

具体代码:

CheckingMail, 查询邮件。

import static java.lang.System.out;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;

public class CheckingMail {
    
    private String username;
    private String password;
    private String host;
    private String port = "995";
    
    public CheckingMail(String host, String username, String password){
        this.host = host;
        this.username = username;
        this.password = password;
    }

    public void check() throws Exception{
        
        Properties props = new Properties();        
        props.put("mail.pop3.host", host);
        props.put("mail.pop3.port", port);
        props.put("mail.pop3.stattls.enable", "true");
        
        Session session = Session.getInstance(props, 
                new javax.mail.Authenticator(){
             protected PasswordAuthentication getPasswordAuthentication() {
                   return new PasswordAuthentication(username, password);
           }
        });    
        
        Store store = session.getStore("pop3s");
        store.connect(host, username, password);
        
        System.out.println(" connect completed ");
                
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        
        Message[] messages = folder.getMessages();
        
        System.out.println(" the number of message retrieved from mailbox : " + messages.length);
        
        for (int i = 0 ; i < messages.length; i++){
            Message msg = messages[i];
            InternetAddress address =  (InternetAddress) msg.getFrom()[0];
            out.println(" [ #" + i + " ] ");
            out.println( "Subject : " + msg.getSubject());
            out.println("From : " + address.getPersonal() + "<" + address.getAddress() + ">");
            out.println("ContentType : " + msg.getContentType());
            out.println("Content Detail : " + msg.getContent().toString());
            out.println();
        }
        
        out.println(" Eamil retrieved completely ");
        
        folder.close(false);
        store.close();
    }
}

CheckingMailDemo, 演示邮件查询。

public class CheckingMailDemo {

    public static void main() {
        
        try {
            String host = "pop.163.com";

            final String username = "yourUserName@163.com";
            final String password = "yourPassword";

            CheckingMail checkingMail = new CheckingMail(host, username, password);
            checkingMail.check();
            
        } catch (Exception e) {
            e.printStackTrace();
        }    
    }
}

 

参考资料

 

posted @ 2016-06-18 11:45  TonyYPZhang  阅读(2414)  评论(0编辑  收藏  举报