From 27c15d8c1fbf37683fcd0c869aa56f2a09957b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B7=AF=E6=98=8E=E6=85=A7?= <1191093413@qq.com> Date: Tue, 19 Sep 2023 11:27:30 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E7=AE=B1=E8=A7=A3=E6=9E=90=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dxhy/core/task/SnEmailGatherTask.java | 52 ++++++++++++++++--- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/dxhy-core/src/main/java/com/dxhy/core/task/SnEmailGatherTask.java b/dxhy-core/src/main/java/com/dxhy/core/task/SnEmailGatherTask.java index 387a437f..a95f8b33 100644 --- a/dxhy-core/src/main/java/com/dxhy/core/task/SnEmailGatherTask.java +++ b/dxhy-core/src/main/java/com/dxhy/core/task/SnEmailGatherTask.java @@ -29,11 +29,14 @@ import com.sun.mail.pop3.POP3Folder; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import javax.mail.*; +import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeUtility; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.NoSuchAlgorithmException; @@ -114,9 +117,6 @@ public class SnEmailGatherTask { try { store = mailAuth(emailName, password); if(store == null){ - MailGatherLogVo gatherLogVo = new MailGatherLogVo(); - gatherLogVo.setFromAddress(emailName); - buildMsg(gatherLogVo,"1","邮箱认证失败"); return; } folder = store.getFolder("INBOX"); @@ -148,8 +148,9 @@ public class SnEmailGatherTask { MailGatherLogVo gatherLogVo = new MailGatherLogVo(); gatherLogVo.setId(IdUtil.nanoId()); gatherLogVo.setSubject(EmailParseUtils.getSubject(msg)); - gatherLogVo.setFromAddress(emailName); -// String address = EmailParseUtils.getReceiveAddress(msg, null); +// String address = EmailParseUtils.getReceiveAddress(msg, null); + String from = getFrom(msg); + gatherLogVo.setFromAddress(from); gatherLogVo.setReceiveAddress(emailName); String sentDate = EmailParseUtils.getSentDate(msg, null); gatherLogVo.setSentDate(sentDate); @@ -213,9 +214,9 @@ public class SnEmailGatherTask { log.info("调用影像票夹接口返回参数:{}",body); Map resultMap = JSONObject.parseObject(body, Map.class); Object status = resultMap.get("status"); + gatherLogVo.setFileType(map.get("contentType")); + gatherLogVo.setFileName(map.get("fileName")); if(status != null && "200".equals(String.valueOf(status))){ - gatherLogVo.setFileName(map.get("fileName")); - gatherLogVo.setFileType(map.get("contentType")); buildMsg(gatherLogVo,"0","推送影像票夹成功"); }else { gatherLogVo.setFphm(""); @@ -317,6 +318,32 @@ public class SnEmailGatherTask { return hashMap; } + /** + * 获得邮件发件人 + * + * @param msg 邮件内容 + * @return 姓名 + * @throws MessagingException + * @throws UnsupportedEncodingException + */ + public String getFrom(MimeMessage msg) throws MessagingException, UnsupportedEncodingException { + String from = ""; + Address[] froms = msg.getFrom(); + if (froms.length < 1){ + return ""; + } + + InternetAddress address = (InternetAddress) froms[0]; + String person = address.getPersonal(); + if (person != null) { + person = MimeUtility.decodeText(person) + " "; + } else { + person = ""; + } + from = person + "<" + address.getAddress() + ">"; + + return from; + } public Store mailAuth(String emailName, String password){ //判断是否为QQ还是163 @@ -356,12 +383,21 @@ public class SnEmailGatherTask { // }); //Session session = Session.getInstance(props); Store store = null; + MailGatherLogVo gatherLogVo = new MailGatherLogVo(); + gatherLogVo.setFromAddress(emailName); try { store = session.getStore("pop3"); store.connect(emailName, password); + }catch(AuthenticationFailedException e){ + buildMsg(gatherLogVo,"1","密码错误连接邮箱失败"); + e.printStackTrace(); + log.error("连接邮箱异常信息{}",e); } catch (Exception e) { + buildMsg(gatherLogVo,"1","未知连接异常"); e.printStackTrace(); - log.error("连接邮箱异常信息{}",e.getMessage()); + log.error("连接邮箱异常信息{}",e); + + }finally { try { store.close(); } catch (MessagingException ex) {