😎发送同步到sap失败邮件服务
This commit is contained in:
parent
e5f2374772
commit
bead0e6483
@ -17,11 +17,13 @@ public class SysEmailService : IDynamicApiController, ITransient
|
|||||||
{
|
{
|
||||||
private readonly EmailOptions _emailOptions;
|
private readonly EmailOptions _emailOptions;
|
||||||
private readonly SysConfigService _sysConfigService;
|
private readonly SysConfigService _sysConfigService;
|
||||||
|
private readonly IServiceScopeFactory _scopeFactory;
|
||||||
|
|
||||||
public SysEmailService(IOptions<EmailOptions> emailOptions, SysConfigService sysConfigService)
|
public SysEmailService(IOptions<EmailOptions> emailOptions, SysConfigService sysConfigService, IServiceScopeFactory scopeFactory)
|
||||||
{
|
{
|
||||||
_emailOptions = emailOptions.Value;
|
_emailOptions = emailOptions.Value;
|
||||||
_sysConfigService = sysConfigService;
|
_sysConfigService = sysConfigService;
|
||||||
|
_scopeFactory = scopeFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -52,4 +54,53 @@ public class SysEmailService : IDynamicApiController, ITransient
|
|||||||
|
|
||||||
await Task.CompletedTask;
|
await Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送同步到sap失败邮件 📧
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="content"></param>
|
||||||
|
/// <param name="title"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[DisplayName("发送同步到sap失败邮件")]
|
||||||
|
public async Task SapSyncFailureSendEmail([Required] string content, string title = "")
|
||||||
|
{
|
||||||
|
using var serviceScope = _scopeFactory.CreateScope();
|
||||||
|
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("1300000000001").CopyNew();
|
||||||
|
|
||||||
|
var webTitle = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SysWebTitle);
|
||||||
|
title = string.IsNullOrWhiteSpace(title) ? $"{webTitle} 系统邮件" : title;
|
||||||
|
|
||||||
|
var userEmail = await db.CopyNew().Queryable<SysUser>().Where(x => x.Status == StatusEnum.Enable && !string.IsNullOrEmpty(x.Email)).ToListAsync();
|
||||||
|
|
||||||
|
using var client = new SmtpClient();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client.Connect(_emailOptions.Host, _emailOptions.Port, _emailOptions.EnableSsl);
|
||||||
|
client.Authenticate(_emailOptions.UserName, _emailOptions.Password);
|
||||||
|
|
||||||
|
foreach (var item in userEmail)
|
||||||
|
{
|
||||||
|
var message = new MimeMessage();
|
||||||
|
message.From.Add(new MailboxAddress(_emailOptions.DefaultFromEmail, _emailOptions.DefaultFromEmail));
|
||||||
|
message.To.Add(new MailboxAddress(item.Email, item.Email));
|
||||||
|
message.Subject = title;
|
||||||
|
message.Body = new TextPart("html")
|
||||||
|
{
|
||||||
|
Text = content
|
||||||
|
};
|
||||||
|
|
||||||
|
await client.SendAsync(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// 记录日志或进行其他处理
|
||||||
|
Console.WriteLine($"邮件发送失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
client.Disconnect(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -3,12 +3,12 @@
|
|||||||
|
|
||||||
"Email": {
|
"Email": {
|
||||||
"Host": "smtp.163.com", // 主机
|
"Host": "smtp.163.com", // 主机
|
||||||
"Port": 465, // 端口 465、994、25
|
"Port": 25, // 端口 465、994、25
|
||||||
"EnableSsl": true, // 启用SSL
|
"EnableSsl": false, // 启用SSL
|
||||||
"DefaultFromEmail": "xxx@163.com", // 默认发件者邮箱
|
"DefaultFromEmail": "nhybrb727@16", // 默认发件者邮箱
|
||||||
"DefaultToEmail": "xxx@qq.com", // 默认接收人邮箱
|
"DefaultToEmail": "1257020195@qq.com", // 默认接收人邮箱
|
||||||
"UserName": "xxx@163.com", // 邮箱账号
|
"UserName": "nhybrb727@163.com", // 邮箱账号
|
||||||
"Password": "", // 邮箱授权码
|
"Password": "DDcWFu8azNjik3uK", // 邮箱授权码
|
||||||
"DefaultFromName": "Admin.NET 通用权限开发平台" // 默认邮件标题
|
"DefaultFromName": "Manage数据集成平台" // 默认邮件标题
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user