JavaMail学习笔记使用POP3协议接收并解析电子邮件(全)
Posted 幸福过饱和
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaMail学习笔记使用POP3协议接收并解析电子邮件(全)相关的知识,希望对你有一定的参考价值。
[java] view plain copy
- package org.yangxin.study.jm;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Properties;
- import javax.mail.Address;
- import javax.mail.BodyPart;
- import javax.mail.Flags;
- import javax.mail.Folder;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.Part;
- import javax.mail.Session;
- import javax.mail.Store;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import javax.mail.internet.MimeUtility;
- /**
- * 使用POP3协议接收邮件
- */
- public class POP3ReceiveMailTest
- public static void main(String[] args) throws Exception
- receive();
- /**
- * 接收邮件
- */
- public static void receive() throws Exception
- // 准备连接服务器的会话信息
- Properties props = new Properties();
- props.setProperty("mail.store.protocol", "pop3"); // 协议
- props.setProperty("mail.pop3.port", "110"); // 端口
- props.setProperty("mail.pop3.host", "pop3.163.com"); // pop3服务器
- // 创建Session实例对象
- Session session = Session.getInstance(props);
- Store store = session.getStore("pop3");
- store.connect("xyang0917@163.com", "123456abc");
- // 获得收件箱
- Folder folder = store.getFolder("INBOX");
- /* Folder.READ_ONLY:只读权限
- * Folder.READ_WRITE:可读可写(可以修改邮件的状态)
- */
- folder.open(Folder.READ_WRITE); //打开收件箱
- // 由于POP3协议无法获知邮件的状态,所以getUnreadMessageCount得到的是收件箱的邮件总数
- System.out.println("未读邮件数: " + folder.getUnreadMessageCount());
- // 由于POP3协议无法获知邮件的状态,所以下面得到的结果始终都是为0
- System.out.println("删除邮件数: " + folder.getDeletedMessageCount());
- System.out.println("新邮件: " + folder.getNewMessageCount());
- // 获得收件箱中的邮件总数
- System.out.println("邮件总数: " + folder.getMessageCount());
- // 得到收件箱中的所有邮件,并解析
- Message[] messages = folder.getMessages();
- parseMessage(messages);
- //释放资源
- folder.close(true);
- store.close();
- /**
- * 解析邮件
- * @param messages 要解析的邮件列表
- */
- public static void parseMessage(Message ...messages) throws MessagingException, IOException
- if (messages == null || messages.length < 1)
- throw new MessagingException("未找到要解析的邮件!");
- // 解析所有邮件
- for (int i = 0, count = messages.length; i < count; i++)
- MimeMessage msg = (MimeMessage) messages[i];
- System.out.println("------------------解析第" + msg.getMessageNumber() + "封邮件-------------------- ");
- System.out.println("主题: " + getSubject(msg));
- System.out.println("发件人: " + getFrom(msg));
- System.out.println("收件人:" + getReceiveAddress(msg, null));
- System.out.println("发送时间:" + getSentDate(msg, null));
- System.out.println("是否已读:" + isSeen(msg));
- System.out.println("邮件优先级:" + getPriority(msg));
- System.out.println("是否需要回执:" + isReplySign(msg));
- System.out.println("邮件大小:" + msg.getSize() * 1024 + "kb");
- boolean isContainerAttachment = isContainAttachment(msg);
- System.out.println("是否包含附件:" + isContainerAttachment);
- if (isContainerAttachment)
- saveAttachment(msg, "c:\\\\mailtmp\\\\"+msg.getSubject() + "_"); //保存附件
- StringBuffer content = new StringBuffer(30);
- getMailTextContent(msg, content);
- System.out.println("邮件正文:" + (content.length() > 100 ? content.substring(0,100) + "..." : content));
- System.out.println("------------------第" + msg.getMessageNumber() + "封邮件解析结束-------------------- ");
- System.out.println();
- /**
- * 获得邮件主题
- * @param msg 邮件内容
- * @return 解码后的邮件主题
- */
- public static String getSubject(MimeMessage msg) throws UnsupportedEncodingException, MessagingException
- return MimeUtility.decodeText(msg.getSubject());
- /**
- * 获得邮件发件人
- * @param msg 邮件内容
- * @return 姓名 <Email地址>
- * @throws MessagingException
- * @throws UnsupportedEncodingException
- */
- public static String getFrom(MimeMessage msg) throws MessagingException, UnsupportedEncodingException
- String from = "";
- Address[] froms = msg.getFrom();
- if (froms.length < 1)
- throw new MessagingException("没有发件人!");
- InternetAddress address = (InternetAddress) froms[0];
- String person = address.getPersonal();
- if (person != null)
- person = MimeUtility.decodeText(person) + " ";
- else
- person = "";
- from = person + "<" + address.getAddress() + ">";
- return from;
- Javamail