xinyb
3 天以前 3b74e3df72726e188d36393ecfd7964d095ef7e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
package com.yc.crm.mail.action;
 
import com.sun.mail.imap.IMAPStore;
import com.yc.action.mail.EmailConfigUtils;
import com.yc.crm.mail.entity.FoundationEntity;
import com.yc.crm.mail.entity.T482102Entity;
import com.yc.crm.mail.service.MailAccountIfc;
import com.yc.crm.mail.service.MailIfc;
import com.yc.crm.mail.service.MailServiceIfc;
import com.yc.crm.mail.util.AllBackMsg;
import com.yc.entity.DataSourceEntity;
import com.yc.factory.FactoryBean;
import com.yc.multiData.MultiDataSource;
import com.yc.multiData.SpObserver;
import com.yc.utils.SessionKey;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;
 
import javax.mail.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
 
/**
 * @BelongsProject: eCoWorksV3
 * @BelongsPackage: com.yc.crm.action
 * @author: xinyb
 * @CreateTime: 2024-07-19  17:33
 * @Description: 个人邮箱配置
 */
@CrossOrigin
@RestController
@RequestMapping("/crm/mail/account")
public class MailAccount {
    ThreadPoolTaskExecutor threadPoolExecutor = (ThreadPoolTaskExecutor) FactoryBean.getBean("threadPoolExecutor");//线程池
 
    @Autowired
    MailAccountIfc mailAccountIfc;
    @Autowired
    MailIfc mailIfc;
    @Autowired
    MailServiceIfc mailServiceIfc;
    /**
     * 添加邮箱配置信息
     *
     * @param account
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/addAccount.do")
    public AllBackMsg addAccount(@RequestBody T482102Entity account, HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            if (StringUtils.isBlank(account.getEmail())) {
                msg.setFail("请输入邮箱账号");
                return msg;
            }
            if (account.getMailType().equals(0) || account.getMailType().equals(1)) {
                if (StringUtils.isBlank(account.getPassword())) {
                    msg.setFail("请输入邮箱密码");
                    return msg;
                }
                account.setReceiveEmail(account.getEmail());
                account.setReceivePassword(account.getPassword());
                account.setSmtpEmail(account.getEmail());
                account.setSmtpPassword(account.getPassword());
            }
            if (account.getMailType().equals(1) || account.getMailType().equals(2)) {
                if (StringUtils.isBlank(account.getReceiveProtocol())) {
                    msg.setFail("请选择协议类型");
                    return msg;
                }
                if (StringUtils.isBlank(account.getReceiveHost())) {
                    msg.setFail("请输入收邮件服务器");
                    return msg;
                }
                if (account.getReceivePort().equals(0)) {
                    msg.setFail("请输入收邮件服务器端口");
                    return msg;
                }
                if (StringUtils.isBlank(account.getSmtpHost())) {
                    msg.setFail("请输入发邮件服务器");
                    return msg;
                }
                if (account.getSmtpPort().equals(0)) {
                    msg.setFail("请输入发邮件服务器端口");
                    return msg;
                }
                if (account.getMailType().equals(2)) {
                    if (StringUtils.isBlank(account.getReceiveEmail())) {
                        msg.setFail("请输入收件账号");
                        return msg;
                    }
                    if (StringUtils.isBlank(account.getReceivePassword())) {
                        msg.setFail("请输入收件密码");
                        return msg;
                    }
                    if (StringUtils.isBlank(account.getSmtpEmail())) {
                        msg.setFail("请输入发件账号");
                        return msg;
                    }
                    if (StringUtils.isBlank(account.getSmtpPassword())) {
                        msg.setFail("请输入发件密码");
                        return msg;
                    }
                }
            }
            HttpSession session = request.getSession();
            String companyId = (String) session.getAttribute(SessionKey.COMPANY_ID);
            String companyName = (String) session.getAttribute(SessionKey.COMPANY_NAME);
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            String userName = (String) session.getAttribute(SessionKey.USER_NAME);
            account.setCompanyId(companyId);
            account.setCompanyName(companyName);
            account.setUserCode(userCode);
            account.setUserName(userName);
            if (account.getMailType().equals(0)) {
                //初始配置
                getMailConfigProperties(account);
            }
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            //邮件检测
            String error = emailDetection(account);
            if (StringUtils.isNotBlank(error)) {
                account.setMailStatus("异常");
            }
            account= mailAccountIfc.addEmailAccount(account);//添加新的邮箱配置
            Map<String, Object> map = new HashMap<>();
            if (StringUtils.isBlank(error)) {
                map.put("code", 0);
                map.put("companyId", companyId);
                map.put("companyName", companyName);
                map.put("userCode", userCode);
                map.put("userName", userName);
                map.put("email", account.getEmail());
                msg.setSuccess("已保存", map);
                //多线程收邮件
                threadPoolExecutor.execute(new MailController.ReceivingEmailsSave(mailServiceIfc, account, new FoundationEntity(request)));
            } else {
                map.put("code", -1);
                map.put("mailStatus", "异常");
                map.put("error", error);
                msg.setSuccess("执行成功", map);
            }
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * 添加别名邮箱
     *
     * @param account
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/addAliasEmail.do")
    public AllBackMsg addAliasEmail(String aliasEmail, Integer accountId, HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            if (StringUtils.isBlank(aliasEmail)) {
                msg.setFail("请输入别名邮箱");
                return msg;
            }
            if (accountId.equals(0)) {
                msg.setFail("邮箱账号Id不能为空");
                return msg;
            }
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            T482102Entity t482102Entity = mailAccountIfc.getAccountInfo(userCode, accountId);
            if (t482102Entity.getMailType().equals(0) || t482102Entity.getMailType().equals(1)) {
                t482102Entity.setEmail(aliasEmail);
                t482102Entity.setReceiveEmail(aliasEmail);
                t482102Entity.setSmtpEmail(aliasEmail);
            }
            //邮箱检测
            String error = emailDetection(t482102Entity);
            Map<String, Object> map = new HashMap<>();
            if (StringUtils.isBlank(error)) {
                mailAccountIfc.updateAliasEmail(aliasEmail, accountId);
                map.put("code", 0);
                map.put("userCode", userCode);
                map.put("aliasEmail", aliasEmail);
                map.put("accountId", accountId);
                msg.setSuccess("已保存", map);
            } else {
                map.put("code", -1);
                map.put("mailStatus", "异常");
                map.put("error", error);
                msg.setSuccess("执行成功", map);
            }
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * 更新邮箱配置信息
     *
     * @param account
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/updateAccount.do")
    public AllBackMsg updateAccount(@RequestBody T482102Entity account, HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            if (account.getAccountId() == 0) {
                msg.setFail("获取不到邮箱配置ID,请检查");
                return msg;
            }
            if (StringUtils.isBlank(account.getEmail())) {
                msg.setFail("请输入邮箱账号");
                return msg;
            }
            if (account.getMailType().equals(0)) {
                account.setMailType(1);
            }
            if (account.getMailType().equals(1)) {
                if (StringUtils.isBlank(account.getPassword())) {
                    msg.setFail("请输入邮箱密码");
                    return msg;
                }
                account.setReceiveEmail(account.getEmail());
                account.setReceivePassword(account.getPassword());
                account.setSmtpEmail(account.getEmail());
                account.setSmtpPassword(account.getPassword());
            } else if (account.getMailType().equals(2)) {
                if (StringUtils.isBlank(account.getReceiveEmail())) {
                    msg.setFail("请输入收件账号");
                    return msg;
                }
                if (StringUtils.isBlank(account.getReceivePassword())) {
                    msg.setFail("请输入收件密码");
                    return msg;
                }
                if (StringUtils.isBlank(account.getSmtpEmail())) {
                    msg.setFail("请输入发件账号");
                    return msg;
                }
                if (StringUtils.isBlank(account.getSmtpPassword())) {
                    msg.setFail("请输入发件密码");
                    return msg;
                }
            }
            if (StringUtils.isBlank(account.getReceiveHost())) {
                msg.setFail("请输入收邮件服务器");
                return msg;
            }
            if (account.getReceivePort().equals(0)) {
                msg.setFail("请输入收邮件服务器端口");
                return msg;
            }
            if (StringUtils.isBlank(account.getSmtpHost())) {
                msg.setFail("请输入发邮件服务器");
                return msg;
            }
            if (account.getSmtpPort().equals(0)) {
                msg.setFail("请输入发邮件服务器端口");
                return msg;
            }
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            String userName = (String) session.getAttribute(SessionKey.USER_NAME);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
            account.setUserCode(userCode);
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            mailAccountIfc.updateEmailAccount(account);
            String error = emailDetection(account);
            Map<String, Object> map = new HashMap<>();
            if (StringUtils.isBlank(error)) {
                map.put("code", 0);
                map.put("userCode", userCode);
                map.put("userName", userName);
                map.put("email", account.getEmail());
                msg.setSuccess("已更新", map);
            } else {
                //异常处理
                mailAccountIfc.updateMailStatus("异常", account.getAccountId());
                map.put("code", -1);
                map.put("mailStatus", "异常");
                map.put("error", error);
                msg.setSuccess("执行成功", map);
            }
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * 删除邮箱配置信息
     *
     * @param account
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/deleteAccount.do")
    public AllBackMsg deleteAccount(@RequestParam(defaultValue = "0") Integer accountId, HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            if (accountId.equals(0)) {
                msg.setFail("邮箱编号ID获取不到");
                return msg;
            }
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            Integer cont = mailAccountIfc.deleteEmailAccount(userCode, accountId);
            if (cont > 0) {
                msg.setOk("已删除");
            }
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * 查询邮箱配置列表
     *
     * @param search   搜索
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/getAccountList.do")
    public AllBackMsg getAccountList(HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            List<T482102Entity> t482102Entities = mailAccountIfc.getAccount(userCode);
            if (t482102Entities.size() > 0) {
                msg.setSuccess("执行成功", t482102Entities);
            } else {
                msg.setFail("未找到配置信息,0条数据");
            }
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
 
    /**
     * 查询个人邮箱信息
     *
     * @param search   搜索
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/getAccount.do")
    public AllBackMsg getAccount(String mail, HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            if (StringUtils.isBlank(mail)) {
                msg.setFail("参数 邮件账号 不能为空");
                return msg;
            }
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            T482102Entity t482102Entities = mailAccountIfc.getAccountInfo(userCode, mail);
            if (StringUtils.isNotBlank(t482102Entities.getEmail())) {
                msg.setSuccess("执行成功", t482102Entities);
            } else {
                msg.setFail("未找到配置信息");
            }
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * 检测绑定是否已经存在(是否已绑定)
     *
     * @param email
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/eEmailExists.do")
    public AllBackMsg eEmailExists(String email, HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
 
        try {
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
            if (StringUtils.isBlank(email)) {
                msg.setFail("邮箱不能为空");
                return msg;
            }
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            //根据当前用户查询绑定的邮箱信息
            T482102Entity emailEntity = mailAccountIfc.getAccountInfo(userCode, email);//返回邮箱的账号信息
            Map<String, Object> map = new HashMap<>();
            if (emailEntity == null || StringUtils.isBlank(emailEntity.getEmail())) {
                map.put("code", 0);
                map.put("msg", "该邮箱未绑定");
            } else {
                map.put("code", -1);
                map.put("msg", "该邮箱已绑定");
            }
            msg.setSuccess("执行成功", map);
        } catch (MessagingException e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * 检测绑定是否有效
     *
     * @param email
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/isEmailValid.do")
    public AllBackMsg isEmailValid(String email, HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
 
        try {
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
            if (StringUtils.isBlank(email)) {
                msg.setFail("检测绑定邮箱不能为空");
                return msg;
            }
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            //根据当前用户查询绑定的邮箱信息
            T482102Entity emailEntity = mailAccountIfc.getAccountInfo(userCode, email);//返回邮箱的账号信息
            if (emailEntity == null || StringUtils.isBlank(emailEntity.getEmail())) {
                msg.setFail("找不到邮箱:" + email + "配置信息请完善。");
                return msg;
            }
            String error = emailDetection(emailEntity);
            Map<String, Object> map = new HashMap<>();
            if (StringUtils.isBlank(error)) {
                map.put("code", 0);
                map.put("email", emailEntity.getEmail());
                map.put("protocol", emailEntity.getReceiveProtocol());
                map.put("receiveHost", emailEntity.getReceiveHost());
                map.put("receiveSSL", emailEntity.isReceiveSSL());
                map.put("receivePort", emailEntity.getReceivePort());
                map.put("smtpHost", emailEntity.getSmtpHost());
                map.put("smtpSSL", emailEntity.isSmtpSSL());
                map.put("smtpPort", emailEntity.getSmtpPort());
                map.put("proxyFlag", emailEntity.isProxyFlag());
                map.put("biSyncFlag", emailEntity.isBiSyncFlag());
                map.put("mailStatus", "正常");
                msg.setSuccess("执行成功", map);
            } else {
                //异常处理
                mailAccountIfc.updateMailStatus("异常", emailEntity.getAccountId());
                map.put("code", -1);
                map.put("mailStatus", "异常");
                map.put("error", error);
                msg.setSuccess("执行成功", map);
            }
        } catch (MessagingException e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * 邮箱检测
     *
     * @param emailEntity
     * @return
     * @throws MessagingException
     */
    private String emailDetection(T482102Entity emailEntity) throws MessagingException {
        String errorMsg = "";
        try {
            String protocol = emailEntity.getReceiveProtocol().toLowerCase();//接收协议 imap pop3
            Properties props = new Properties();
            props.put("mail.store.protocol", protocol);//指定接收的邮件协议 IMAP over pop3
            if (protocol.contains("imap")) {
                props.put("mail.imaps.host", emailEntity.getReceiveHost());
                props.put("mail.imaps.port", emailEntity.getReceivePort());
            } else if (protocol.contains("pop3")) {
                props.put("mail.pop3.host", emailEntity.getReceiveHost());
                props.put("mail.pop3.port", emailEntity.getReceivePort());
            } else {
                props.put("mail.imaps.host", emailEntity.getReceiveHost());
                props.put("mail.imaps.port", emailEntity.getReceivePort());
            }
//            props.put("mail.imap.ssl.enable", emailEntity.isReceiveSSL()+""); // 使用 SSL 连接
//            props.put("mail.imap.starttls.enable", "true");//// IMAP 协议设置 STARTTLS
 
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.ssl.enable", emailEntity.isSmtpSSL() + "");//启用SSL
            props.put("mail.smtp.host", emailEntity.getSmtpHost()); // 替换为你的SMTP服务器
            props.put("mail.smtp.port", emailEntity.getSmtpPort()); // 或者你使用的端口
 
//            props.put("mail.smtp.starttls.enable", "true");//启用tls
//            props.setProperty("mail.smtp.socketFactory.port", "587");//启用tls后port:587
 
 
            HashMap IAM = new HashMap();
            //带上IMAP ID信息,由key和value组成,例如name,version,vendor,support-email等。
            IAM.put("name", emailEntity.getReceiveEmail());
            IAM.put("version", emailEntity.getDocVersion() + "");
            IAM.put("vendor", emailEntity.getCompanyName());
            IAM.put("support-email", emailEntity.getEmail());
 
            //创建会话
            Session session = Session.getInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(emailEntity.getReceiveEmail(), emailEntity.getReceivePassword());
                }
            });
 
            //存储对象
            IMAPStore store = (IMAPStore) session.getStore(protocol);//imap协议或pop3协议类型(推荐你使用IMAP协议来存取服务器上的邮件。)
            //连接
            store.connect(emailEntity.getReceiveHost(), emailEntity.getReceiveEmail(), emailEntity.getReceivePassword());
            store.id(IAM);//163邮箱需要,不然会报:A3 NO SELECT Unsafe Login. Please contact kefu@188.com for help
            Folder folder = null;
            // 获得收件箱
            folder = store.getFolder("INBOX");
            // 以读写模式打开收件箱
            folder.open(Folder.READ_WRITE);
            folder.close(false);
            store.close();
 
            Transport transport = session.getTransport();
            transport.connect();
            transport.close();
            mailAccountIfc.updateMailStatus("正常", emailEntity.getAccountId());
        } catch (MessagingException e) {
            errorMsg = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
        } catch (Exception e) {
            errorMsg = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
        }
        return errorMsg;
    }
 
    /**
     * 常规查询
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/getConvention.do")
    public AllBackMsg getConvention(HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
 
        try {
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
 
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            //根据当前用户查询绑定的邮箱信息
 
        } catch (MessagingException e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * 常规更新
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/updateConvention.do")
    public AllBackMsg updateConvention(HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
 
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            //根据当前用户查询绑定的邮箱信息
 
        } catch (MessagingException e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    private T482102Entity getMailConfigProperties(T482102Entity t482102) {
        try {
            String email = t482102.getEmail();
            int index = email.lastIndexOf("@");
            String emailSuffix = email.substring(index + 1).toLowerCase();//邮箱后缀
            String port = "";
            t482102.setReceiveHost(EmailConfigUtils.getMailConfig("mail.imap.host." + emailSuffix));
            t482102.setSmtpHost(EmailConfigUtils.getMailConfig("mail.smtp.host." + emailSuffix));
            port = EmailConfigUtils.getMailConfig("mail.imap.port." + emailSuffix);
            if (StringUtils.isNotBlank(port)) {
                t482102.setReceivePort(Integer.parseInt(port));
            }
            port = EmailConfigUtils.getMailConfig("mail.smtp.port." + emailSuffix);
            if (StringUtils.isNotBlank(port)) {
                t482102.setSmtpPort(Integer.parseInt(port));
            }
        } catch (Exception e) {
            return t482102;
        }
        return t482102;
    }
}