using dy.net.model.dto; using dy.net.service; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace dy.net.Controllers { [Route("api/email")] [ApiController] [Authorize] public sealed class EmailController : ControllerBase { private readonly EmailNotificationSettingsService _settings; public EmailController(EmailNotificationSettingsService settings) => _settings = settings; [HttpGet("config")] public async Task GetConfig() { var settings = await _settings.GetAsync(); return ApiResult.Success(_settings.ToDto(settings)); } [HttpPut("config")] public async Task UpdateConfig(EmailNotificationSettingsDto dto) { try { return ApiResult.Success(await _settings.SaveAsync(dto)); } catch (InvalidOperationException ex) { return ApiResult.Fail(ex.Message); } } [HttpPost("test")] public async Task Test(EmailNotificationSettingsDto dto, CancellationToken cancellationToken) { try { var message = await _settings.TestAsync(dto, cancellationToken); return ApiResult.Success(new { message }); } catch (Exception ex) when (ex is not OperationCanceledException) { var message = ex.GetBaseException().Message; Serilog.Log.Warning(ex, "SMTP 测试邮件发送失败"); return ApiResult.Fail(string.IsNullOrWhiteSpace(message) ? "测试邮件发送失败" : message); } } } }