|
|
|
|
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 发送邮件(内部方法)
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发送邮件(内部方法)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="emailList">邮件详情列表</param>
|
|
|
|
|
/// <returns>返回回执</returns>
|
|
|
|
|
public async Task<CommonWebApiResult> InnerSendEmailNotice(List<EmailApiDto> 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 发送邮件
|
|
|
|
|
/// <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>();
|
|
|
|
|
|
|
|
|
|
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 static class StringUtilsExtension
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取用户邮件列表
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">任意字符串</param>
|
|
|
|
|
/// <returns>返回用户邮件列表</returns>
|
|
|
|
|
public static List<string> GetUserEmailList(this string input)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
|
|
|
return new List<string>();
|
|
|
|
|
|
|
|
|
|
return input.Split(new char[] { ',' }).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|