DigitalFactory/Admin.NET/Admin.NET.Core/Service/Message/SysEmailService.cs

49 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 大名科技(天津)有限公司版权所有 电话18020030720 QQ515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
using MailKit.Net.Smtp;
using MimeKit;
namespace Admin.NET.Core.Service;
/// <summary>
/// 系统邮件发送服务
/// </summary>
[ApiDescriptionSettings(Order = 370)]
public class SysEmailService : IDynamicApiController, ITransient
{
private readonly EmailOptions _emailOptions;
public SysEmailService(IOptions<EmailOptions> emailOptions)
{
_emailOptions = emailOptions.Value;
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="content"></param>
/// <param name="title"></param>
/// <returns></returns>
[DisplayName("发送邮件")]
public async Task SendEmail([Required] string content, string title = "Admin.NET 系统邮件")
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress(_emailOptions.DefaultFromEmail, _emailOptions.DefaultFromEmail));
message.To.Add(new MailboxAddress(_emailOptions.DefaultToEmail, _emailOptions.DefaultToEmail));
message.Subject = title;
message.Body = new TextPart("html")
{
Text = content
};
using var client = new SmtpClient();
client.Connect(_emailOptions.Host, _emailOptions.Port, _emailOptions.EnableSsl);
client.Authenticate(_emailOptions.UserName, _emailOptions.Password);
client.Send(message);
client.Disconnect(true);
await Task.CompletedTask;
}
}