自定义邮件发送

dev
嵇文龙 1 month ago
parent b5fb878673
commit ee9f528486

@ -58,7 +58,7 @@ public interface IUserEmailService
/// 获取用户邮箱配置
/// </summary>
/// <returns></returns>
public Task<DataResult<CodeUserEmailRes>> GetUserEmailSetInfo();
public Task<DataResult<CodeUserEmailRes>> GetCurrentUserEmailAsync();
/// <summary>

@ -115,8 +115,9 @@ public class CodeUserEmailService : ServiceBase, IUserEmailService
/// <returns></returns>
public DataResult<CodeUserEmailRes> GetUserEmailInfo(string id)
{
var int64Id = long.Parse(id);
var data = TenantDb.Queryable<CodeUserEmail>()
.Where(x => x.Id == long.Parse(id) && x.Status == StatusEnum.Enable)
.Where(x => x.Id == int64Id && x.Status == StatusEnum.Enable)
.Select<CodeUserEmailRes>()
.First();
return DataResult<CodeUserEmailRes>.Success(data, MultiLanguageConst.DataQuerySuccess);
@ -150,17 +151,18 @@ public class CodeUserEmailService : ServiceBase, IUserEmailService
/// 获取用户邮箱配置
/// </summary>
/// <returns></returns>
public async Task<DataResult<CodeUserEmailRes>> GetUserEmailSetInfo()
public async Task<DataResult<CodeUserEmailRes>> GetCurrentUserEmailAsync()
{
var userId = long.Parse(User.UserId);
var data = await TenantDb.Queryable<CodeUserEmail>()
.Where(a => a.IsCompany == false && a.CreateBy == long.Parse(User.UserId))
.Where(a => a.CreateBy == userId && !a.IsCompany)
.Select<CodeUserEmailRes>()
.FirstAsync();
if (data == null)
{
data = await TenantDb.Queryable<CodeUserEmail>()
.Where(a => a.IsCompany == true)
.Where(a => a.IsCompany)
.Select<CodeUserEmailRes>()
.FirstAsync();
}

@ -123,25 +123,25 @@ namespace DS.WMS.MainApi.Controllers
});
}
/// <summary>
/// 新增客户干系人
/// </summary>
/// <param name="clientId">客户ID</param>
/// <returns></returns>
[HttpPost, Route("Create")]
public async Task<DataResult> EditAsync(long clientId)
{
var date = DateTime.Now.Date;
return await _invokeService.EditAsync(new InfoClientStakeholder
{
ClientId = clientId,
CreateBy = long.Parse(_user.UserId),
CreateByName = _user.UserName,
StartDate = date,
EndDate = date.AddMonths(4),
Status = StakeholderStatus.Uncommitted
});
}
///// <summary>
///// 新增客户干系人
///// </summary>
///// <param name="clientId">客户ID</param>
///// <returns></returns>
//[HttpPost, Route("Create")]
//public async Task<DataResult> EditAsync(long clientId)
//{
// var date = DateTime.Now.Date;
// return await _invokeService.EditAsync(new InfoClientStakeholder
// {
// ClientId = clientId,
// CreateBy = long.Parse(_user.UserId),
// CreateByName = _user.UserName,
// StartDate = date,
// EndDate = date.AddMonths(4),
// Status = StakeholderStatus.Uncommitted
// });
//}
/// <summary>
/// 更新客户干系人

@ -1,6 +1,10 @@
using System.Data.Common;
using DS.Module.Core;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core;
using DS.WMS.Core.Code.Interface;
using DS.WMS.MainApi.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
@ -12,25 +16,29 @@ namespace DS.WMS.MainApi.Controllers
/// </summary>
public class MaintenanceController : ApiController
{
readonly ISqlSugarClient sugarClient;
readonly IConfiguration config;
readonly ApiFox api;
/// <summary>
/// 初始化
/// </summary>
/// <param name="sugarClient"></param>
public MaintenanceController(ISqlSugarClient sugarClient)
/// <param name="config"></param>
public MaintenanceController(IConfiguration config)
{
this.sugarClient = sugarClient;
this.config = config;
api = new ApiFox();
}
/// <summary>
/// 运行SQL脚本
/// </summary>
/// <param name="sugarClient"></param>
/// <param name="script">SQL脚本</param>
/// <param name="dbNames">运行的数据库,为空时执行全部租户库</param>
/// <returns></returns>
[HttpPost, Route("RunScript"), AllowAnonymous]
public async Task<IActionResult> RunScriptAsync([FromForm] string script, [FromQuery] params string[] dbNames)
public async Task<IActionResult> RunScriptAsync([FromServices] ISqlSugarClient sugarClient,
[FromForm] string script, [FromQuery] params string[] dbNames)
{
bool hasError = default;
var linkList = await sugarClient.Queryable<SysTenantLink>().ToListAsync();
@ -72,5 +80,65 @@ namespace DS.WMS.MainApi.Controllers
return hasError ? StatusCode(500) : Ok();
}
/// <summary>
/// 发送自定义邮件
/// </summary>
/// <param name="userEmailService"></param>
/// <param name="user"></param>
/// <param name="model">邮件发送模型</param>
/// <returns></returns>
[HttpPost, Route("SendMail")]
public async Task<DataResult> SendMailAsync([FromServices] IUserEmailService userEmailService, [FromServices] IUser user,
[FromBody] SendMailModel model)
{
if (string.IsNullOrEmpty(config["MailConfig:ApiUrl"]))
return DataResult.Failed("请联系站点管理员配置邮件服务URL");
var userEmail = await userEmailService.GetCurrentUserEmailAsync();
if (!userEmail.Succeeded || userEmail.Data == null)
return DataResult.Failed("你尚未配置邮箱设置,请到系统设置中维护后重试此操作");
if (model.Attachments?.Length > 0)
{
for (int i = 0; i < model.Attachments.Length; i++)
{
var attachment = model.Attachments[i];
var response = await api.SendRequestAsync(HttpMethod.Get, attachment.Url);
if (!response.IsSuccessStatusCode)
return DataResult.Failed("无法从下列地址中获取文件作为附件:" + attachment.Url);
var bytes = await response.Content.ReadAsByteArrayAsync();
if (bytes == null || bytes.Length == 0)
return DataResult.Failed("下列地址中获取的文件大小为零,无法生成附件:" + attachment.Url);
attachment.Base64 = Convert.ToBase64String(bytes);
if (string.IsNullOrEmpty(attachment.FileName))
attachment.FileName = Path.GetFileName(attachment.Url);
}
}
string sendTo = string.Join(",", model.Receivers);
dynamic[] mailParams = [new
{
SendTo = sendTo,
model.Title,
Body = model.Content,
ShowName = string.IsNullOrEmpty(userEmail.Data.ShowName) ? userEmail.Data.MailAccount : userEmail.Data.ShowName,
Account = userEmail.Data.MailAccount,
userEmail.Data.Password,
Server = userEmail.Data.SmtpServer,
Port = userEmail.Data.SmtpPort,
UseSSL = userEmail.Data.SmtpSSL.GetValueOrDefault(),
Attaches = model.Attachments?.Select(x => new
{
AttachName = x.FileName,
AttachContent = x.Base64
}).ToList()
}];
var mailResult = await api.SendRequestAsync(HttpMethod.Post, config["MailConfig:ApiUrl"]!, mailParams);
return mailResult.IsSuccessStatusCode ? DataResult.Success : DataResult.Failed("发送失败,邮件服务返回状态码为错误");
}
}
}

@ -129,7 +129,7 @@ public class UserEmailController : ApiController
[Route("GetUserEmailSetInfo")]
public async Task<DataResult<CodeUserEmailRes>> GetUserEmailSetInfo()
{
var res = await _invokeService.GetUserEmailSetInfo();
var res = await _invokeService.GetCurrentUserEmailAsync();
return res;
}
}

@ -0,0 +1,49 @@
namespace DS.WMS.MainApi.Models
{
/// <summary>
/// 自定义邮件发送模型
/// </summary>
public class SendMailModel
{
/// <summary>
/// 邮件标题
/// </summary>
public string? Title { get; set; }
/// <summary>
/// 邮件正文
/// </summary>
public string? Content { get; set; }
/// <summary>
/// 邮件接收人
/// </summary>
public string[]? Receivers { get; set; }
/// <summary>
/// 附件
/// </summary>
public Attachment[]? Attachments { get; set; }
}
/// <summary>
/// 邮件附件
/// </summary>
public class Attachment
{
/// <summary>
/// 附件文件名
/// </summary>
public string? FileName { get; set; }
/// <summary>
/// 附件URL
/// </summary>
public string Url { get; set; } = string.Empty;
/// <summary>
/// 附件的BASE64字符串表示
/// </summary>
public string? Base64 { get; set; }
}
}

@ -63,5 +63,15 @@
},
"RedisInfo": {
"RedisConfig": "127.0.0.1:6379,password=,defaultDatabase=15"
},
"MailConfig": {
"ApiUrl": "http://47.104.73.97:8801/mail/send",
"DefaultSetting": {
"Account": "lyle@yyts.fun",
"Password": "15275215387jZ",
"Host": "smtp.mxhichina.com",
"Port": 465,
"UseSSL": true
}
}
}
Loading…
Cancel
Save