From 2bc0a8f3a9a685466cbaf13a44b8f36b44dbc1ce Mon Sep 17 00:00:00 2001 From: Hao Date: Fri, 4 Mar 2022 14:07:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=99=84=E4=BB=B6=E5=8F=91?= =?UTF-8?q?=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MailSend.Web/Controllers/MailController.cs | 73 +++++++++++++++++++++- MailSend.Web/Global.asax.cs | 4 ++ MailSend.Web/MailSend.Web.csproj | 23 ++++++- MailSend.Web/Models/ReqModel.cs | 8 +++ MailSend.Web/Properties/AssemblyInfo.cs | 1 + MailSend.Web/Web.config | 42 +++++++++---- MailSend.Web/log4net.config | 46 ++++++++++++++ MailSend.Web/packages.config | 4 ++ MailSend.Web/quartz_jobs.xml | 50 +++++++++++++++ 9 files changed, 233 insertions(+), 18 deletions(-) create mode 100644 MailSend.Web/log4net.config create mode 100644 MailSend.Web/quartz_jobs.xml diff --git a/MailSend.Web/Controllers/MailController.cs b/MailSend.Web/Controllers/MailController.cs index 40e493a..ed87327 100644 --- a/MailSend.Web/Controllers/MailController.cs +++ b/MailSend.Web/Controllers/MailController.cs @@ -19,8 +19,9 @@ namespace MailSend.Web.Controllers private MailDataContext mailData = new MailDataContext(); + //发送邮件 [HttpPost] - public ActionResult SendBatch() + public ActionResult Send() { var resp = new RespCommon(); @@ -34,6 +35,8 @@ namespace MailSend.Web.Controllers return Json(resp); } + logger.Debug($"收到邮件发送请求:{strJson}"); + var smtpList = mailData.MailSendSmtp.AsNoTracking() .Select(x => x.GID) .ToList(); @@ -71,7 +74,66 @@ namespace MailSend.Web.Controllers mailSend.Body = item.Body; mailSend.SmtpConfig = item.SmtpConfig; mailSend.ShowName = item.ShowName; - mailData.MailSend.Add(mailSend); + + //附件处理 + if (item.Attaches.Count > 0) + { + var list = new List(); + foreach (var att in item.Attaches) + { + if (string.IsNullOrEmpty(att.AttachName)) + { + resp.Success = false; + resp.Code = RespCommon.RespCodeParamError; + resp.Message = $"附件名称不能为空"; + return Json(resp); + } + + if (string.IsNullOrEmpty(att.AttachContent)) + { + resp.Success = false; + resp.Code = RespCommon.RespCodeParamError; + resp.Message = $"附件内容不能为空:{att.AttachName}"; + return Json(resp); + } + + try + { + var bsArr = Convert.FromBase64String(att.AttachContent); + var ext = Path.GetExtension(att.AttachName); + var fileTmpName = DateTime.Now.Ticks.ToString(); + var tempPath = Server.MapPath("~/Temp"); + if (!Directory.Exists(tempPath)) + { + Directory.CreateDirectory(tempPath); + } + + var fileFullPath = Path.Combine(tempPath, fileTmpName + ext); + System.IO.File.WriteAllBytes(fileFullPath, bsArr); + + var attModel = new AttachFileModel(); + attModel.FileName = att.AttachName; + attModel.FilePath = fileFullPath; + + list.Add(attModel); + } + catch (Exception ex) + { + logger.Error($"附件还原失败:{ex.Message}"); + logger.Error(ex.StackTrace); + + resp.Success = false; + resp.Code = RespCommon.RespCodeParamError; + resp.Message = $"附件还原失败:{att.AttachName}"; + return Json(resp); + } + } + + mailSend.AttachFiles = JsonConvert.SerializeObject(list); + + mailData.MailSend.Add(mailSend); + } + } mailData.SaveChanges(); @@ -82,5 +144,12 @@ namespace MailSend.Web.Controllers return Json(resp); } + + [HttpGet] + public ActionResult Test() + { + logger.Debug($"{DateTime.Now}"); + return Content("ok"); + } } } \ No newline at end of file diff --git a/MailSend.Web/Global.asax.cs b/MailSend.Web/Global.asax.cs index 680c7c3..0a3aff0 100644 --- a/MailSend.Web/Global.asax.cs +++ b/MailSend.Web/Global.asax.cs @@ -1,3 +1,4 @@ +using Quartz.Impl; using System; using System.Collections.Generic; using System.Linq; @@ -14,6 +15,9 @@ namespace MailSend.Web AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); + + var scheduler = StdSchedulerFactory.GetDefaultScheduler().Result; + scheduler.Start(); } } } diff --git a/MailSend.Web/MailSend.Web.csproj b/MailSend.Web/MailSend.Web.csproj index dcdaa9b..6d89eab 100644 --- a/MailSend.Web/MailSend.Web.csproj +++ b/MailSend.Web/MailSend.Web.csproj @@ -56,9 +56,22 @@ ..\packages\log4net.2.0.14\lib\net45\log4net.dll + + ..\packages\Microsoft.Extensions.Logging.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll + + + ..\packages\Quartz.3.3.3\lib\net461\Quartz.dll + + + ..\packages\Quartz.Jobs.3.3.3\lib\net461\Quartz.Jobs.dll + + + ..\packages\Quartz.Plugins.3.3.3\lib\net461\Quartz.Plugins.dll + + @@ -133,6 +146,9 @@ + + PreserveNewest + Web.config @@ -145,10 +161,11 @@ + - - - + + PreserveNewest + diff --git a/MailSend.Web/Models/ReqModel.cs b/MailSend.Web/Models/ReqModel.cs index b9d02ce..f6edecd 100644 --- a/MailSend.Web/Models/ReqModel.cs +++ b/MailSend.Web/Models/ReqModel.cs @@ -15,5 +15,13 @@ namespace MailSend.Web.Models public string SmtpConfig { get; set; } + + public List Attaches { get; set; } + } + + public class ReqSendMailAttach + { + public string AttachName { get; set; } + public string AttachContent { get; set; } } } \ No newline at end of file diff --git a/MailSend.Web/Properties/AssemblyInfo.cs b/MailSend.Web/Properties/AssemblyInfo.cs index 8297f9a..b1b6de6 100644 --- a/MailSend.Web/Properties/AssemblyInfo.cs +++ b/MailSend.Web/Properties/AssemblyInfo.cs @@ -33,3 +33,4 @@ using System.Runtime.InteropServices; // 方法是按如下所示使用 "*": [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)] diff --git a/MailSend.Web/Web.config b/MailSend.Web/Web.config index d338d4d..9845e0e 100644 --- a/MailSend.Web/Web.config +++ b/MailSend.Web/Web.config @@ -1,4 +1,4 @@ - + -
+
+
+ - - - - + + + + - + - - + + @@ -56,13 +58,27 @@ - - + + - + - \ No newline at end of file + + + + + + + + + + + + + + + diff --git a/MailSend.Web/log4net.config b/MailSend.Web/log4net.config new file mode 100644 index 0000000..bddff14 --- /dev/null +++ b/MailSend.Web/log4net.config @@ -0,0 +1,46 @@ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MailSend.Web/packages.config b/MailSend.Web/packages.config index cef0feb..3eb095b 100644 --- a/MailSend.Web/packages.config +++ b/MailSend.Web/packages.config @@ -10,7 +10,11 @@ + + + + \ No newline at end of file diff --git a/MailSend.Web/quartz_jobs.xml b/MailSend.Web/quartz_jobs.xml new file mode 100644 index 0000000..eb9a257 --- /dev/null +++ b/MailSend.Web/quartz_jobs.xml @@ -0,0 +1,50 @@ + + + + true + + + + + + LogZipJob + System + 压缩日志 + MailSend.Common.LogZipJob,MailSend.Common + true + false + + + + + LogZipJobScheduler + System + LogZipJob + System + 0 30 0 * * ? + + + + + + ClearTempJob + System + 清理临时文件 + MailSend.Common.ClearTempJob,MailSend.Common + true + false + + + + + ClearTempJobScheduler + System + ClearTempJob + System + -1 + 1800000 + + + + + \ No newline at end of file