Java - POP3 - 收信
我有需要做一個工作,就是收信,並取得信件內容做相對的事,
但我這次只研究,要怎麼最基本的收信功能,
以下這個網址是我主要參考的程式碼,我改成我需要的程式碼
https://www.javaworld.com.tw/jute/post/view?bid=7&id=60099&sty=3
package com.spider.net;
import javax.mail.*;
import javax.mail.internet.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class SecurePOP3Client {
public String host = "mail.xxxx.com.tw";
public String provider = "pop3";
public String userId = "root";
public String password = "password";
public Message[] getMessage(String host,String userId,String password) {
this.host = host;
this.userId = userId;
this.password = password;
return getMessage();
}
public Message[] getMessage(String userId,String password) {
this.userId = userId;
this.password = password;
return getMessage();
}
public Message[] getMessage() {
Properties props = new Properties();
Message[] messages = null;
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore(provider);
store.connect(host, userId, password);
Folder inbox = store.getFolder("INBOX");
if (inbox == null) {
System.out.println("No INBOX");
messages = null;
return messages;
}
inbox.open(Folder.READ_ONLY);
messages = inbox.getMessages();
inbox.close(false);
store.close();
}
catch (Exception e) {
e.printStackTrace();
}
return messages;
}
}
但Mail需要有的好像是J2EE的功能,所以J2SE有些東西沒有需要另外include javax.mail-api-1.4.6.jar,mailapi-1.6.2.jar,pop3.jar
所以要到以下地方去下載這些jar檔
https://javaee.github.io/javamail/
https://jar-download.com/maven-repository-class-search.php?search_box=com.sun.mail.util.MailLogger