1+ using Microsoft . AspNetCore . Mvc ;
2+ using Microsoft . Extensions . Options ;
3+ using ZR . Admin . WebApi . Filters ;
4+ using ZR . Model . Dto ;
5+ using ZR . ServiceCore . Model ;
6+ using ZR . ServiceCore . Services ;
7+
8+ //创建时间:2023-11-20
9+ namespace ZR . Admin . WebApi . Controllers . Email
10+ {
11+ /// <summary>
12+ /// 邮件发送记录
13+ /// </summary>
14+ [ Verify ]
15+ [ Route ( "system/EmailLog" ) ]
16+ [ ApiExplorerSettings ( GroupName = "sys" ) ]
17+ public class EmailLogController : BaseController
18+ {
19+ /// <summary>
20+ /// 邮件发送记录接口
21+ /// </summary>
22+ private readonly IEmailLogService _EmailLogService ;
23+ private OptionsSetting OptionsSetting ;
24+ public EmailLogController (
25+ IEmailLogService EmailLogService ,
26+ IOptions < OptionsSetting > options )
27+ {
28+ _EmailLogService = EmailLogService ;
29+ OptionsSetting = options . Value ;
30+ }
31+
32+ /// <summary>
33+ /// 查询邮件发送记录列表
34+ /// </summary>
35+ /// <param name="parm"></param>
36+ /// <returns></returns>
37+ [ HttpGet ( "list" ) ]
38+ [ ActionPermissionFilter ( Permission = "emaillog:list" ) ]
39+ public IActionResult QueryEmailLog ( [ FromQuery ] EmailLogQueryDto parm )
40+ {
41+ var response = _EmailLogService . GetList ( parm ) ;
42+ return SUCCESS ( response ) ;
43+ }
44+
45+ /// <summary>
46+ /// 查询邮件发送记录详情
47+ /// </summary>
48+ /// <param name="Id"></param>
49+ /// <returns></returns>
50+ [ HttpGet ( "{Id}" ) ]
51+ [ ActionPermissionFilter ( Permission = "emaillog:query" ) ]
52+ public IActionResult GetEmailLog ( long Id )
53+ {
54+ var response = _EmailLogService . GetInfo ( Id ) ;
55+
56+ var info = response . Adapt < EmailLogDto > ( ) ;
57+ return SUCCESS ( info ) ;
58+ }
59+
60+ /// <summary>
61+ /// 添加邮件发送记录
62+ /// </summary>
63+ /// <returns></returns>
64+ [ HttpPost ( "sendEmail" ) ]
65+ [ ActionPermissionFilter ( Permission = "tool:email:send" ) ]
66+ [ Log ( Title = "邮件发送" , BusinessType = BusinessType . INSERT ) ]
67+ public IActionResult SendEmail ( [ FromBody ] EmailLogDto dto )
68+ {
69+ if ( dto . IdArr . Length <= 0 ) { return ToResponse ( ApiResult . Error ( $ "发送失败Id 不能为空") ) ; }
70+ int count = 0 ;
71+ foreach ( var item in dto . IdArr )
72+ {
73+ var response = _EmailLogService . GetInfo ( item ) ;
74+ if ( response ? . IsSend == 0 )
75+ {
76+ MailHelper mailHelper = new ( ) ;
77+ string [ ] toUsers = response . ToEmails . Split ( "," , StringSplitOptions . RemoveEmptyEntries ) ;
78+
79+ string result = mailHelper . SendMail ( toUsers , response . Subject , "" , response . FileUrl , response . EmailContent ) ;
80+ count += _EmailLogService . Update ( x => x . Id == item , x => new EmailLog ( )
81+ {
82+ IsSend = 1 ,
83+ SendTime = DateTime . Now ,
84+ SendResult = result
85+ } ) ;
86+ }
87+ }
88+ return SUCCESS ( count ) ;
89+ }
90+
91+ /// <summary>
92+ /// 删除邮件发送记录
93+ /// </summary>
94+ /// <returns></returns>
95+ [ HttpDelete ( "{ids}" ) ]
96+ [ ActionPermissionFilter ( Permission = "emaillog:delete" ) ]
97+ [ Log ( Title = "邮件发送记录" , BusinessType = BusinessType . DELETE ) ]
98+ public IActionResult DeleteEmailLog ( string ids )
99+ {
100+ long [ ] idsArr = Tools . SpitLongArrary ( ids ) ;
101+ if ( idsArr . Length <= 0 ) { return ToResponse ( ApiResult . Error ( $ "删除失败Id 不能为空") ) ; }
102+
103+ var response = _EmailLogService . Delete ( idsArr ) ;
104+
105+ return ToResponse ( response ) ;
106+ }
107+
108+ /// <summary>
109+ /// 发送邮件
110+ /// </summary>
111+ /// <param name="sendEmailVo">请求参数接收实体</param>
112+ /// <returns></returns>
113+ [ ActionPermissionFilter ( Permission = "tool:email:send" ) ]
114+ [ Log ( Title = "发送邮件" ) ]
115+ [ HttpPost ( "/common/SendEmail" ) ]
116+ public IActionResult SendEmail ( [ FromBody ] SendEmailDto sendEmailVo )
117+ {
118+ if ( sendEmailVo == null )
119+ {
120+ return ToResponse ( ApiResult . Error ( $ "请求参数不完整") ) ;
121+ }
122+ if ( string . IsNullOrEmpty ( OptionsSetting . MailOptions . FromEmail ) || string . IsNullOrEmpty ( OptionsSetting . MailOptions . Password ) )
123+ {
124+ return ToResponse ( ApiResult . Error ( $ "请配置邮箱信息") ) ;
125+ }
126+
127+ MailHelper mailHelper = new ( ) ;
128+
129+ string [ ] toUsers = sendEmailVo . ToUser . Split ( "," , StringSplitOptions . RemoveEmptyEntries ) ;
130+ string result = string . Empty ;
131+ if ( sendEmailVo . IsSend )
132+ {
133+ result = mailHelper . SendMail ( toUsers , sendEmailVo . Subject , sendEmailVo . Content , sendEmailVo . FileUrl , sendEmailVo . HtmlContent ) ;
134+ }
135+ _EmailLogService . AddEmailLog ( new EmailLog ( ) { EmailContent = sendEmailVo . HtmlContent , Subject = sendEmailVo . Subject , ToEmails = sendEmailVo . ToUser , AddTime = DateTime . Now , FromEmail = OptionsSetting . MailOptions . FromEmail , IsSend = sendEmailVo . IsSend ? 1 : 0 , SendResult = result } ) ;
136+ //logger.Info($"发送邮件{JsonConvert.SerializeObject(sendEmailVo)}, 结果{result}");
137+
138+ return SUCCESS ( result ) ;
139+ }
140+
141+ }
142+ }
0 commit comments