⚡ 邮件发送优化
This commit is contained in:
parent
fdef53f2a5
commit
8f2cc923d9
@ -24,10 +24,12 @@ namespace Infrastructure
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class MailOptions
|
public class MailOptions
|
||||||
{
|
{
|
||||||
public string From { get; set; }
|
public string FromName { get; set; }
|
||||||
|
public string FromEmail { get; set; }
|
||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
public string Smtp { get; set; }
|
public string Smtp { get; set; }
|
||||||
public int Port { get; set; }
|
public int Port { get; set; }
|
||||||
|
public bool UseSsl { get; set; }
|
||||||
public string Signature { get; set; }
|
public string Signature { get; set; }
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -70,15 +70,15 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
/// <param name="sendEmailVo">请求参数接收实体</param>
|
/// <param name="sendEmailVo">请求参数接收实体</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[ActionPermissionFilter(Permission = "tool:email:send")]
|
[ActionPermissionFilter(Permission = "tool:email:send")]
|
||||||
[Log(Title = "发送邮件", IsSaveRequestData = false)]
|
[Log(Title = "发送邮件")]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public IActionResult SendEmail([FromBody] SendEmailDto sendEmailVo)
|
public IActionResult SendEmail([FromBody] SendEmailDto sendEmailVo)
|
||||||
{
|
{
|
||||||
if (sendEmailVo == null || string.IsNullOrEmpty(sendEmailVo.Subject) || string.IsNullOrEmpty(sendEmailVo.ToUser))
|
if (sendEmailVo == null)
|
||||||
{
|
{
|
||||||
return ToResponse(ApiResult.Error($"请求参数不完整"));
|
return ToResponse(ApiResult.Error($"请求参数不完整"));
|
||||||
}
|
}
|
||||||
if (string.IsNullOrEmpty(OptionsSetting.MailOptions.From) || string.IsNullOrEmpty(OptionsSetting.MailOptions.Password))
|
if (string.IsNullOrEmpty(OptionsSetting.MailOptions.FromEmail) || string.IsNullOrEmpty(OptionsSetting.MailOptions.Password))
|
||||||
{
|
{
|
||||||
return ToResponse(ApiResult.Error($"请配置邮箱信息"));
|
return ToResponse(ApiResult.Error($"请配置邮箱信息"));
|
||||||
}
|
}
|
||||||
@ -90,11 +90,11 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
{
|
{
|
||||||
toUsers.Append(mailHelper.FromEmail);
|
toUsers.Append(mailHelper.FromEmail);
|
||||||
}
|
}
|
||||||
mailHelper.SendMail(toUsers, sendEmailVo.Subject, sendEmailVo.Content, sendEmailVo.FileUrl, sendEmailVo.HtmlContent);
|
string result = mailHelper.SendMail(toUsers, sendEmailVo.Subject, sendEmailVo.Content, sendEmailVo.FileUrl, sendEmailVo.HtmlContent);
|
||||||
|
|
||||||
logger.Info($"发送邮件{JsonConvert.SerializeObject(sendEmailVo)}");
|
logger.Info($"发送邮件{JsonConvert.SerializeObject(sendEmailVo)}, 结果{result}");
|
||||||
|
|
||||||
return SUCCESS(true);
|
return SUCCESS(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region 上传
|
#region 上传
|
||||||
|
|||||||
@ -26,7 +26,7 @@ namespace ZR.Admin.WebApi.Extensions
|
|||||||
/// 初始化db
|
/// 初始化db
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="Configuration"></param>
|
/// <param name="Configuration"></param>
|
||||||
public static void AddDb(IConfiguration Configuration)
|
public static void AddDb(this IServiceCollection services, IConfiguration Configuration)
|
||||||
{
|
{
|
||||||
List<DbConfigs> dbConfigs = Configuration.GetSection("DbConfigs").Get<List<DbConfigs>>();
|
List<DbConfigs> dbConfigs = Configuration.GetSection("DbConfigs").Get<List<DbConfigs>>();
|
||||||
|
|
||||||
|
|||||||
@ -18,8 +18,8 @@
|
|||||||
"urls": "http://localhost:8888", //项目启动url,如果改动端口前端对应devServer也需要进行修改
|
"urls": "http://localhost:8888", //项目启动url,如果改动端口前端对应devServer也需要进行修改
|
||||||
"corsUrls": "http://localhost:8887", //跨域地址(前端启动项目,前后端分离单独部署需要设置),多个用","隔开
|
"corsUrls": "http://localhost:8887", //跨域地址(前端启动项目,前后端分离单独部署需要设置),多个用","隔开
|
||||||
"JwtSettings": {
|
"JwtSettings": {
|
||||||
"Issuer": "ZRAdmin.NET",
|
"Issuer": "ZRAdmin.NET", //即token的签发者。
|
||||||
"Audience": "ZRAdmin.NET",
|
"Audience": "ZRAdmin.NET", //指该token是服务于哪个群体的(群体范围)
|
||||||
"SecretKey": "SecretKey-ZRADMIN.NET-20210101",
|
"SecretKey": "SecretKey-ZRADMIN.NET-20210101",
|
||||||
"Expire": 1440 //jwt登录过期时间(分)
|
"Expire": 1440 //jwt登录过期时间(分)
|
||||||
},
|
},
|
||||||
@ -60,8 +60,10 @@
|
|||||||
},
|
},
|
||||||
//邮箱配置信息
|
//邮箱配置信息
|
||||||
"MailOptions": {
|
"MailOptions": {
|
||||||
|
//发件人名称
|
||||||
|
"FromName": "system",
|
||||||
//发送人邮箱
|
//发送人邮箱
|
||||||
"From": "", //eg:xxxx@qq.com
|
"FromEmail": "", //eg:xxxx@qq.com
|
||||||
//发送人邮箱密码
|
//发送人邮箱密码
|
||||||
"Password": "123456",
|
"Password": "123456",
|
||||||
//协议
|
//协议
|
||||||
|
|||||||
@ -14,50 +14,18 @@ namespace ZR.Common
|
|||||||
/// 发送人邮箱
|
/// 发送人邮箱
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string FromEmail { get; set; } = "";
|
public string FromEmail { get; set; } = "";
|
||||||
/// <summary>
|
|
||||||
/// 发送人密码
|
|
||||||
/// </summary>
|
|
||||||
public string FromPwd { get; set; } = "";
|
|
||||||
/// <summary>
|
|
||||||
/// 发送协议
|
|
||||||
/// </summary>
|
|
||||||
public string Smtp { get; set; } = "smtp.qq.com";
|
|
||||||
/// <summary>
|
|
||||||
/// 协议端口
|
|
||||||
/// </summary>
|
|
||||||
public int Port { get; set; } = 587;
|
|
||||||
/// <summary>
|
|
||||||
/// 邮件签名
|
|
||||||
/// </summary>
|
|
||||||
public string Signature { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 是否使用SSL协议
|
|
||||||
/// </summary>
|
|
||||||
public bool UseSsl { get; set; } = false;
|
|
||||||
private readonly MailOptions mailOptions = new();
|
private readonly MailOptions mailOptions = new();
|
||||||
|
|
||||||
public MailHelper()
|
public MailHelper()
|
||||||
{
|
{
|
||||||
AppSettings.Bind("MailOptions", mailOptions);
|
AppSettings.Bind("MailOptions", mailOptions);
|
||||||
FromEmail = mailOptions.From;
|
FromEmail= mailOptions.FromEmail;
|
||||||
Smtp = mailOptions.Smtp;
|
|
||||||
FromPwd = mailOptions.Password;
|
|
||||||
Port = mailOptions.Port;
|
|
||||||
}
|
}
|
||||||
public MailHelper(string fromEmail, string smtp, int port, string fromPwd)
|
public MailHelper(MailOptions _mailOptions)
|
||||||
{
|
{
|
||||||
FromEmail = fromEmail;
|
this.mailOptions = _mailOptions;
|
||||||
Smtp = smtp;
|
FromEmail = _mailOptions.FromEmail;
|
||||||
FromPwd = fromPwd;
|
|
||||||
Port = port;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public MailHelper(string fromEmail, string fromPwd)
|
|
||||||
{
|
|
||||||
FromEmail = fromEmail;
|
|
||||||
FromPwd = fromPwd;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送一个
|
/// 发送一个
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -65,13 +33,13 @@ namespace ZR.Common
|
|||||||
/// <param name="subject"></param>
|
/// <param name="subject"></param>
|
||||||
/// <param name="text"></param>
|
/// <param name="text"></param>
|
||||||
/// <param name="path"></param>
|
/// <param name="path"></param>
|
||||||
public void SendMail(string toAddress, string subject, string text, string path = "", string html = "")
|
public string SendMail(string toAddress, string subject, string text, string path = "", string html = "")
|
||||||
{
|
{
|
||||||
IEnumerable<MailboxAddress> mailboxes = new List<MailboxAddress>() {
|
IEnumerable<MailboxAddress> mailboxes = new List<MailboxAddress>() {
|
||||||
new MailboxAddress(toAddress, toAddress)
|
new MailboxAddress(toAddress, toAddress)
|
||||||
};
|
};
|
||||||
|
|
||||||
SendMail(mailboxes, subject, text, path, html);
|
return SendMail(mailboxes, subject, text, path, html);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -81,7 +49,7 @@ namespace ZR.Common
|
|||||||
/// <param name="subject"></param>
|
/// <param name="subject"></param>
|
||||||
/// <param name="text"></param>
|
/// <param name="text"></param>
|
||||||
/// <param name="path"></param>
|
/// <param name="path"></param>
|
||||||
public void SendMail(string[] toAddress, string subject, string text, string path = "", string html = "")
|
public string SendMail(string[] toAddress, string subject, string text, string path = "", string html = "")
|
||||||
{
|
{
|
||||||
IList<MailboxAddress> mailboxes = new List<MailboxAddress>() { };
|
IList<MailboxAddress> mailboxes = new List<MailboxAddress>() { };
|
||||||
foreach (var item in toAddress)
|
foreach (var item in toAddress)
|
||||||
@ -89,7 +57,7 @@ namespace ZR.Common
|
|||||||
mailboxes.Add(new MailboxAddress(item, item));
|
mailboxes.Add(new MailboxAddress(item, item));
|
||||||
}
|
}
|
||||||
|
|
||||||
SendMail(mailboxes, subject, text, path, html);
|
return SendMail(mailboxes, subject, text, path, html);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -100,11 +68,11 @@ namespace ZR.Common
|
|||||||
/// <param name="text"></param>
|
/// <param name="text"></param>
|
||||||
/// <param name="path">附件url地址</param>
|
/// <param name="path">附件url地址</param>
|
||||||
/// <param name="html">网页HTML内容</param>
|
/// <param name="html">网页HTML内容</param>
|
||||||
private void SendMail(IEnumerable<MailboxAddress> toAddress, string subject, string text, string path = "", string html = "")
|
private string SendMail(IEnumerable<MailboxAddress> toAddress, string subject, string text, string path = "", string html = "")
|
||||||
{
|
{
|
||||||
MimeMessage message = new MimeMessage();
|
MimeMessage message = new MimeMessage();
|
||||||
//发件人
|
//发件人
|
||||||
message.From.Add(new MailboxAddress(FromEmail, FromEmail));
|
message.From.Add(new MailboxAddress(mailOptions.FromName, mailOptions.FromEmail));
|
||||||
//收件人
|
//收件人
|
||||||
message.To.AddRange(toAddress);
|
message.To.AddRange(toAddress);
|
||||||
message.Subject = subject;
|
message.Subject = subject;
|
||||||
@ -157,18 +125,18 @@ namespace ZR.Common
|
|||||||
//开始发送
|
//开始发送
|
||||||
using var client = new SmtpClient();
|
using var client = new SmtpClient();
|
||||||
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
||||||
|
//client.CheckCertificateRevocation = false;
|
||||||
|
client.Connect(mailOptions.Smtp, mailOptions.Port, mailOptions.UseSsl);
|
||||||
|
|
||||||
//Smtp服务器
|
|
||||||
//client.Connect("smtp.qq.com", 587, false);
|
|
||||||
client.Connect(Smtp, Port, true);
|
|
||||||
//登录,发送
|
//登录,发送
|
||||||
//特别说明,对于服务器端的中文相应,Exception中有编码问题,显示乱码了
|
//特别说明,对于服务器端的中文相应,Exception中有编码问题,显示乱码了
|
||||||
client.Authenticate(FromEmail, FromPwd);
|
client.Authenticate(System.Text.Encoding.UTF8, mailOptions.FromEmail, mailOptions.Password);
|
||||||
|
|
||||||
client.Send(message);
|
var result = client.Send(message);
|
||||||
//断开
|
//断开
|
||||||
client.Disconnect(true);
|
client.Disconnect(true);
|
||||||
Console.WriteLine($"发送邮件成功{DateTime.Now}");
|
Console.WriteLine($"【{DateTime.Now}】发送邮件结果{result}");
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user