DeviceManager/TsSFCDeivceClient/Email/EmailSend.cs
2024-05-29 09:56:37 +08:00

161 lines
6.1 KiB
C#
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*---------------------------------------------------------------------
* 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
}
}