diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/MyMailUtil.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/MyMailUtil.java new file mode 100644 index 00000000..23d6a023 --- /dev/null +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/MyMailUtil.java @@ -0,0 +1,83 @@ +package org.jeecg.common.util; + +import cn.hutool.core.util.StrUtil; + +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import java.util.Date; +import java.util.Objects; + +public class MyMailUtil { + + private Integer port; + + public MyMailUtil(Integer port){ + this.port = port; + } + + /** + * 获取邮件主题 + * @param message + * @return + */ + public static String getSubject(Message message) throws MessagingException{ + String subject = null; + try { + subject = message.getSubject(); + } catch (MessagingException e) { + try { + subject = message.getHeader("subject")[0]; + } catch (MessagingException ex) { + throw new RuntimeException(ex); + } + } + return subject; + } + + public static Date getReceivedDate(Message message) throws MessagingException{ + Date receivedDate = null; + try { + receivedDate = message.getReceivedDate(); + } catch (MessagingException e) { + try { + receivedDate = new Date(message.getHeader("Date")[0]); + } catch (MessagingException ex) { + throw new RuntimeException(ex); + } + } + return receivedDate; + } + + public static String getMessageID(Message message) throws MessagingException{ + String messageId = ""; + try { + messageId = ((MimeMessage) message).getMessageID(); + } catch (MessagingException e) { + try { + messageId = Objects.isNull(message.getHeader("Message-ID")) ? "" : message.getHeader("Message-ID")[0]; + } catch (MessagingException ex) { + throw new RuntimeException(ex); + } + } + return messageId; + } + + public static String getAddress(Message message) throws MessagingException{ + String address = ""; + try { + address = ((InternetAddress) message.getFrom()[0]).getAddress(); + } catch (MessagingException e) { + try { + String[] froms = message.getHeader("From"); + if (null != froms) { + address = StrUtil.subBetween(froms[0], "<",">"); + } + } catch (MessagingException ex) { + throw new RuntimeException(ex); + } + } + return address; + } +}