DeviceManager/TsSFCDeivceClient/Email/EmailSend.cs

161 lines
6.1 KiB
C#
Raw Normal View History

2024-05-29 01:56:37 +00:00
/*---------------------------------------------------------------------
* Copyright (C) 2024 TECHSCAN
* www.techscan.cn
*
*  
*                
*
*
* EmailSend.cs
* N/A
* 26950
* CLR版本 4.0.30319.42000
* 2024/5/24 12:47:40
* Client MAYONGLONG
* V1.0.0
==============================================================
* Ver
* V1.0.0 2024/5/24 12:47:40 Myl Create
*
======================================================================
//--------------------------------------------------------------------*/
using log4net;
using MimeKit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TsSFCDeivceClient.Email
{
public class EmailSend
{
#region Fields & Property
private ILog m_log;
#endregion
#region Ctor
public EmailSend()
{
m_log = log4net.LogManager.GetLogger((System.Reflection.MethodBase.GetCurrentMethod().DeclaringType));
}
#endregion
#region Methods
public async void SendEmail(string cTitle, string cMessage, string cEmailAddressTo, string cEMailAddressCC, IList<string> lstAttachments, bool bRework = false)
{
try
{
m_log.Info($"开始发送外协{(bRework ? "" : "")}发货清单邮件.");
MailKitHelp vEMail = new MailKitHelp();
// mail.medtronic.com
vEMail.SmtpAddress = EmailSendParams.DefaultParams.SmtpAddress;
// false
vEMail.IsSSL = EmailSendParams.DefaultParams.IsSSL;
// 25
vEMail.SmtpPort = EmailSendParams.DefaultParams.SmtpPort;
//是否启用无密码模式
if (EmailSendParams.DefaultParams.EmailNoPass)
{
vEMail.IsUseDefaultCredentials = true;
}
else
{
vEMail.PassWord = EmailSendParams.DefaultParams.Password;
}
// rs.czopssfc@medtronic.com
vEMail.FromMailAddress = EmailSendParams.DefaultParams.FromMailAddress;
//收件人
vEMail.ToMailAddress = cEmailAddressTo.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
//抄送
if (!string.IsNullOrEmpty(cEMailAddressCC))
{
vEMail.IsCC = true;
vEMail.CarbonCopy = cEMailAddressCC.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
}
vEMail.IsBodyHtml = true;
#region
var rs = await Task.Run(() =>
{
#region
if (lstAttachments != null && lstAttachments.Any())
{
// 清除历史邮件
if (vEMail.Attachments != null)
vEMail.Attachments.Clear();
vEMail.IsSendAttachments = true;
foreach (string item in lstAttachments)
{
MimePart attachment = new MimePart()
{
//读取文件,只能用绝对路径
Content = new MimeContent(File.OpenRead(item), ContentEncoding.Default),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
//文件名字
//FileName = Path.GetFileNameWithoutExtension(item),
FileName = Path.GetFileName(item),
};
if (vEMail.Attachments == null)
{
vEMail.Attachments = new List<MimePart>();
}
vEMail.Attachments.Add(attachment);
}
}
#endregion
vEMail.Body = vEMail.GetHtmlInfoString(cMessage);
if (!string.IsNullOrEmpty(cTitle))
{
vEMail.Title = $"外协{(bRework ? "" : "")}订单(供应商:{cTitle})发货通知";
}
else
{
vEMail.Title = $"外协{(bRework ? "" : "")}订单发货通知";
}
string result = "";
var vSendStatus = vEMail.Send(out result);
if (vSendStatus != MailKitHelp.SendStatus.Success)
{
m_log.Error($"外协{(bRework ? "" : "")}订单发货通知邮件发送失败:{result}");
}
else
{
if (result.StartsWith("邮件发送成功"))
{
m_log.Info($"外协{(bRework ? "" : "")}订单发货通知邮件发送成功.");
}
}
return result;
});
#endregion
}
catch (Exception ex)
{
m_log.Error($"外协{(bRework ? "" : "")}订单发货通知邮件发送时异常:{ex.Message}", ex);
}
}
#endregion
}
}