|
@@ -1,21 +1,30 @@
|
|
package com.fjhx.utils;
|
|
package com.fjhx.utils;
|
|
|
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
|
+import com.fjhx.dto.SendDto;
|
|
|
|
+import com.fjhx.entity.MailAddress;
|
|
import com.fjhx.entity.MailFolder;
|
|
import com.fjhx.entity.MailFolder;
|
|
import com.fjhx.enums.MailFlagEnum;
|
|
import com.fjhx.enums.MailFlagEnum;
|
|
import com.sun.mail.imap.IMAPFolder;
|
|
import com.sun.mail.imap.IMAPFolder;
|
|
import com.sun.mail.imap.IMAPStore;
|
|
import com.sun.mail.imap.IMAPStore;
|
|
|
|
+import com.sun.mail.util.MailSSLSocketFactory;
|
|
import org.springblade.core.tool.utils.StringPool;
|
|
import org.springblade.core.tool.utils.StringPool;
|
|
|
|
+import org.springblade.system.attachment.entity.Attachment;
|
|
|
|
|
|
-import javax.mail.Flags;
|
|
|
|
-import javax.mail.MessagingException;
|
|
|
|
-import javax.mail.Session;
|
|
|
|
|
|
+import javax.activation.DataHandler;
|
|
|
|
+import javax.mail.*;
|
|
|
|
+import javax.mail.internet.*;
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
+import java.net.URL;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
public class EmailUtil {
|
|
public class EmailUtil {
|
|
|
|
|
|
private static final Properties props = new Properties();
|
|
private static final Properties props = new Properties();
|
|
private static final HashMap<String, String> map = new HashMap<>();
|
|
private static final HashMap<String, String> map = new HashMap<>();
|
|
|
|
+ private static final Session session;
|
|
|
|
+
|
|
static {
|
|
static {
|
|
props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
|
props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
|
|
|
|
|
@@ -23,13 +32,14 @@ public class EmailUtil {
|
|
map.put("version", "1.0.0");
|
|
map.put("version", "1.0.0");
|
|
map.put("vendor", "myClient");
|
|
map.put("vendor", "myClient");
|
|
map.put("support-email", "fjhx@fjhx.com");
|
|
map.put("support-email", "fjhx@fjhx.com");
|
|
|
|
+
|
|
|
|
+ session = Session.getInstance(props);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取store
|
|
* 获取store
|
|
*/
|
|
*/
|
|
public static IMAPStore getIMAPStore(String host, String user, String password) throws MessagingException {
|
|
public static IMAPStore getIMAPStore(String host, String user, String password) throws MessagingException {
|
|
- Session session = Session.getInstance(props);
|
|
|
|
IMAPStore store = (IMAPStore) session.getStore("imap");
|
|
IMAPStore store = (IMAPStore) session.getStore("imap");
|
|
store.connect(host, 993, user, password);
|
|
store.connect(host, 993, user, password);
|
|
store.id(map);
|
|
store.id(map);
|
|
@@ -67,6 +77,159 @@ public class EmailUtil {
|
|
return stringJoiner.toString();
|
|
return stringJoiner.toString();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取收件人、抄送人、密送人、回复人
|
|
|
|
+ */
|
|
|
|
+ public static List<MailAddress> mailAddressList(Message message, Long mainInfoId) throws MessagingException {
|
|
|
|
+ Address[] to = message.getRecipients(Message.RecipientType.TO);
|
|
|
|
+ Address[] cc = message.getRecipients(Message.RecipientType.CC);
|
|
|
|
+ Address[] bcc = message.getRecipients(Message.RecipientType.BCC);
|
|
|
|
+ Address[] replyTo = message.getReplyTo();
|
|
|
|
+
|
|
|
|
+ List<MailAddress> mailAddressList = new ArrayList<>();
|
|
|
|
+ addMailAddressList(mailAddressList, to, mainInfoId, 1);
|
|
|
|
+ addMailAddressList(mailAddressList, cc, mainInfoId, 2);
|
|
|
|
+ addMailAddressList(mailAddressList, bcc, mainInfoId, 3);
|
|
|
|
+ addMailAddressList(mailAddressList, replyTo, mainInfoId, 4);
|
|
|
|
+ return mailAddressList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
|
+
|
|
|
|
+ Attachment attachment = new Attachment();
|
|
|
|
+ attachment.setPath("https://os.winfaster.cn/fjhx/saas/GOLDSUN/2023/03/06/pdf/a69898863c834e68b19942ad26a49e10.pdf");
|
|
|
|
+ attachment.setRealName("测试.pdf");
|
|
|
|
+
|
|
|
|
+ SendDto sendDto = new SendDto();
|
|
|
|
+ SendDto.Address address = new SendDto.Address();
|
|
|
|
+ address.setAddress("2428241269@qq.com");
|
|
|
|
+ address.setPersonal("测试小小张");
|
|
|
|
+ sendDto.setTo(Arrays.asList(address));
|
|
|
|
+
|
|
|
|
+ sendDto.setAttachmentList(Arrays.asList(attachment));
|
|
|
|
+
|
|
|
|
+ sendDto.setSubject("测试标题");
|
|
|
|
+ sendDto.setContent("测试内容");
|
|
|
|
+ sendMail("imap.qq.com", "2428241269@qq.com", "kdypajdrwfhtdihb", sendDto);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static void sendMail(String host, String user, String password, SendDto sendDto) throws Exception {
|
|
|
|
+
|
|
|
|
+ // 创建一个配置文件并保存
|
|
|
|
+ Properties properties = new Properties();
|
|
|
|
+ properties.setProperty("mail.host", host);
|
|
|
|
+ properties.setProperty("mail.transport.protocol", "smtp");
|
|
|
|
+ properties.setProperty("mail.smtp.auth", "true");
|
|
|
|
+
|
|
|
|
+ // QQ存在一个特性设置SSL加密
|
|
|
|
+ MailSSLSocketFactory sf = new MailSSLSocketFactory();
|
|
|
|
+ sf.setTrustAllHosts(true);
|
|
|
|
+ properties.put("mail.smtp.ssl.enable", "true");
|
|
|
|
+ properties.put("mail.smtp.ssl.socketFactory", sf);
|
|
|
|
+
|
|
|
|
+ // 创建一个session对象
|
|
|
|
+ Session session = Session.getDefaultInstance(properties, new Authenticator() {
|
|
|
|
+ @Override
|
|
|
|
+ protected PasswordAuthentication getPasswordAuthentication() {
|
|
|
|
+ return new PasswordAuthentication(user, password);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ // 获取连接对象
|
|
|
|
+ Transport transport = session.getTransport();
|
|
|
|
+
|
|
|
|
+ // 连接服务器
|
|
|
|
+ transport.connect(host, user, password);
|
|
|
|
+
|
|
|
|
+ // 创建邮件对象
|
|
|
|
+ MimeMessage mimeMessage = new MimeMessage(session);
|
|
|
|
+
|
|
|
|
+ // // 邮件发送人
|
|
|
|
+ mimeMessage.setFrom(new InternetAddress(user));
|
|
|
|
+
|
|
|
|
+ List<SendDto.Address> to = sendDto.getTo();
|
|
|
|
+ if (ObjectUtil.isNotEmpty(to)) {
|
|
|
|
+ mimeMessage.setRecipients(Message.RecipientType.TO, getAddresses(to));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<SendDto.Address> cc = sendDto.getCc();
|
|
|
|
+ if (ObjectUtil.isNotEmpty(cc)) {
|
|
|
|
+ mimeMessage.setRecipients(Message.RecipientType.CC, getAddresses(cc));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<SendDto.Address> bcc = sendDto.getBcc();
|
|
|
|
+ if (ObjectUtil.isNotEmpty(bcc)) {
|
|
|
|
+ mimeMessage.setRecipients(Message.RecipientType.BCC, getAddresses(bcc));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<SendDto.Address> replyTo = sendDto.getReplyTo();
|
|
|
|
+ if (ObjectUtil.isNotEmpty(replyTo)) {
|
|
|
|
+ mimeMessage.setReplyTo(getAddresses(replyTo));
|
|
|
|
+ }
|
|
|
|
+ // 邮件标题
|
|
|
|
+ mimeMessage.setSubject(sendDto.getSubject());
|
|
|
|
+
|
|
|
|
+ // 邮件内容大对象
|
|
|
|
+ MimeMultipart mimeMultipart = new MimeMultipart();
|
|
|
|
+
|
|
|
|
+ // 对象内添加正文
|
|
|
|
+ BodyPart content = new MimeBodyPart();
|
|
|
|
+ content.setContent(sendDto.getContent(), "text/html;charset=UTF-8");
|
|
|
|
+ mimeMultipart.addBodyPart(content);
|
|
|
|
+
|
|
|
|
+ // 对象内添加邮件
|
|
|
|
+ List<Attachment> attachmentList = sendDto.getAttachmentList();
|
|
|
|
+ if (ObjectUtil.isNotEmpty(attachmentList)) {
|
|
|
|
+ for (Attachment attachment : attachmentList) {
|
|
|
|
+ DataHandler dataHandler = new DataHandler(new URL(attachment.getPath()));
|
|
|
|
+
|
|
|
|
+ MimeBodyPart mimeBodyPart = new MimeBodyPart();
|
|
|
|
+ mimeBodyPart.setDataHandler(dataHandler);
|
|
|
|
+ mimeBodyPart.setContentID(IdWorker.getIdStr());
|
|
|
|
+ mimeBodyPart.setFileName(MimeUtility.encodeText(attachment.getRealName()));
|
|
|
|
+ mimeMultipart.addBodyPart(mimeBodyPart);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ mimeMessage.setContent(mimeMultipart);
|
|
|
|
+
|
|
|
|
+ // 发送邮件
|
|
|
|
+ transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
|
|
|
|
+
|
|
|
|
+ // 关闭连接
|
|
|
|
+ transport.close();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static InternetAddress[] getAddresses(List<SendDto.Address> addressList) throws UnsupportedEncodingException {
|
|
|
|
+ InternetAddress[] internetAddresses = new InternetAddress[addressList.size()];
|
|
|
|
+ for (int i = 0; i < addressList.size(); i++) {
|
|
|
|
+ InternetAddress internetAddress = new InternetAddress();
|
|
|
|
+ internetAddress.setPersonal(addressList.get(i).getPersonal(), "utf-8");
|
|
|
|
+ internetAddress.setAddress(addressList.get(i).getAddress());
|
|
|
|
+ internetAddresses[i] = internetAddress;
|
|
|
|
+ }
|
|
|
|
+ return internetAddresses;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void addMailAddressList(List<MailAddress> mailAddressList, Address[] addresses, Long mainInfoId, Integer type) {
|
|
|
|
+ if (addresses == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ for (Address address : addresses) {
|
|
|
|
+ InternetAddress internetAddress = (InternetAddress) address;
|
|
|
|
+
|
|
|
|
+ MailAddress mailAddress = new MailAddress();
|
|
|
|
+ mailAddress.setType(type);
|
|
|
|
+ mailAddress.setEmail(internetAddress.getAddress());
|
|
|
|
+ mailAddress.setPersonalName(internetAddress.getPersonal());
|
|
|
|
+ mailAddress.setMailInfoId(mainInfoId);
|
|
|
|
+ mailAddressList.add(mailAddress);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
private static void addMailFolderList(List<MailFolder> mailFolderList, IMAPFolder[] folders, String mailboxId) throws MessagingException {
|
|
private static void addMailFolderList(List<MailFolder> mailFolderList, IMAPFolder[] folders, String mailboxId) throws MessagingException {
|
|
for (IMAPFolder folder : folders) {
|
|
for (IMAPFolder folder : folders) {
|
|
int type = folder.getType();
|
|
int type = folder.getType();
|