Java 實現郵件的接收

追風sky霧發表於2016-08-12

Java實現接收指定郵件地址的郵件,程式碼如下:

package com.mailSend;

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author
 * @describe
 * @date 2016/8/12
 */
public class RecieveMailUtil {

    private MimeMessage message;
    private Session session;
    private Folder folder;
    private Store store;
    private String mailHost="";
    private int mailPort;
    private String username="";
    private String password="";
    private String mail_auth = "";


    private Properties properties = new Properties();
    /*
     * 初始化方法
     */
    public RecieveMailUtil(boolean debug) {
        InputStream in = SendMailUtil.class.getClassLoader().getResourceAsStream("config.properties");
        try {
            properties.load(in);
            this.mailHost = properties.getProperty("mail.pop3.host");
            this.mailPort = Integer.parseInt(properties.getProperty("mail.pop3.port"));
            this.username = properties.getProperty("mail.username");
            this.password = properties.getProperty("mail.password");
            this.mail_auth = properties.getProperty("mail.auth");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Authenticator auth = new MyAuthenticator(username, password);
        session = Session.getDefaultInstance(properties, auth);
        session.setDebug(debug);//開啟後有除錯資訊
    }

    /**
     * 收取郵件
     */
    public void recieveEmail() {
        try {
            // Get the store
            store = session.getStore("pop3");
            store.connect(mailHost, username, password);

            /* Get folder */
            folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);

            // Get directory
            Message message[] = folder.getMessages();

            for (int i=0, n=message.length; i<n; i++) {
                System.out.println(i + ": " + message[i].getSentDate()
                        + "\t" + message[i].getSubject());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (folder!=null){
                try {
                    folder.close(false);
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
            if(store!=null){
                try {
                    // Close connection
                    store.close();
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public static void main(String[] args) {
        RecieveMailUtil se = new RecieveMailUtil(false);
        se.recieveEmail();
    }
}

依賴的倆檔案:

mail.pop3.host=pop3.163.com
mail.pop3.port=110
mail.username=xxx@163.com
mail.password=xxxx
mail.auth = true

類檔案

class MyAuthenticator  extends Authenticator {
    private String strUser;
    private String strPwd;
    public MyAuthenticator(String user, String password) {
        this.strUser = user;
        this.strPwd = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(strUser, strPwd);
    }
}

目前是將收件箱中的所有郵件標題以及時間列印出來!

相關文章