80 lines
2.6 KiB
C#
80 lines
2.6 KiB
C#
using FluentFTP;
|
||
using YLErp.MailKit;
|
||
using YLErp.Modules.AppModule;
|
||
|
||
namespace YLErp.Helpers
|
||
{
|
||
/// <summary>
|
||
/// 邮件发送帮助类
|
||
/// </summary>
|
||
public class EmailHelper
|
||
{
|
||
static readonly Dictionary<string, DateTime> _dic;
|
||
|
||
static EmailHelper()
|
||
{
|
||
_dic = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);
|
||
}
|
||
|
||
public static string SendMailOfTradeConfirmBook(string mailTo, IEnumerable<string> filesToAttach, string title, string body, bool isHtml = false)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(title))
|
||
{
|
||
title = "交易确认书";
|
||
}
|
||
if (string.IsNullOrWhiteSpace(body))
|
||
{
|
||
body = "请查看附件交易确认书";
|
||
}
|
||
|
||
try
|
||
{
|
||
return SendMail(mailTo, title, body, isHtml, filesToAttach);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogFactory.GetLogger().Error("邮件发送失败:" + title, ex);
|
||
return ex.Message;
|
||
}
|
||
}
|
||
|
||
public static string SendMail(string mailTo, string subject, string body, bool isBodyHtml = true, IEnumerable<string> filesToAttach = null, string ccEmail = null, string mailFrom = null)
|
||
{
|
||
if (string.IsNullOrEmpty(mailTo))
|
||
{
|
||
throw new ArgumentException("不能为空", nameof(mailTo));
|
||
}
|
||
|
||
return doSendMail(mailTo, subject, body, isBodyHtml, filesToAttach, ccEmail);
|
||
}
|
||
private static string doSendMail( string mailTo, string subject, string body, bool isBodyHtml, IEnumerable<string> filesToAttach, string ccEmail)
|
||
{
|
||
try
|
||
{
|
||
//去重
|
||
var toSet = mailTo.Split(new[] { ';', ',', ',' }, StringSplitOptions.RemoveEmptyEntries)
|
||
.ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||
|
||
var mailToArr = toSet.ToArray();
|
||
|
||
MailSender.SendApi(new MailSendingOption
|
||
{
|
||
MailTo = mailToArr,
|
||
Subject = subject,
|
||
Body = body,
|
||
IsBodyHtml = isBodyHtml,
|
||
FilesToAttach = filesToAttach,
|
||
CC = ccEmail
|
||
});
|
||
|
||
return string.Empty;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogFactory.GetLogger("邮件发送").Error("邮件发送失败:" + subject, ex);
|
||
|
||
return ex.Message;
|
||
}
|
||
}
|
||
}
|
||
} |