Javamail收取邮件

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 check(String host, String storeType, String user, String password) {
		ArrayList 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 = "[email protected]";// change accordingly
		String password = "123456";// change accordingly

		ArrayList als = check(host, mailStoreType, username, password);
		for (String s : als) {
			System.out.println(s);
		}

	}

}

你可能感兴趣的:(java,jvm,数据库)