实现附件发送

master
Hao 3 years ago
parent 0ea6197ab8
commit 2bc0a8f3a9

@ -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,9 +74,68 @@ namespace MailSend.Web.Controllers
mailSend.Body = item.Body;
mailSend.SmtpConfig = item.SmtpConfig;
mailSend.ShowName = item.ShowName;
//附件处理
if (item.Attaches.Count > 0)
{
var list = new List<AttachFileModel>();
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();
resp.Success = true;
@ -82,5 +144,12 @@ namespace MailSend.Web.Controllers
return Json(resp);
}
[HttpGet]
public ActionResult Test()
{
logger.Debug($"{DateTime.Now}");
return Content("ok");
}
}
}

@ -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();
}
}
}

@ -56,9 +56,22 @@
<HintPath>..\packages\log4net.2.0.14\lib\net45\log4net.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Extensions.Logging.Abstractions, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Extensions.Logging.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Quartz, Version=3.3.3.0, Culture=neutral, PublicKeyToken=f6b8c98a402cc8a4, processorArchitecture=MSIL">
<HintPath>..\packages\Quartz.3.3.3\lib\net461\Quartz.dll</HintPath>
</Reference>
<Reference Include="Quartz.Jobs, Version=3.3.3.0, Culture=neutral, PublicKeyToken=f6b8c98a402cc8a4, processorArchitecture=MSIL">
<HintPath>..\packages\Quartz.Jobs.3.3.3\lib\net461\Quartz.Jobs.dll</HintPath>
</Reference>
<Reference Include="Quartz.Plugins, Version=3.3.3.0, Culture=neutral, PublicKeyToken=f6b8c98a402cc8a4, processorArchitecture=MSIL">
<HintPath>..\packages\Quartz.Plugins.3.3.3\lib\net461\Quartz.Plugins.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
@ -133,6 +146,9 @@
<ItemGroup>
<Content Include="favicon.ico" />
<Content Include="Global.asax" />
<Content Include="quartz_jobs.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Web.config" />
<Content Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
@ -145,10 +161,11 @@
<Content Include="Views\Shared\Error.cshtml" />
<Content Include="Views\Shared\_Layout.cshtml" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="App_Data\" />
</ItemGroup>
<ItemGroup>
<Content Include="log4net.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>

@ -15,5 +15,13 @@ namespace MailSend.Web.Models
public string SmtpConfig { get; set; }
public List<ReqSendMailAttach> Attaches { get; set; }
}
public class ReqSendMailAttach
{
public string AttachName { get; set; }
public string AttachContent { get; set; }
}
}

@ -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)]

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
https://go.microsoft.com/fwlink/?LinkId=301880
@ -6,21 +6,23 @@
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
<connectionStrings>
<add name="MailDB" connectionString="Data Source=192.168.0.80;Initial Catalog=MailSend;Persist Security Info=True;User ID=sa;Password=sa123" providerName="System.Data.SqlClient" />
<add name="MailDB" connectionString="Data Source=192.168.0.80;Initial Catalog=MailSend;Persist Security Info=True;User ID=sa;Password=sa123" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<compilation debug="true" targetFramework="4.6.1"/>
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
@ -56,13 +58,27 @@
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
</compilers>
</system.codedom>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
<quartz>
<add key="quartz.scheduler.instanceName" value="ServerScheduler"/>
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
<add key="quartz.threadPool.threadCount" value="50"/>
<add key="quartz.threadPool.threadPriority" value="2"/>
<add key="quartz.jobStore.misfireThreshold" value="30000"/>
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
<add key="quartz.plugin.jobInitializer.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Plugins"/>
<add key="quartz.plugin.jobInitializer.fileNames" value="~/quartz_jobs.xml"/>
<add key="quartz.plugin.jobInitializer.failOnFileNotFound" value="true"/>
</quartz>
</configuration>

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" requirePermission="false" />
</configSections>
<log4net>
<root>
<!-- Value of priority may be ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
<level value="DEBUG" />
<appender-ref ref="DebugAppender" />
<appender-ref ref="ErrorAppender" />
</root>
<appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
<!-- debug log: 记录 DEBUG, INFO 级别的日志 -->
<file value="App_Data\\Logs\\debug.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout" >
<param name="ConversionPattern" value="%date [%p] [%logger] [%thread] - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMax value="INFO" />
</filter>
</appender>
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<!-- error log: 记录 WARN,ERROR,FATAL 级别的日志 -->
<file value="App_Data\\Logs\\error.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout" >
<param name="ConversionPattern" value="%date [%p] [%logger] [%thread] - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
</filter>
</appender>
</log4net>
</configuration>

@ -10,7 +10,11 @@
<package id="Microsoft.AspNet.WebPages" version="3.2.7" targetFramework="net461" />
<package id="Microsoft.AspNet.WebPages.zh-Hans" version="3.2.7" targetFramework="net461" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net461" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="2.1.1" targetFramework="net461" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
<package id="Modernizr" version="2.8.3" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
<package id="Quartz" version="3.3.3" targetFramework="net461" />
<package id="Quartz.Jobs" version="3.3.3" targetFramework="net461" />
<package id="Quartz.Plugins" version="3.3.3" targetFramework="net461" />
</packages>

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8" ?>
<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData">
<processing-directives>
<overwrite-existing-data>true</overwrite-existing-data>
</processing-directives>
<schedule>
<!-- 压缩日志 -->
<job>
<name>LogZipJob</name>
<group>System</group>
<description>压缩日志</description>
<job-type>MailSend.Common.LogZipJob,MailSend.Common</job-type>
<durable>true</durable>
<recover>false</recover>
</job>
<trigger>
<cron>
<name>LogZipJobScheduler</name>
<group>System</group>
<job-name>LogZipJob</job-name>
<job-group>System</job-group>
<cron-expression>0 30 0 * * ?</cron-expression>
</cron>
</trigger>
<!-- 清理临时文件 -->
<job>
<name>ClearTempJob</name>
<group>System</group>
<description>清理临时文件</description>
<job-type>MailSend.Common.ClearTempJob,MailSend.Common</job-type>
<durable>true</durable>
<recover>false</recover>
</job>
<trigger>
<simple>
<name>ClearTempJobScheduler</name>
<group>System</group>
<job-name>ClearTempJob</job-name>
<job-group>System</job-group>
<repeat-count>-1</repeat-count>
<repeat-interval>1800000</repeat-interval>
</simple>
</trigger>
</schedule>
</job-scheduling-data>
Loading…
Cancel
Save