You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.6 KiB
C#

3 years ago
using log4net;
using MailSend.Common;
using MailSend.Common.Models;
using MailSend.Web.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace MailSend.Web.Controllers
{
public class MailController : Controller
{
private static ILog logger = LogManager.GetLogger("MailController");
private MailDataContext mailData = new MailDataContext();
[HttpPost]
public ActionResult SendBatch()
{
var resp = new RespCommon();
StreamReader sr = new StreamReader(Request.InputStream, Encoding.UTF8);
var strJson = sr.ReadToEnd();
if (string.IsNullOrEmpty(strJson))
{
resp.Success = false;
resp.Code = RespCommon.RespCodeParamError;
resp.Message = "参数错误";
return Json(resp);
}
var smtpList = mailData.MailSendSmtp.AsNoTracking()
.Select(x => x.GID)
.ToList();
List<ReqSendMail> listToSend = null;
try
{
listToSend = JsonConvert.DeserializeObject<List<ReqSendMail>>(strJson);
}
catch
{
logger.Error($"JSON格式有误解析失败{strJson}");
resp.Success = false;
resp.Code = RespCommon.RespCodeParamError;
resp.Message = "JSON格式有误解析失败";
return Json(resp);
}
foreach (var item in listToSend)
{
if (!smtpList.Contains(item.SmtpConfig))
{
resp.Success = false;
resp.Code = RespCommon.RespCodeParamError;
resp.Message = "未找到SmtpConfig的配置";
return Json(resp);
}
var mailSend = new Common.MailSend();
mailSend.GID = Guid.NewGuid().ToString().Replace("-", "");
mailSend.SendTo = item.SendTo;
mailSend.CCTo = item.CCTo;
mailSend.Title = item.Title;
mailSend.Body = item.Body;
mailSend.SmtpConfig = item.SmtpConfig;
mailSend.ShowName = item.ShowName;
mailData.MailSend.Add(mailSend);
}
mailData.SaveChanges();
resp.Success = true;
resp.Code = RespCommon.RespCodeSuccess;
resp.Message = "提交成功";
return Json(resp);
}
}
}