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.

188 lines
5.3 KiB
C#

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 发送邮件(内部方法)
/// <summary>
/// 发送邮件(内部方法)
/// </summary>
/// <param name="emailList">邮件详情列表</param>
/// <returns>返回回执</returns>
public async Task<CommonWebApiResult> InnerSendEmailNotice(List<EmailApiDto> emailList)
{
string emailUrl = string.Empty;
CommonWebApiResult result = new CommonWebApiResult();
//ILogger<RecvFeeBillWorker> 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 发送邮件
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="title">标题</param>
/// <param name="content">内容</param>
/// <param name="userEmailList">接收人列表</param>
/// <returns></returns>
public async Task<CommonWebApiResult> SendEmailNotice(string title, string content, List<string> userEmailList)
{
List<EmailApiDto> list = new List<EmailApiDto>();
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<AttachesInfo>(),
};
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<AttachesInfo> Attaches { get; set; }
}
public class AttachesInfo
{
public string AttachName { get; set; }
public string AttachContent { get; set; }
}
/// <summary>
/// 通用WebApi返回回执
/// </summary>
public class CommonWebApiResult
{
/// <summary>
/// 是否成功 true=成功 false=失败
/// </summary>
public bool succ { get; set; } = false;
/// <summary>
/// 状态 0-成功
/// </summary>
public int status { get; set; } = 0;
/// <summary>
/// 返回消息
/// </summary>
public string msg { get; set; }
/// <summary>
/// 总记录数
/// </summary>
public int total { get; set; }
/// <summary>
/// 当前页列表数据
/// </summary>
public object rows { get; set; }
/// <summary>
/// 合计信息
/// </summary>
public object summary { get; set; }
/// <summary>
/// 扩展信息
/// </summary>
public object extra { get; set; }
/// <summary>
/// 扩展信息2
/// </summary>
public object extra2 { get; set; }
}
}