diff --git a/admin.net.pro/Admin.NET/Admin.NET.Core/Service/Message/SysEmailService.cs b/admin.net.pro/Admin.NET/Admin.NET.Core/Service/Message/SysEmailService.cs index 0cb0a19..37fbe10 100644 --- a/admin.net.pro/Admin.NET/Admin.NET.Core/Service/Message/SysEmailService.cs +++ b/admin.net.pro/Admin.NET/Admin.NET.Core/Service/Message/SysEmailService.cs @@ -17,11 +17,13 @@ public class SysEmailService : IDynamicApiController, ITransient { private readonly EmailOptions _emailOptions; private readonly SysConfigService _sysConfigService; + private readonly IServiceScopeFactory _scopeFactory; - public SysEmailService(IOptions emailOptions, SysConfigService sysConfigService) + public SysEmailService(IOptions emailOptions, SysConfigService sysConfigService, IServiceScopeFactory scopeFactory) { _emailOptions = emailOptions.Value; _sysConfigService = sysConfigService; + _scopeFactory = scopeFactory; } /// @@ -52,4 +54,53 @@ public class SysEmailService : IDynamicApiController, ITransient await Task.CompletedTask; } + + /// + /// 发送同步到sap失败邮件 📧 + /// + /// + /// + /// + [DisplayName("发送同步到sap失败邮件")] + public async Task SapSyncFailureSendEmail([Required] string content, string title = "") + { + using var serviceScope = _scopeFactory.CreateScope(); + var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("1300000000001").CopyNew(); + + var webTitle = await _sysConfigService.GetConfigValueByCode(ConfigConst.SysWebTitle); + title = string.IsNullOrWhiteSpace(title) ? $"{webTitle} 系统邮件" : title; + + var userEmail = await db.CopyNew().Queryable().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); + } + } + } \ No newline at end of file diff --git a/admin.net.pro/Admin.NET/Vistar.Application/Configuration/Email.json b/admin.net.pro/Admin.NET/Vistar.Application/Configuration/Email.json index bc53ba9..25c6298 100644 --- a/admin.net.pro/Admin.NET/Vistar.Application/Configuration/Email.json +++ b/admin.net.pro/Admin.NET/Vistar.Application/Configuration/Email.json @@ -3,12 +3,12 @@ "Email": { "Host": "smtp.163.com", // 主机 - "Port": 465, // 端口 465、994、25 - "EnableSsl": true, // 启用SSL - "DefaultFromEmail": "xxx@163.com", // 默认发件者邮箱 - "DefaultToEmail": "xxx@qq.com", // 默认接收人邮箱 - "UserName": "xxx@163.com", // 邮箱账号 - "Password": "", // 邮箱授权码 - "DefaultFromName": "Admin.NET 通用权限开发平台" // 默认邮件标题 + "Port": 25, // 端口 465、994、25 + "EnableSsl": false, // 启用SSL + "DefaultFromEmail": "nhybrb727@16", // 默认发件者邮箱 + "DefaultToEmail": "1257020195@qq.com", // 默认接收人邮箱 + "UserName": "nhybrb727@163.com", // 邮箱账号 + "Password": "DDcWFu8azNjik3uK", // 邮箱授权码 + "DefaultFromName": "Manage数据集成平台" // 默认邮件标题 } } \ No newline at end of file