|
|
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 Send()
|
|
|
{
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
logger.Debug($"收到邮件发送请求:{strJson}");
|
|
|
|
|
|
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 (string.IsNullOrEmpty(item.SmtpConfig) || !smtpList.Contains(item.SmtpConfig))
|
|
|
{
|
|
|
resp.Success = false;
|
|
|
resp.Code = RespCommon.RespCodeParamError;
|
|
|
resp.Message = "SmtpConfig无效";
|
|
|
return Json(resp);
|
|
|
}
|
|
|
|
|
|
if (string.IsNullOrEmpty(item.SendTo))
|
|
|
{
|
|
|
resp.Success = false;
|
|
|
resp.Code = RespCommon.RespCodeParamError;
|
|
|
resp.Message = "接收人不能为空";
|
|
|
return Json(resp);
|
|
|
}
|
|
|
|
|
|
if (string.IsNullOrEmpty(item.Title))
|
|
|
{
|
|
|
resp.Success = false;
|
|
|
resp.Code = RespCommon.RespCodeParamError;
|
|
|
resp.Message = "标题不能为空";
|
|
|
return Json(resp);
|
|
|
}
|
|
|
|
|
|
if (string.IsNullOrEmpty(item.Body))
|
|
|
{
|
|
|
resp.Success = false;
|
|
|
resp.Code = RespCommon.RespCodeParamError;
|
|
|
resp.Message = "正文不能为空";
|
|
|
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;
|
|
|
|
|
|
//附件处理
|
|
|
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;
|
|
|
resp.Code = RespCommon.RespCodeSuccess;
|
|
|
resp.Message = "提交成功";
|
|
|
|
|
|
return Json(resp);
|
|
|
}
|
|
|
|
|
|
[HttpGet]
|
|
|
public ActionResult Test()
|
|
|
{
|
|
|
logger.Debug($"{DateTime.Now}");
|
|
|
return Content("ok");
|
|
|
}
|
|
|
}
|
|
|
} |