package com.yeshi.buwan.util.email;
|
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
import java.util.Properties;
|
|
import javax.mail.Address;
|
import javax.mail.BodyPart;
|
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.MimeUtility;
|
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Element;
|
|
import com.yeshi.buwan.util.LogUtil;
|
|
|
/**
|
* 邮箱接收解析
|
*
|
* @author Administrator
|
*
|
*/
|
public class EmailParser {
|
public static String receive(String account, String pwd) throws Exception {
|
// 准备连接服务器的会话信息
|
Properties props = new Properties();
|
props.setProperty("mail.store.protocol", "pop3"); // 协议
|
props.setProperty("mail.pop3.port", "110"); // 端口
|
props.setProperty("mail.pop3.host", "pop3." + account.split("@")[1]); // pop3服务器
|
|
// 创建Session实例对象
|
Session session = Session.getInstance(props);
|
Store store = session.getStore("pop3");
|
store.connect(account, pwd);
|
|
// 获得收件箱
|
Folder folder = store.getFolder("INBOX");
|
/*
|
* Folder.READ_ONLY:只读权限 Folder.READ_WRITE:可读可写(可以修改邮件的状态)
|
*/
|
folder.open(Folder.READ_WRITE); // 打开收件箱
|
// 得到收件箱中的所有邮件,并解析
|
Message[] messages = folder.getMessages();
|
String leshi = parseMessage(messages);
|
|
// 释放资源
|
folder.close(true);
|
store.close();
|
return leshi;
|
}
|
|
/**
|
* 解析邮件
|
*
|
* @param messages
|
* 要解析的邮件列表
|
*/
|
public static String parseMessage(Message... messages)
|
throws MessagingException, IOException {
|
if (messages == null || messages.length < 1)
|
throw new MessagingException("未找到要解析的邮件!");
|
|
// 解析所有邮件
|
for (int i = messages.length - 1; i >= 0; i--) {
|
MimeMessage msg = (MimeMessage) messages[i];
|
|
StringBuffer content = new StringBuffer(1000);
|
getMailTextContent(msg, content);
|
if (getFrom(msg).contains("乐视网")) {
|
Document doc = Jsoup.parse(content.toString());
|
Element el = doc.getElementsByTag("a").get(0);
|
LogUtil.i("乐视连接:" + el.attr("href"));
|
return el.attr("href");
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 获取发件人
|
*
|
* @param msg
|
* @return
|
* @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;
|
}
|
|
/**
|
* 获取邮件内容
|
*
|
* @param part
|
* @param content
|
* @throws MessagingException
|
* @throws IOException
|
*/
|
public static void getMailTextContent(Part part, StringBuffer content)
|
throws MessagingException, IOException {
|
// 如果是文本类型的附件,通过getContent方法可以取到文本内容,但这不是我们需要的结果,所以在这里要做判断
|
boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;
|
if (part.isMimeType("text/*") && !isContainTextAttach) {
|
content.append(part.getContent().toString());
|
} else if (part.isMimeType("message/rfc822")) {
|
getMailTextContent((Part) part.getContent(), content);
|
} else if (part.isMimeType("multipart/*")) {
|
Multipart multipart = (Multipart) part.getContent();
|
int partCount = multipart.getCount();
|
for (int i = 0; i < partCount; i++) {
|
BodyPart bodyPart = multipart.getBodyPart(i);
|
getMailTextContent(bodyPart, content);
|
}
|
}
|
}
|
|
}
|