24282 2 years ago
parent
commit
5dfe1c88c0
1 changed files with 32 additions and 19 deletions
  1. 32 19
      src/main/java/com/fjhx/email/utils/EmailUtil.java

+ 32 - 19
src/main/java/com/fjhx/email/utils/EmailUtil.java

@@ -26,13 +26,36 @@ import java.util.*;
 
 public class EmailUtil {
 
-    private static final HashMap<String, String> ImapStoreId = new HashMap<>();
+    private static final Properties properties;
+
+    private static final Session session;
+
+    private static final HashMap<String, String> storeId = new HashMap<>();
 
     static {
-        ImapStoreId.put("name", "fjhx");
-        ImapStoreId.put("version", "1.0.0");
-        ImapStoreId.put("vendor", "fjhxClient");
-        ImapStoreId.put("support-email", "fjhx@fjhx.com");
+
+        properties = new Properties();
+
+        // 开启ssl
+        properties.put("mail.imap.ssl.enable", true);
+
+        // 套接字连接超时值(毫秒)。此超时由java.net.Socket实现。默认是无限超时。
+        properties.put("mail.imap.connectiontimeout", 1000 * 60);
+
+        // 套接字读取超时值(毫秒)。此超时由java.net.Socket实现。默认是无限超时。
+        properties.put("mail.imap.timeout", 1000 * 60);
+
+        // 套接字写超时值,单位为毫秒。此超时是通过使用java.util.concurrent实现的。
+        // ScheduledExecutorService每个连接,调度线程在超时过期时关闭套接字。
+        // 因此,使用此超时的开销是每个连接一个线程。默认是无限超时。
+        properties.put("mail.imap.writetimeout", 1000 * 60);
+
+        session = Session.getInstance(properties);
+
+        storeId.put("name", "fjhx");
+        storeId.put("version", "1.0");
+        storeId.put("vendor", "fjhxClient");
+        storeId.put("support-email", "fjhx@fjhx.com");
     }
 
     // @SneakyThrows
@@ -65,27 +88,17 @@ public class EmailUtil {
      * 993
      */
     public static IMAPStore getIMAPStore(MailboxInfo mailboxInfo) throws MessagingException {
-        Properties ImapProperties = new Properties();
-        ImapProperties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
-        // 开启ssl
-        ImapProperties.put("mail.imap.ssl.enable", true);
-        // 套接字连接超时值(毫秒)。此超时由java.net.Socket实现。默认是无限超时。
-        ImapProperties.put("mail.imap.connectiontimeout", 1000 * 60);
-        // 套接字读取超时值(毫秒)。此超时由java.net.Socket实现。默认是无限超时。
-        ImapProperties.put("mail.imap.timeout", 1000 * 60);
-        // 套接字写超时值,单位为毫秒。此超时是通过使用java.util.concurrent实现的。
-        // ScheduledExecutorService每个连接,调度线程在超时过期时关闭套接字。
-        // 因此,使用此超时的开销是每个连接一个线程。默认是无限超时。
-        ImapProperties.put("mail.imap.writetimeout", 1000 * 60);
 
-        Session session = Session.getInstance(ImapProperties);
         IMAPStore store = (IMAPStore) session.getStore("imap");
+
         store.connect(
                 mailboxInfo.getReceiveHost(),
                 mailboxInfo.getReceivePort(),
                 mailboxInfo.getMailUser(),
                 mailboxInfo.getMailPassword());
-        store.id(ImapStoreId);
+
+        store.id(storeId);
+
         return store;
     }