feat: 增加邮件发送测试工具

This commit is contained in:
Argo Zhang 2020-02-22 13:57:44 +08:00
parent 2c49fbff42
commit 70af25a63f
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
4 changed files with 101 additions and 2 deletions

View File

@ -1,7 +1,10 @@
using System.Threading.Tasks;
using Bootstrap.Client.Extensions;
using Bootstrap.Client.Models;
using Longbow.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
namespace Bootstrap.Client
{
@ -11,7 +14,6 @@ namespace Bootstrap.Client
[Authorize]
public class ToolsController : Controller
{
/// <summary>
/// SQL 视图
/// </summary>
@ -23,6 +25,58 @@ namespace Bootstrap.Client
return View(new SQLModel(this));
}
/// <summary>
/// SQL 视图
/// </summary>
/// <returns></returns>
[Authorize(Roles = "Administrators")]
[HttpGet]
public IActionResult Mail()
{
return View(new MailModel(this));
}
/// <summary>
///
/// </summary>
/// <param name="config"></param>
/// <param name="auth"></param>
/// <returns></returns>
[HttpPost]
[AutoValidateAntiforgeryToken]
public async Task<IActionResult> Mail([FromServices]IConfiguration config, string auth)
{
if (Longbow.Security.Cryptography.LgbCryptography.ComputeHash(auth, "l9w+7loytBzNHYkKjGzpWzbhYpU7kWZenT1OeZxkor28wQJQ") != "/oEQLKLccvHA+MsDwCwmgaKddR0IEcOy9KgBmFsHXRs=")
{
// 授权码不正确拒绝执行
return View(new MailModel(this) { Result = "授权码不正确" });
}
else
{
var section = config.GetSection("MailClient");
var smtpHost = section.GetValue("Host", "smtp.163.com");
var password = section.GetValue("Password", "");
var from = section.GetValue("From", "");
var to = section.GetValue("To", "");
var port = section.GetValue("Port", 25);
var enableSsl = section.GetValue("EnableSsl", false);
var smtpMessage = new SmtpMessage()
{
Host = smtpHost,
Password = password,
From = from,
To = to,
Port = port,
EnableSsl = enableSsl,
Title = "Send Mail Test",
Message = "Email from Bootstrap Admin Master"
};
var result = await smtpMessage.SendAsync();
return View(new MailModel(this) { Result = result ? "发送成功" : "发送失败" });
}
}
/// <summary>
/// SQL 视图
/// </summary>

View File

@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Mvc;
namespace Bootstrap.Client.Models
{
/// <summary>
/// Mail Model
/// </summary>
public class MailModel : NavigatorBarModel
{
/// <summary>
/// 构造函数
/// </summary>
public MailModel(ControllerBase controller) : base(controller)
{
}
/// <summary>
/// 获得执行结果
/// </summary>
public string Result { get; set; } = "";
}
}

View File

@ -0,0 +1,14 @@
@model MailModel
@{
ViewData["Title"] = "邮件测试工具";
}
<form method="post">
<div class="form-group">
<div class="input-group">
<input type="password" name="auth" class="form-control" autocomplete="off" />
<div class="input-group-append">
<button type="submit" class="btn btn-danger"><i class="fa fa-exclamation-triangle" aria-hidden="true"></i><span>执行 (@Model.Result)</span></button>
</div>
</div>
</div>
</form>

View File

@ -43,12 +43,20 @@
},
"SmtpClient": {
"Host": "smtp.163.com",
"Port": 587,
"Port": 994,
"EnableSsl": true,
"Password": "",
"From": "honeywell_mes@163.com",
"To": "argo@163.com"
},
"MailClient": {
"Host": "smtp.exmail.qq.com",
"Port": 587,
"EnableSsl": true,
"Password": "!Honeywell1",
"From": "ba-support@dsnixi.com",
"To": "argo@163.com"
},
"AllowOrigins": "http://localhost,http://argo.zylweb.cn",
"LongbowCache": {
"Enabled": true,