|
@@ -8,13 +8,17 @@ 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.smtp.SMTPMessage;
|
|
import com.sun.mail.util.MailSSLSocketFactory;
|
|
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 org.springblade.system.attachment.entity.Attachment;
|
|
|
|
|
|
import javax.activation.DataHandler;
|
|
import javax.activation.DataHandler;
|
|
import javax.mail.*;
|
|
import javax.mail.*;
|
|
-import javax.mail.internet.*;
|
|
|
|
|
|
+import javax.mail.internet.InternetAddress;
|
|
|
|
+import javax.mail.internet.MimeBodyPart;
|
|
|
|
+import javax.mail.internet.MimeMultipart;
|
|
|
|
+import javax.mail.internet.MimeUtility;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
@@ -41,8 +45,12 @@ public class EmailUtil {
|
|
*/
|
|
*/
|
|
public static IMAPStore getIMAPStore(String host, String user, String password) throws MessagingException {
|
|
public static IMAPStore getIMAPStore(String host, String user, String password) throws MessagingException {
|
|
IMAPStore store = (IMAPStore) session.getStore("imap");
|
|
IMAPStore store = (IMAPStore) session.getStore("imap");
|
|
- store.connect(host, 993, user, password);
|
|
|
|
- store.id(map);
|
|
|
|
|
|
+
|
|
|
|
+ synchronized (EmailUtil.class) {
|
|
|
|
+ store.id(map);
|
|
|
|
+ store.connect(host, 993, user, password);
|
|
|
|
+ }
|
|
|
|
+
|
|
return store;
|
|
return store;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -94,37 +102,37 @@ public class EmailUtil {
|
|
return mailAddressList;
|
|
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 main(String[] args) throws Exception {
|
|
|
|
+ //
|
|
|
|
+ // Attachment attachment = new Attachment();
|
|
|
|
+ // attachment.setPath("https://os.winfaster.cn/fjhx/saas/GrandStar/2023/03/08/png/3092592f2d614d83854fad5e4c107143.png");
|
|
|
|
+ // attachment.setRealName("board-1.png");
|
|
|
|
+ //
|
|
|
|
+ // SendDto.Address address = new SendDto.Address();
|
|
|
|
+ // address.setAddress("2428241269@qq.com");
|
|
|
|
+ //
|
|
|
|
+ // SendDto sendDto = new SendDto();
|
|
|
|
+ // sendDto.setTo(Collections.singletonList(address));
|
|
|
|
+ // sendDto.setAttachmentList(Collections.singletonList(attachment));
|
|
|
|
+ // sendDto.setSubject("测试标题");
|
|
|
|
+ // sendDto.setContent("测试内容");
|
|
|
|
+ //
|
|
|
|
+ // sendMail("imap.qq.com", "2428241269@qq.com", "kdypajdrwfhtdihb", sendDto);
|
|
|
|
+ // // sendMail("imap.163.com", "l17689260703@163.com", "WFARTAFVZZKOVNUF", sendDto);
|
|
|
|
+ // }
|
|
|
|
|
|
|
|
|
|
public static void sendMail(String host, String user, String password, SendDto sendDto) throws Exception {
|
|
public static void sendMail(String host, String user, String password, SendDto sendDto) throws Exception {
|
|
|
|
|
|
|
|
+ // QQ存在一个特性设置SSL加密
|
|
|
|
+ MailSSLSocketFactory sf = new MailSSLSocketFactory();
|
|
|
|
+ sf.setTrustAllHosts(true);
|
|
|
|
+
|
|
// 创建一个配置文件并保存
|
|
// 创建一个配置文件并保存
|
|
Properties properties = new Properties();
|
|
Properties properties = new Properties();
|
|
properties.setProperty("mail.host", host);
|
|
properties.setProperty("mail.host", host);
|
|
properties.setProperty("mail.transport.protocol", "smtp");
|
|
properties.setProperty("mail.transport.protocol", "smtp");
|
|
properties.setProperty("mail.smtp.auth", "true");
|
|
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.enable", "true");
|
|
properties.put("mail.smtp.ssl.socketFactory", sf);
|
|
properties.put("mail.smtp.ssl.socketFactory", sf);
|
|
|
|
|
|
@@ -139,11 +147,8 @@ public class EmailUtil {
|
|
// 获取连接对象
|
|
// 获取连接对象
|
|
Transport transport = session.getTransport();
|
|
Transport transport = session.getTransport();
|
|
|
|
|
|
- // 连接服务器
|
|
|
|
- transport.connect(host, user, password);
|
|
|
|
-
|
|
|
|
// 创建邮件对象
|
|
// 创建邮件对象
|
|
- MimeMessage mimeMessage = new MimeMessage(session);
|
|
|
|
|
|
+ SMTPMessage mimeMessage = new SMTPMessage(session);
|
|
|
|
|
|
// // 邮件发送人
|
|
// // 邮件发送人
|
|
mimeMessage.setFrom(new InternetAddress(user));
|
|
mimeMessage.setFrom(new InternetAddress(user));
|
|
@@ -194,9 +199,10 @@ public class EmailUtil {
|
|
|
|
|
|
mimeMessage.setContent(mimeMultipart);
|
|
mimeMessage.setContent(mimeMultipart);
|
|
|
|
|
|
|
|
+ // 连接服务器
|
|
|
|
+ transport.connect(host, user, password);
|
|
// 发送邮件
|
|
// 发送邮件
|
|
- transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
|
|
|
|
-
|
|
|
|
|
|
+ transport.sendMessage(mimeMessage, new Address[]{new InternetAddress(user)});
|
|
// 关闭连接
|
|
// 关闭连接
|
|
transport.close();
|
|
transport.close();
|
|
|
|
|