admin
2021-09-24 f788607ff771a47bc60d6a86e00b3433c40f3d2c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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);
            }
        }
    }
 
}