[Javaは]例としてアリクラウドメールPOP3プロトコルへのメッセージを読みます

 

結果を確認するには1、

 

 

 

 

 

 

図2に示すように、コード

 

  1  インポートorg.apache.tomcat.util.http.fileupload.IOUtils。
  2  インポートorg.springframework.util.ObjectUtils。
  3  
  4  インポートのjavax.mail *。;
  5  インポートjavax.mail.internet.InternetAddress。
  6  インポートjavax.mail.internet.MimeMessageの。
  7  インポートjavax.mail.internet.MimeUtility。
  8  輸入 java.io. * ;
  9  インポートjava.text.SimpleDateFormatの。
10  インポートjava.util.ArrayListの。
11  インポートjava.util.Date。
12  インポート java.util.List;
 13 import java.util.Properties;
 14 
 15 public class ShowMail {
 16 
 17     public static String NORM_DATETIME_PATTERN = "yyyy-MM-dd hh:mm:ss";
 18     private MimeMessage mimeMessage;
 19     /**
 20      * 附件下载后的存放目录
 21      */
 22     private String saveAttachPath = "";
 23     /**
 24      * 存放邮件内容的StringBuffer对象
 25      */
 26     private StringBuffer bodyText = new StringBuffer();
 27 
 28     /**
 29      * 构造函数,初始化一个MimeMessage对象
 30      *
 31      * @param mimeMessage
 32      */
 33     public ShowMail(MimeMessage mimeMessage) {
 34         this.mimeMessage = mimeMessage;
 35     }
 36 
 37     /**
 38      * 获得发件人的地址和姓名
 39      *
 40      * @return
 41      * @throws MessagingException
 42      */
 43     public String getFrom() throws MessagingException {
 44         InternetAddress address[] = (InternetAddress[]) mimeMessage.getFrom();
 45         String from = address[0].getAddress();
 46         if (from == null) {
 47             from = "";
 48         }
 49         String personal = address[0].getPersonal();
 50 
 51         if (personal == null) {
 52             personal = "";
 53         }
 54 
 55         String fromAddr = null;
 56         if (personal != null || from != null) {
 57             fromAddr = personal + "<" + from + ">";
 58         }
 59         return fromAddr;
 60     }
 61 
 62     /**
 63      * 获得邮件的收件人,抄送,和密送的地址和姓名,根据所传递的参数的不同
 64      *
 65      * @param type "to"----收件人 "cc"---抄送人地址 "bcc"---密送人地址
 66      * @return
 67      * @throws MessagingException
 68      * @throws UnsupportedEncodingException
 69      */
 70     public String getMailAddress(String type) throws MessagingException, UnsupportedEncodingException {
 71         if (ObjectUtils.isEmpty(type)) {
 72             return "";
 73         }
 74 
 75         String addType = type.toUpperCase();
 76 
 77         if (!addType.equals("TO") && !addType.equals("CC") && !addType.equals("BCC")) {
 78             return "";
 79         }
 80         InternetAddress[] address;
 81 
 82         if (addType.equals("TO")) {
 83             address = (InternetAddress[]) mimeMessage.getRecipients(Message.RecipientType.TO);
 84         } else if (addType.equals("CC")) {
 85             address = (InternetAddress[]) mimeMessage.getRecipients(Message.RecipientType.CC);
 86         } else {
 87             address = (InternetAddress[]) mimeMessage.getRecipients(Message.RecipientType.BCC);
 88         }
 89 
 90         if (ObjectUtils.isEmpty(address)) {
 91             return "";
 92         }
 93         StringBuilder mailAddr = new StringBuilder();
 94         String emailAddr;
 95         String personal;
 96         for (int i = 0; i < address.length; i++) {
 97             emailAddr = address[i].getAddress();
 98             if (emailAddr == null) {
 99                 emailAddr = "";
100             } else {
101                 emailAddr = MimeUtility.decodeText(emailAddr);
102             }
103             personal = address[i].getPersonal();
104             if (personal == null) {
105                 personal = "";
106             } else {
107                 personal = MimeUtility.decodeText(personal);
108             }
109             mailAddr.append(",").append(personal).append("<").append(emailAddr).append(">");
110         }
111         return mailAddr.toString().substring(1);
112     }
113 
114     /**
115      * 获得邮件主题
116      *
117      * @return
118      * @throws MessagingException
119      * @throws UnsupportedEncodingException
120      */
121     public String getSubject() throws MessagingException, UnsupportedEncodingException {
122         String subject = MimeUtility.decodeText(mimeMessage.getSubject());
123         if (subject == null) {
124             subject = "";
125         }
126         return subject;
127     }
128 
129     /**
130      * 获得邮件发送日期
131      *
132      * @return
133      * @throws MessagingException
134      */
135     public String getSentDate() throws MessagingException {
136         Date sentDate = mimeMessage.getSentDate();
137         SimpleDateFormat format = new SimpleDateFormat(NORM_DATETIME_PATTERN);
138         return format.format(sentDate);
139     }
140 
141     /**
142      * 获得邮件正文内容
143      *
144      * @return
145      */
146     public String getBodyText() {
147         return bodyText.toString();
148     }
149 
150     /**
151      * 解析邮件,把得到的邮件内容保存到一个StringBuffer对象中,解析邮件
152      * 主要是根据MimeType类型的不同执行不同的操作,一步一步的解析
153      * @param part
154      * @throws MessagingException
155      * @throws IOException
156      */
157     public void getMailContent(Part part) throws MessagingException, IOException {
158 
159         String contentType = part.getContentType();
160 
161         int nameIndex = contentType.indexOf("name");
162 
163         boolean conName = false;
164 
165         if (nameIndex != -1) {
166             conName = true;
167         }
168 
169         if (part.isMimeType("text/plain") && conName == false) {
170             bodyText.append((String) part.getContent());
171         } else if (part.isMimeType("text/html") && conName == false) {
172             bodyText.append((String) part.getContent());
173         } else if (part.isMimeType("multipart/*")) {
174             Multipart multipart = (Multipart) part.getContent();
175             int counts = multipart.getCount();
176             for (int i = 0; i < counts; i++) {
177                 this.getMailContent(multipart.getBodyPart(i));
178             }
179         } else if (part.isMimeType("message/rfc822")) {
180             this.getMailContent((Part) part.getContent());
181         }
182     }
183 
184     /**
185      * 判断此邮件是否需要回执,如果需要回执返回"true",否则返回"false"
186      *
187      * @return
188      * @throws MessagingException
189      */
190     public boolean getReplySign() throws MessagingException {
191 
192         boolean replySign = false;
193 
194         String needReply[] = mimeMessage.getHeader("Disposition-Notification-To");
195 
196         if (needReply != null) {
197             replySign = true;
198         }
199         return replySign;
200     }
201 
202     /**
203      * 判断此邮件是否已读,如果未读返回false,反之返回true
204      *
205      * @return
206      * @throws MessagingException
207      */
208     public boolean isNew() throws MessagingException {
209         boolean isNew = false;
210         Flags flags = mimeMessage.getFlags();
211         Flags.Flag[] flag = flags.getSystemFlags();
212         for (int i = 0; i < flag.length; i++) {
213             if (flag[i] == Flags.Flag.SEEN) {
214                 isNew = true;
215             }
216         }
217         return isNew;
218     }
219 
220     /**
221      * 判断此邮件是否包含附件
222      *
223      * @param part
224      * @return
225      * @throws MessagingException
226      * @throws IOException
227      */
228     public boolean isContainAttach(Part part) throws MessagingException, IOException {
229         boolean attachFlag = false;
230         if (part.isMimeType("multipart/*")) {
231             Multipart mp = (Multipart) part.getContent();
232             BodyPart mPart;
233             String conType;
234             for (int i = 0; i < mp.getCount(); i++) {
235                 mPart = mp.getBodyPart(i);
236                 String disposition = mPart.getDisposition();
237                 if (Part.ATTACHMENT.equals(disposition) || Part.INLINE.equals(disposition)) {
238                     attachFlag = true;
239                 } else if (mPart.isMimeType("multipart/*")) {
240                     attachFlag = this.isContainAttach(mPart);
241                 } else {
242                     conType = mPart.getContentType();
243                     if (conType.toLowerCase().indexOf("application") != -1 || conType.toLowerCase().indexOf("name") != -1){
244                         attachFlag = true;
245                     }
246                 }
247             }
248         } else if (part.isMimeType("message/rfc822")) {
249             attachFlag = isContainAttach((Part) part.getContent());
250         }
251         return attachFlag;
252     }
253 
254     /**
255      * 保存附件
256      *
257      * @param part
258      * @throws MessagingException
259      * @throws IOException
260      */
261     public void saveAttachMent(Part part) throws MessagingException, IOException {
262         String fileName;
263         if (part.isMimeType("multipart/*")) {
264             Multipart mp = (Multipart) part.getContent();
265             BodyPart mPart;
266             for (int i = 0; i < mp.getCount(); i++) {
267                 mPart = mp.getBodyPart(i);
268                 String disposition = mPart.getDisposition();
269                 if (Part.ATTACHMENT.equals(disposition) || Part.INLINE.equals(disposition)) {
270                     fileName = mPart.getFileName();
271                     if (null != fileName && fileName.toLowerCase().indexOf("gb2312") != -1) {
272                         fileName = MimeUtility.decodeText(fileName);
273                     }
274                     this.saveFile(fileName, mPart.getInputStream());
275                 } else if (mPart.isMimeType("multipart/*")) {
276                     this.saveAttachMent(mPart);
277                 } else {
278                     fileName = mPart.getFileName();
279                     if ((fileName != null) && (fileName.toLowerCase().indexOf("GB2312") != -1)) {
280                         fileName = MimeUtility.decodeText(fileName);
281                         this.saveFile(fileName, mPart.getInputStream());
282                     }
283                 }
284             }
285         } else if (part.isMimeType("message/rfc822")) {
286             this.saveAttachMent((Part) part.getContent());
287         }
288     }
289 
290     /**
291      * 设置附件存放路径
292      *
293      * @param attachPath
294      */
295     public void setAttachPath(String attachPath) {
296         this.saveAttachPath = attachPath;
297     }
298 
299     /**
300      * 获得附件存放路径
301      *
302      * @return
303      */
304     public String getAttachPath() {
305         return saveAttachPath;
306     }
307 
308     /**
309      * 真正的保存附件到指定目录里
310      *
311      * @param fileName
312      * @param in
313      * @throws IOException
314      */
315     private void saveFile(String fileName, InputStream in) throws IOException {
316         String osName = System.getProperty("os.name");
317         String storeDir = this.getAttachPath();
318         if (null == osName) {
319             osName = "";
320         }
321         if (osName.toLowerCase().indexOf("win") != -1) {
322             if (ObjectUtils.isEmpty(storeDir))
323                 storeDir = "C:\\tmp";
324         } else {
325             storeDir = "/tmp";
326         }
327         FileOutputStream fos = new FileOutputStream(new File(storeDir + File.separator + fileName));
328         IOUtils.copy(in, fos);
329         IOUtils.closeQuietly(fos);
330         IOUtils.closeQuietly(in);
331     }
332 
333     /**
334      *  获取邮箱信息
335      *
336      * @param host 邮件服务器
337      * @param username 邮箱名
338      * @param password 密码
339      * @param protocol 协议
340      * @param fromMail 只读取该邮箱发来的邮件
341      * @param startDate 只读取该日期以后的邮件
342      * @return
343      * @throws MessagingException
344      */
345     public static List<Message> getMailMessage(String host, String username, String password, String protocol, String fromMail, String startDate) throws MessagingException {
346         Properties props = new Properties();
347         Session session = Session.getDefaultInstance(props, null);
348         fromMail = "<" + fromMail + ">";
349 
350         Store store = session.getStore(protocol);
351         store.connect(host, username, password);
352 
353         Folder folder = store.getFolder("INBOX");
354         folder.open(Folder.READ_ONLY);
355         Message messages[] = folder.getMessages();
356 
357         List<Message> messageList = new ArrayList<>();
358         for (Message message: messages) {
359             String from = (message.getFrom()[0]).toString();
360             if(null != from && from.contains(fromMail) && DateUtils.strToDate(startDate).getTime() < message.getSentDate().getTime()) {
361                 messageList.add(message);
362             }
363         }
364         return messageList;
365     }
366 
367     public static void main(String[] args) throws MessagingException, IOException {
368         String host = "pop3.mxhichina.com";
369         String username = "[email protected]";
370         String password = "密码";
371         String protocol = "pop3";
372         String fromMail = "[email protected]";
373         String startDate = "2020-2-24 23:35:40";
374         List<Message> messageList = getMailMessage(host, username, password, protocol, fromMail, startDate);
375         System.out.println("邮件数量:" + messageList.size());
376         ShowMail re;
377         Message message;
378         for (int i = 0, leng = messageList.size(); i < leng; i++) {
379             message = messageList.get(i);
380             re = new ShowMail((MimeMessage) message);
381             System.out.println("邮件【" + i + "】主题:" + re.getSubject());
382             System.out.println("邮件【" + i + "】发送时间:" + re.getSentDate());
383             System.out.println("邮件【" + i + "】是否需要回复:" + re.getReplySign());
384             System.out.println("邮件【" + i + "】是否已读:" + re.isNew());
385             System.out.println("邮件【" + i + "】是否包含附件:" + re.isContainAttach( message));
386             System.out.println("邮件【" + i + "】发送人地址:" + re.getFrom());
387             System.out.println("邮件【" + i + "】收信人地址:" + re.getMailAddress("to"));
388             System.out.println("邮件【" + i + "】抄送:" + re.getMailAddress("cc"));
389             System.out.println("邮件【" + i + "】暗抄:" + re.getMailAddress("bcc"));
390             System.out.println("邮件【" + i + "】发送时间:" + re.getSentDate());
391             System.out.println("邮件【" + i + "】邮件ID:" + ((MimeMessage) message).getMessageID());
392             re.getMailContent(message);
393             System.out.println("邮件【" + i + "】正文内容:\r\n" + re.getBodyText());
394             re.setAttachPath("D:\\Download\\mailFile\\");
395             re.saveAttachMent(message);
396         }
397     }
398 
399 }

 

 

参考文章:https://blog.csdn.net/qq_34173549/article/details/79661997

おすすめ

転載: www.cnblogs.com/xiaostudy/p/12363368.html