using Furion.JsonSerialization;
using Furion.RemoteRequest.Extensions;
using Furion;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Furion.Logging;
using Microsoft.Extensions.Logging;
namespace Myshipping.Application.Helper
{
public class EmailNoticeHelper
{
#region 发送邮件(内部方法)
///
/// 发送邮件(内部方法)
///
/// 邮件详情列表
/// 返回回执
public async Task InnerSendEmailNotice(List emailList)
{
string emailUrl = string.Empty;
CommonWebApiResult result = new CommonWebApiResult();
//日志
var _logger = Log.CreateLogger(nameof(EmailNoticeHelper));
DateTime bDate = DateTime.Now;
try
{
emailUrl = App.Configuration["EmailNoticeUrl"];
var res = await emailUrl.SetBody(emailList, "application/json").PostAsync();
DateTime eDate = DateTime.Now;
TimeSpan ts = eDate.Subtract(bDate);
var timeDiff = ts.TotalMilliseconds;
_logger.LogInformation($"邮件上传完成 用时:{timeDiff}ms.发送邮件返回:{JSON.Serialize(res)}");
if (res != null && res.StatusCode == System.Net.HttpStatusCode.OK)
{
var userResult = await res.Content.ReadAsStringAsync();
var respObj = JsonConvert.DeserializeAnonymousType(userResult, new
{
Success = false,
Message = string.Empty,
Code = -9999,
});
result.succ = respObj.Success;
result.msg = respObj.Message;
}
}
catch (Exception ex)
{
_logger.LogInformation($"发送邮件异常:{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();
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 static class StringUtilsExtension
{
///
/// 获取用户邮件列表
///
/// 任意字符串
/// 返回用户邮件列表
public static List GetUserEmailList(this string input)
{
if (string.IsNullOrWhiteSpace(input))
return new List();
return input.Split(new char[] { ',' }).ToList();
}
}
}