DeviceManager/TsSFCDevice.Client.Launch/Common/Email/EmailSend.cs

153 lines
5.5 KiB
C#
Raw Normal View History

2024-08-02 02:52:45 +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 MimeKit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace TsSFCDevice.Client.Launch.Common.Email
{
public class EmailSend
{
#region Fields & Property
#endregion
#region Ctor
public EmailSend()
{
}
#endregion
#region Methods
public async void SendEmail(string cTitle, string cMessage, string cEmailAddressTo, string cEMailAddressCC, IList<string> lstAttachments, bool bRework = false)
{
try
{
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)
{
}
else
{
if (result.StartsWith("邮件发送成功"))
{
}
}
return result;
});
#endregion
}
catch (Exception ex)
{
}
}
#endregion
}
}