package a;
import java.util.ArrayList;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
public class Main {
public static ArrayList<String> check(String host, String storeType, String user, String password) {
ArrayList<String> als = new ArrayList<>();
try {
// create properties field
Properties properties = new Properties();
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", "995");
properties.put("mail.pop3.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);
// create the POP3 store object and connect with the pop server
Store store = emailSession.getStore("pop3s");
store.connect(host, user, password);
// create the folder object and open it
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
// retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
System.out.println("新闻数量:" + messages.length);
for (Message message : messages) {
String s = message.getSubject();
als.add("新闻内容:" + s);
}
// close the store and folder objects
emailFolder.close(false);
store.close();
} catch (Exception e) {
e.printStackTrace();
}
return als;
}
public static void main(String[] args) {
String host = "outlook.office365.com";// change accordingly
String mailStoreType = "pop3";
String username = "www.oracle.com@outlook.com";// change accordingly
String password = "123456";// change accordingly
ArrayList<String> als = check(host, mailStoreType, username, password);
for (String s : als) {
System.out.println(s);
}
}
}
Javamail收取邮件
于 2022-12-19 01:06:52 首次发布