|
|
@ -11,10 +11,12 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.mail.Flags;
|
|
|
|
import javax.mail.*;
|
|
|
|
import javax.mail.Message;
|
|
|
|
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
|
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
|
|
|
|
import javax.mail.internet.MimeMultipart;
|
|
|
|
import javax.mail.internet.MimeUtility;
|
|
|
|
import javax.mail.internet.MimeUtility;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
@ -33,23 +35,28 @@ public class MailHelper {
|
|
|
|
private EventLogMapper eventLogMapper;
|
|
|
|
private EventLogMapper eventLogMapper;
|
|
|
|
|
|
|
|
|
|
|
|
protected void parseMsg(Message[] messages,LarkTableRequest add) {
|
|
|
|
protected void parseMsg(Message[] messages,LarkTableRequest add) {
|
|
|
|
|
|
|
|
log.info("------------------------开始解析邮件----------------------------------");
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 意向度、现有学历、学生姓名、报名时间、手机号码、省市、备注、回放记录、业务员、信息完成度、数据来源、目标国家、所属学校、邮箱来源
|
|
|
|
* 意向度、现有学历、学生姓名、报名时间、手机号码、省市、备注、回放记录、业务员、信息完成度、数据来源、目标国家、所属学校、邮箱来源
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
List<EventLog> logs = new ArrayList<>();
|
|
|
|
List<EventLog> logs = new ArrayList<>();
|
|
|
|
for (Message message : messages) {
|
|
|
|
for (Message message : messages) {
|
|
|
|
|
|
|
|
String msgSubject = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
System.out.println("消息:" + message.getSubject());
|
|
|
|
log.info("----------------------------------------------------------");
|
|
|
|
String from = MimeUtility.decodeText(message.getFrom()[0].toString());
|
|
|
|
String from = MimeUtility.decodeText(message.getFrom()[0].toString());
|
|
|
|
InternetAddress internetAddress = new InternetAddress(from);
|
|
|
|
InternetAddress internetAddress = new InternetAddress(from);
|
|
|
|
System.out.println("发件人:" + internetAddress.getPersonal() + '(' + internetAddress.getAddress() + ')');
|
|
|
|
msgSubject = String.format("主题:%s,发件人:%s(%s),收件人:%s",
|
|
|
|
|
|
|
|
message.getSubject(),
|
|
|
|
|
|
|
|
internetAddress.getPersonal(),
|
|
|
|
|
|
|
|
internetAddress.getAddress(),
|
|
|
|
|
|
|
|
this.getReceiveAddress(message,null));
|
|
|
|
String recordId = syncLarkTable(message,add);
|
|
|
|
String recordId = syncLarkTable(message,add);
|
|
|
|
this.buildLog(logs,message,recordId,null);
|
|
|
|
this.buildLog(logs,msgSubject,recordId,null);
|
|
|
|
message.setFlag(Flags.Flag.SEEN, true);
|
|
|
|
message.setFlag(Flags.Flag.SEEN, true);
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
this.buildLog(logs,message,null,e);
|
|
|
|
this.buildLog(logs,msgSubject,null,e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (CollectionUtils.isNotEmpty(logs)){
|
|
|
|
if (CollectionUtils.isNotEmpty(logs)){
|
|
|
@ -57,22 +64,62 @@ public class MailHelper {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void buildLog(List<EventLog> logs,Message message,String recordId,Exception e){
|
|
|
|
public String getReceiveAddress(Message msg, Message.RecipientType type) throws MessagingException {
|
|
|
|
EventLog log = e != null ?
|
|
|
|
StringBuffer receiveAddress = new StringBuffer();
|
|
|
|
new EventLog(null, EventOperateType.SYNC_MAIL.getCode(), message.toString())
|
|
|
|
Address[] addresss = null;
|
|
|
|
: new EventLog(null, EventOperateType.SYNC_MAIL.getCode(), message.toString(),null,e.getMessage());
|
|
|
|
if (type == null) {
|
|
|
|
|
|
|
|
addresss = msg.getAllRecipients();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
addresss = msg.getRecipients(type);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (addresss == null || addresss.length < 1)
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
for (Address address : addresss) {
|
|
|
|
|
|
|
|
InternetAddress internetAddress = (InternetAddress) address;
|
|
|
|
|
|
|
|
receiveAddress.append(internetAddress.toUnicodeString()).append(",");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
receiveAddress.deleteCharAt(receiveAddress.length() - 1); //删除最后一个逗号
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return receiveAddress.toString();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public 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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void buildLog(List<EventLog> logs,String message,String recordId,Exception e){
|
|
|
|
|
|
|
|
EventLog log = e == null ?
|
|
|
|
|
|
|
|
new EventLog(null, EventOperateType.SYNC_MAIL.getCode(), message)
|
|
|
|
|
|
|
|
: new EventLog(null, EventOperateType.SYNC_MAIL.getCode(), message,null,e.getMessage());
|
|
|
|
log.setRemark(recordId);
|
|
|
|
log.setRemark(recordId);
|
|
|
|
logs.add(log);
|
|
|
|
logs.add(log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String syncLarkTable(Message message,LarkTableRequest add){
|
|
|
|
private String syncLarkTable(Message message,LarkTableRequest add) throws MessagingException, IOException {
|
|
|
|
Map<String, Object> body = this.buildBody(message);
|
|
|
|
StringBuffer content = new StringBuffer(30);
|
|
|
|
|
|
|
|
getMailTextContent(message,content);
|
|
|
|
|
|
|
|
Map<String, Object> body = this.buildBody(message,content.toString());
|
|
|
|
add.setBody(body);
|
|
|
|
add.setBody(body);
|
|
|
|
CreateAppTableRecordRespBody respBody = larkTableHelper.addTableRecord(add);
|
|
|
|
CreateAppTableRecordRespBody respBody = larkTableHelper.addTableRecord(add);
|
|
|
|
return respBody.getRecord().getRecordId();
|
|
|
|
return respBody.getRecord().getRecordId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected Map<String,Object> buildBody(Message message){
|
|
|
|
protected Map<String,Object> buildBody(Message message,String content){
|
|
|
|
return new HashMap<>();
|
|
|
|
return new HashMap<>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|