songjun
2024-09-04 cc908053e0b5075fbfd27350b6da4b39bca9e550
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using PalGain.Core;
using PalGain.Core.Tasks;
using BatchService.Framework.Utility;
using sbcLabSystem.Data.Domain.Backstage;
using sbcLabSystem.Data.Domain.Account;
using sbcLabSystem.Service.Account;
using sbcLabSystem.Service.QC;
using sbcLabSystem.Service.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
namespace sbcLabSystem.Service.Task
{
    public class SendEmailTaskService : ITask
    {
        private delegate void SentMailProcess(EmileInfo mailInfo, EmailSmtpInfo smtpInfo);
        private QCService _qcService;
        private AccountService _accountService;
 
        public SendEmailTaskService(QCService i_qcService,
            AccountService i_accountService)
        {
            _qcService = i_qcService;
            _accountService = i_accountService;
        }
 
        public void Execute()
        {
            try
            {
                LogHelper.Debug(LogHelper.LoggerType.ScheduleTask, "准备发送邮件");
                EmailSmtpInfo smtpInfo = _accountService.GetDefaultSmtpInfo();
                _accountService.GetUnSentEmails().ToList().ForEach(x =>
                {
                    SentMailProcess process = new SentMailProcess(SentProcess);
                    process.BeginInvoke(x, smtpInfo, null, null);
                    Thread.Sleep(10 * 1000);
                });
                LogHelper.Debug(LogHelper.LoggerType.ScheduleTask, 
                    string.Format("共计发送邮件:{0}", _accountService.GetUnSentEmails().Count()));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.ToString());
            }
        }
 
        private void SentProcess(EmileInfo mailInfo, EmailSmtpInfo smtpInfo)
        {
            Email emailSender = new Email();
            emailSender.isbodyHtml = true;
            emailSender.host = smtpInfo.Host;
            emailSender.IsEnableSSL = smtpInfo.IsEnableSSL;
            emailSender.SSLPort = smtpInfo.SSLPort;
            emailSender.mailBody = mailInfo.Boody;
            emailSender.mailFrom = smtpInfo.UserEmail;
            emailSender.mailPwd = smtpInfo.Password;
            emailSender.mailToArray = new string[] { mailInfo.EmileName };
            emailSender.mailSubject = mailInfo.Title;
 
            LogHelper.Debug(LogHelper.LoggerType.ScheduleTask,
                string.Format("发送邮件给:{0},发件人:{1},主题:{2},内容:{3},序列化为:{4}",
                emailSender.mailToArray[0],
                emailSender.mailFrom,
                emailSender.mailSubject,
                emailSender.mailBody,
                JsonConvert.SerializeObject(emailSender)));
 
            bool sendStatus = false;
            try
            {
                sendStatus = emailSender.Send();
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.ToString()); 
                mailInfo.Column1 = ex.Message; 
                _accountService.SetErrorMessageToEmailSentBox(mailInfo);
                
            } 
 
            LogHelper.Debug(LogHelper.LoggerType.ScheduleTask,
                string.Format("发送状态:{0},收件人:{1}",
                sendStatus.ToString(),
                emailSender.mailToArray[0]));
            if (sendStatus)
            {
                _accountService.SetSentStatusToEmailSentBox(mailInfo);
            }
        }
    }
}