package com.test.mail;
import javax.mail.Store;
import javax.mail.FetchProfile;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
public class Semail {
public static void main(String[] args) {
try {
Properties props = new Properties();
Session s = Session.getInstance(props);
Store store = s.getStore("pop3");
store.connect("pop.126.com", "qq155", "07758258");
Folder folder = store.getFolder("Inbox");
folder.open(Folder.READ_WRITE);
FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
Message arraymessage[] = folder.getMessages();
folder.fetch(arraymessage, profile);
System.out.println("收件箱的邮件数:" + arraymessage.length);
for (int i = 0; i < arraymessage.length; i++) {
//邮件发送者
String from = arraymessage[i].getFrom()[0].toString();
InternetAddress ia = new InternetAddress(from);
System.out.println("FROM:" + ia.getPersonal() + '(' +
ia.getAddress() + ')');
//邮件标题
System.out.print(" TITLE:" + arraymessage[i].getSubject());
//邮件大小
System.out.print(" SIZE:" + arraymessage[i].getSize());
//邮件发送时间
System.out.print(" DATE:" + arraymessage[i].getSentDate());
}
folder.close(false);
store.close();
} catch (Exception ee) {
ee.printStackTrace();
}
}
}