需要下载3个jar包:mail.jar/ activation.jar/ additionnal.jar
1.pop3
[html]
/**
* 以pop3方式读取邮件,此方法不能读取邮件是否为已读,已经通过测试
* */
private void getEmail() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
URLName urln = new URLName("pop3", "pop3.163.com", 110, null,
"邮箱名(没有@163.com)", "密码");
// 邮件协议为pop3,邮件服务器是pop3.163.com,端口为110,用户名/密码为abcw111222/123456w
Store store = session.getStore(urln);
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Message message[] = folder.getMessages();
if (message.length > 0) {
Map<String, Object> map;
System.out.println("Messages's length: " + message.length);
ReciveOneMail pmm = null;
for (int i = 0; i < message.length; i++) {
System.out.println("======================");
pmm = new ReciveOneMail((MimeMessage) message[i]);
System.out.println("Message " + i + " subject: "
+ pmm.getSubject());
System.out.println("Message " + i + " sentdate: "
+ pmm.getSentDate());
System.out.println("Message " + i + " replysign: "
+ pmm.getReplySign());
boolean isRead = pmm.isNew();// 判断邮件是否为已读
System.out.println("Message " + i + " hasRead: " + isRead);
System.out.println("Message " + i + " containAttachment: "
+ pmm.isContainAttach((Part) message[i]));
System.out.println("Message " + i + " form: "
+ pmm.getFrom());
System.out.println("Message " + i + " to: "
+ pmm.getMailAddress("to"));
System.out.println("Message " + i + " cc: "
+ pmm.getMailAddress("cc"));
System.out.println("Message " + i + " bcc: "
+ pmm.getMailAddress("bcc"));
pmm.setDateFormat("yy年MM月dd日 HH:mm");
System.out.println("Message " + i + " sentdate: "
+ pmm.getSentDate());
System.out.println("Message " + i + " Message-ID: "
+ pmm.getMessageId());
// 获得邮件内容===============
pmm.getMailContent((Part) message[i]);
System.out.println("Message " + i + " bodycontent: \r\n"
+ pmm.getBodyText());
String file_path = File.separator + "mnt" + File.separator
+ "sdcard" + File.separator;
System.out.println(file_path);
pmm.setAttachPath(file_path);
pmm.saveAttachMent((Part) message[i]);
map = new HashMap<String, Object>();
map.put("from", pmm.getFrom());
map.put("title", pmm.getSubject());
map.put("time", pmm.getSentDate());
map.put("read", isRead);
list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,
list, R.layout.item_receiveemail, new String[] {
"from", "title", "time", "read" }, new int[] {