using Microsoft.Extensions.Logging; using Newtonsoft.Json; using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using NLog; using DS.Module.Core.Helpers; using DS.WMS.Core.Op.Dtos; using DS.Module.Core; namespace DS.WMS.FeeBillRecvService { public class EmailNoticeHelper { #region 发送邮件(内部方法) /// /// 发送邮件(内部方法) /// /// 邮件详情列表 /// 返回回执 public async Task InnerSendEmailNotice(List emailList) { string emailUrl = string.Empty; CommonWebApiResult result = new CommonWebApiResult(); //ILogger logger //日志 NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger(); DateTime bDate = DateTime.Now; try { emailUrl = AppSetting.app(new string[] { "FeeSettings", "ExchangeName" }); //var res = await emailUrl.SetBody(emailList, "application/json").PostAsync(); var jsonBody = Newtonsoft.Json.JsonConvert.SerializeObject(emailList, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); var rlt = RequestHelper.Post(jsonBody, emailUrl); DateTime eDate = DateTime.Now; TimeSpan ts = eDate.Subtract(bDate); var timeDiff = ts.TotalMilliseconds; _logger.Log(NLog.LogLevel.Info, $"邮件上传完成 用时:{timeDiff}ms.发送邮件返回:{JsonConvert.SerializeObject(rlt)}"); if (!string.IsNullOrWhiteSpace(rlt)) { var respObj = JsonConvert.DeserializeAnonymousType(rlt, new { Success = false, Message = string.Empty, Code = -9999, }); result.succ = respObj.Success; result.msg = respObj.Message; } } catch (Exception ex) { _logger.Log(NLog.LogLevel.Info, $"发送邮件异常:{ex.Message}"); result.succ = false; result.msg = $"发送邮件异常:{ex.Message}"; } return result; } #endregion #region 发送邮件 /// /// 发送邮件 /// /// 标题 /// 内容 /// 接收人列表 /// public async Task SendEmailNotice(string title, string content, List userEmailList) { List list = new List(); if (userEmailList.Count == 0) return new CommonWebApiResult { succ = false }; EmailApiDto dto = new EmailApiDto { SendTo = string.Join(";", userEmailList.ToArray()), Title = title, Body = content, Attaches = new List(), }; list.Add(dto); return await InnerSendEmailNotice(list); } #endregion } public class EmailApiDto { public string SendTo { get; set; } //public string CCTo { get; set; } public string Title { get; set; } public string Body { get; set; } //public string ShowName { get; set; } public string SmtpConfig { get; set; } = "NOREPLAY"; //public string Account { get; set; } //public string Password { get; set; } //public string Server { get; set; } //public string Port { get; set; } //public string UseSSL { get; set; } public bool isCallback { get; set; } = false; public List Attaches { get; set; } } public class AttachesInfo { public string AttachName { get; set; } public string AttachContent { get; set; } } /// /// 通用WebApi返回回执 /// public class CommonWebApiResult { /// /// 是否成功 true=成功 false=失败 /// public bool succ { get; set; } = false; /// /// 状态 0-成功 /// public int status { get; set; } = 0; /// /// 返回消息 /// public string msg { get; set; } /// /// 总记录数 /// public int total { get; set; } /// /// 当前页列表数据 /// public object rows { get; set; } /// /// 合计信息 /// public object summary { get; set; } /// /// 扩展信息 /// public object extra { get; set; } /// /// 扩展信息2 /// public object extra2 { get; set; } } }