邮箱解析优化

release
路明慧 2 years ago
parent d0f49ff19b
commit 27c15d8c1f
  1. 50
      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 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");
if(status != null && "200".equals(String.valueOf(status))){
gatherLogVo.setFileName(map.get("fileName"));
gatherLogVo.setFileType(map.get("contentType"));
gatherLogVo.setFileName(map.get("fileName"));
if(status != null && "200".equals(String.valueOf(status))){
buildMsg(gatherLogVo,"0","推送影像票夹成功");
}else {
gatherLogVo.setFphm("");
@ -317,6 +318,32 @@ public class SnEmailGatherTask {
return hashMap;
}
/**
* 获得邮件发件人
*
* @param msg 邮件内容
* @return 姓名 <Email地址>
* @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) {

Loading…
Cancel
Save