|
|
|
@ -62,22 +62,6 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
ArgumentNullException.ThrowIfNull(mailConfig, nameof(mailConfig));
|
|
|
|
|
ArgumentNullException.ThrowIfNull(templateModel, nameof(templateModel));
|
|
|
|
|
|
|
|
|
|
string title, content = string.Empty;
|
|
|
|
|
var razorEngine = new RazorEngine();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string key1 = mailConfig.Id + "-" + nameof(mailConfig.Title);
|
|
|
|
|
title = await RenderTemplateAsync(key1, mailConfig.Title, templateModel, razorEngine);
|
|
|
|
|
|
|
|
|
|
string key2 = mailConfig.Id + "-" + nameof(mailConfig.Content);
|
|
|
|
|
content = await RenderTemplateAsync(key2, mailConfig.Content, templateModel, razorEngine);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await ex.LogAsync(Db);
|
|
|
|
|
return DataResult.Failed($"渲染邮件模板({mailConfig.Id})时出错,请检查模板是否有语法错误");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var order = await TenantDb.Queryable<SeaExport>().Where(x => x.Id == templateModel.BusinessId)
|
|
|
|
|
.Select(x => new
|
|
|
|
|
{
|
|
|
|
@ -108,27 +92,17 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
else if (mailConfig.Sender.IsVouchingClerk)
|
|
|
|
|
senderId = order.Doc;
|
|
|
|
|
|
|
|
|
|
var sender = await Db.Queryable<SysUser>().Where(x => x.Id == senderId).Select(x => new { x.UserName, x.SignatureHtml }).FirstAsync();
|
|
|
|
|
if (sender == null)
|
|
|
|
|
templateModel.Sender = await Db.Queryable<SysUser>().Where(x => x.Id == senderId).Select(x => new MailSender
|
|
|
|
|
{
|
|
|
|
|
DisplayName = x.UserName,
|
|
|
|
|
SignatureHtml = x.SignatureHtml
|
|
|
|
|
}).FirstAsync();
|
|
|
|
|
if (templateModel.Sender == null)
|
|
|
|
|
return DataResult.Failed("未设置发件人");
|
|
|
|
|
|
|
|
|
|
var senderConfig = await TenantDb.Queryable<CodeUserEmail>().FirstAsync(x => x.CreateBy == senderId);
|
|
|
|
|
if (senderConfig == null)
|
|
|
|
|
return DataResult.Failed($"发件人用户:{sender.UserName} 未设置SMTP发件信息");
|
|
|
|
|
|
|
|
|
|
//插入发件人签名
|
|
|
|
|
var htmlDoc = new HtmlDocument();
|
|
|
|
|
htmlDoc.LoadHtml(content);
|
|
|
|
|
var node = htmlDoc.GetElementbyId("sign");
|
|
|
|
|
if (node != null)
|
|
|
|
|
node.InnerHtml = sender.SignatureHtml;
|
|
|
|
|
|
|
|
|
|
using (StringWriter writer = new())
|
|
|
|
|
{
|
|
|
|
|
htmlDoc.Save(writer);
|
|
|
|
|
content = writer.ToString();
|
|
|
|
|
writer.Close();
|
|
|
|
|
}
|
|
|
|
|
return DataResult.Failed($"发件人用户:{templateModel.Sender.DisplayName} 未设置SMTP发件信息");
|
|
|
|
|
|
|
|
|
|
//设置抄送人
|
|
|
|
|
List<long> ccIds = [];
|
|
|
|
@ -162,9 +136,39 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
if (mailConfig.Receiver.IsController)
|
|
|
|
|
receiverTypes.Add("controller");
|
|
|
|
|
|
|
|
|
|
var receiverList = await TenantDb.Queryable<BusinessOrderContact>().Where(x => receiverTypes.Contains(x.CustomerType)
|
|
|
|
|
templateModel.Receivers = await TenantDb.Queryable<BusinessOrderContact>().Where(x => receiverTypes.Contains(x.CustomerType)
|
|
|
|
|
&& x.Email != null && x.Email != string.Empty && x.BusinessId == templateModel.BusinessId && x.BusinessType == templateModel.BusinessType)
|
|
|
|
|
.Select(x => new { x.Name, x.Email }).ToListAsync();
|
|
|
|
|
.Select(x => new MailReceiver { DisplayName = x.Name, MailAddress = x.Email }).ToListAsync();
|
|
|
|
|
|
|
|
|
|
string title, content = string.Empty;
|
|
|
|
|
var razorEngine = new RazorEngine();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string key1 = mailConfig.Id + "-" + nameof(mailConfig.Title);
|
|
|
|
|
title = await RenderTemplateAsync(key1, mailConfig.Title, templateModel, razorEngine);
|
|
|
|
|
|
|
|
|
|
string key2 = mailConfig.Id + "-" + nameof(mailConfig.Content);
|
|
|
|
|
content = await RenderTemplateAsync(key2, mailConfig.Content, templateModel, razorEngine);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await ex.LogAsync(Db);
|
|
|
|
|
return DataResult.Failed($"渲染邮件模板({mailConfig.Id})时出错,请检查模板是否有语法错误");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//插入发件人签名
|
|
|
|
|
var htmlDoc = new HtmlDocument();
|
|
|
|
|
htmlDoc.LoadHtml(content);
|
|
|
|
|
var node = htmlDoc.GetElementbyId("sign");
|
|
|
|
|
if (node != null)
|
|
|
|
|
node.InnerHtml = templateModel.Sender.SignatureHtml;
|
|
|
|
|
|
|
|
|
|
using (StringWriter writer = new())
|
|
|
|
|
{
|
|
|
|
|
htmlDoc.Save(writer);
|
|
|
|
|
content = writer.ToString();
|
|
|
|
|
writer.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var attachmentList = mailConfig.Attachments == null ? [] : new List<Tuple<string, string>>(mailConfig.Attachments.Count);
|
|
|
|
|
try
|
|
|
|
@ -222,13 +226,14 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string sendTo = string.Join(",", templateModel.Receivers.Select(x => x.MailAddress));
|
|
|
|
|
dynamic[] mailParams = [new
|
|
|
|
|
{
|
|
|
|
|
SendTo = string.Join(",", receiverList.Select(x => x.Email)),
|
|
|
|
|
SendTo = sendTo,
|
|
|
|
|
Title = title,
|
|
|
|
|
Body = content,
|
|
|
|
|
CCTo = string.Join(",", ccList),
|
|
|
|
|
ShowName = senderConfig.ShowName.IsNullOrEmpty() ? sender.UserName : senderConfig.ShowName,
|
|
|
|
|
ShowName = senderConfig.ShowName.IsNullOrEmpty() ? templateModel.Sender.DisplayName : senderConfig.ShowName,
|
|
|
|
|
Account = senderConfig.MailAccount,
|
|
|
|
|
senderConfig.Password,
|
|
|
|
|
Server = senderConfig.SmtpServer,
|
|
|
|
@ -245,11 +250,11 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
if (mailResult.Data.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
return DataResult.Successed($"已发邮件(委托单:{order.CustomerNo}),收件人:"
|
|
|
|
|
+ string.Join(",", receiverList.Select(x => x.Email)) + "发件人:" + senderConfig.MailAccount);
|
|
|
|
|
+ string.Join(",", sendTo) + "发件人:" + senderConfig.MailAccount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DataResult.Failed($"邮件发送失败,API返回状态为:{(int)mailResult.Data.StatusCode} {mailResult.Data.StatusCode}"
|
|
|
|
|
+ $"(委托单:{order.CustomerNo}),收件人:" + string.Join(",", receiverList.Select(x => x.Email)) + "发件人:" + senderConfig.MailAccount);
|
|
|
|
|
+ $"(委托单:{order.CustomerNo}),收件人:" + string.Join(",", sendTo) + "发件人:" + senderConfig.MailAccount);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|