⚡ 邮件发送优化
This commit is contained in:
parent
fdef53f2a5
commit
8f2cc923d9
@ -24,10 +24,12 @@ namespace Infrastructure
|
||||
/// </summary>
|
||||
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 Smtp { get; set; }
|
||||
public int Port { get; set; }
|
||||
public bool UseSsl { get; set; }
|
||||
public string Signature { get; set; }
|
||||
}
|
||||
/// <summary>
|
||||
|
||||
@ -70,15 +70,15 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
/// <param name="sendEmailVo">请求参数接收实体</param>
|
||||
/// <returns></returns>
|
||||
[ActionPermissionFilter(Permission = "tool:email:send")]
|
||||
[Log(Title = "发送邮件", IsSaveRequestData = false)]
|
||||
[Log(Title = "发送邮件")]
|
||||
[HttpPost]
|
||||
public IActionResult SendEmail([FromBody] SendEmailDto sendEmailVo)
|
||||
{
|
||||
if (sendEmailVo == null || string.IsNullOrEmpty(sendEmailVo.Subject) || string.IsNullOrEmpty(sendEmailVo.ToUser))
|
||||
if (sendEmailVo == null)
|
||||
{
|
||||
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($"请配置邮箱信息"));
|
||||
}
|
||||
@ -90,11 +90,11 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
{
|
||||
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 上传
|
||||
|
||||
@ -26,7 +26,7 @@ namespace ZR.Admin.WebApi.Extensions
|
||||
/// 初始化db
|
||||
/// </summary>
|
||||
/// <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>>();
|
||||
|
||||
|
||||
@ -18,8 +18,8 @@
|
||||
"urls": "http://localhost:8888", //项目启动url,如果改动端口前端对应devServer也需要进行修改
|
||||
"corsUrls": "http://localhost:8887", //跨域地址(前端启动项目,前后端分离单独部署需要设置),多个用","隔开
|
||||
"JwtSettings": {
|
||||
"Issuer": "ZRAdmin.NET",
|
||||
"Audience": "ZRAdmin.NET",
|
||||
"Issuer": "ZRAdmin.NET", //即token的签发者。
|
||||
"Audience": "ZRAdmin.NET", //指该token是服务于哪个群体的(群体范围)
|
||||
"SecretKey": "SecretKey-ZRADMIN.NET-20210101",
|
||||
"Expire": 1440 //jwt登录过期时间(分)
|
||||
},
|
||||
@ -60,8 +60,10 @@
|
||||
},
|
||||
//邮箱配置信息
|
||||
"MailOptions": {
|
||||
//发件人名称
|
||||
"FromName": "system",
|
||||
//发送人邮箱
|
||||
"From": "", //eg:xxxx@qq.com
|
||||
"FromEmail": "", //eg:xxxx@qq.com
|
||||
//发送人邮箱密码
|
||||
"Password": "123456",
|
||||
//协议
|
||||
|
||||
@ -14,50 +14,18 @@ namespace ZR.Common
|
||||
/// 发送人邮箱
|
||||
/// </summary>
|
||||
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();
|
||||
|
||||
public MailHelper()
|
||||
{
|
||||
AppSettings.Bind("MailOptions", mailOptions);
|
||||
FromEmail = mailOptions.From;
|
||||
Smtp = mailOptions.Smtp;
|
||||
FromPwd = mailOptions.Password;
|
||||
Port = mailOptions.Port;
|
||||
FromEmail= mailOptions.FromEmail;
|
||||
}
|
||||
public MailHelper(string fromEmail, string smtp, int port, string fromPwd)
|
||||
public MailHelper(MailOptions _mailOptions)
|
||||
{
|
||||
FromEmail = fromEmail;
|
||||
Smtp = smtp;
|
||||
FromPwd = fromPwd;
|
||||
Port = port;
|
||||
this.mailOptions = _mailOptions;
|
||||
FromEmail = _mailOptions.FromEmail;
|
||||
}
|
||||
|
||||
public MailHelper(string fromEmail, string fromPwd)
|
||||
{
|
||||
FromEmail = fromEmail;
|
||||
FromPwd = fromPwd;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送一个
|
||||
/// </summary>
|
||||
@ -65,13 +33,13 @@ namespace ZR.Common
|
||||
/// <param name="subject"></param>
|
||||
/// <param name="text"></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>() {
|
||||
new MailboxAddress(toAddress, toAddress)
|
||||
};
|
||||
|
||||
SendMail(mailboxes, subject, text, path, html);
|
||||
return SendMail(mailboxes, subject, text, path, html);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -81,7 +49,7 @@ namespace ZR.Common
|
||||
/// <param name="subject"></param>
|
||||
/// <param name="text"></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>() { };
|
||||
foreach (var item in toAddress)
|
||||
@ -89,7 +57,7 @@ namespace ZR.Common
|
||||
mailboxes.Add(new MailboxAddress(item, item));
|
||||
}
|
||||
|
||||
SendMail(mailboxes, subject, text, path, html);
|
||||
return SendMail(mailboxes, subject, text, path, html);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -100,16 +68,16 @@ namespace ZR.Common
|
||||
/// <param name="text"></param>
|
||||
/// <param name="path">附件url地址</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();
|
||||
//发件人
|
||||
message.From.Add(new MailboxAddress(FromEmail, FromEmail));
|
||||
message.From.Add(new MailboxAddress(mailOptions.FromName, mailOptions.FromEmail));
|
||||
//收件人
|
||||
message.To.AddRange(toAddress);
|
||||
message.Subject = subject;
|
||||
message.Date = DateTime.Now;
|
||||
|
||||
|
||||
//创建附件Multipart
|
||||
Multipart multipart = new Multipart("mixed");
|
||||
var alternative = new MultipartAlternative();
|
||||
@ -157,18 +125,18 @@ namespace ZR.Common
|
||||
//开始发送
|
||||
using var client = new SmtpClient();
|
||||
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中有编码问题,显示乱码了
|
||||
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);
|
||||
Console.WriteLine($"发送邮件成功{DateTime.Now}");
|
||||
Console.WriteLine($"【{DateTime.Now}】发送邮件结果{result}");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user