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);
|
}
|
}
|
}
|
}
|