| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using Application.Features.Config;
- using Application.Features.Config.Commands;
- using Application.Features.Config.Queries;
- using Application.Abstractions.Messaging.Email;
- using MediatR;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.RazorPages;
- using Microsoft.Extensions.Options;
- using SharedKernel;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- namespace Admin.Pages.Config.Test
- {
- public class EmailModel(IMediator mediator, IOptions<AppSettings> options, IMailService mailService) : PageModel
- {
- public readonly AppSettings Settings = options.Value;
- [BindProperty]
- [MaxLength(256)]
- [DataType(DataType.EmailAddress)]
- [DisplayName("To Address")]
- public string? ToAddress { get; set; }
- [BindProperty]
- public InputModel Input { get; set; } = new();
- public async Task OnGetAsync(CancellationToken cancellationToken)
- {
- var config = await mediator.Send(new GetConfigQuery(), cancellationToken);
- if (config is not null)
- {
- Input = InputModel.From(config);
- }
- }
- public async Task<IActionResult> OnPostAsync(CancellationToken cancellationToken)
- {
- if (!ModelState.IsValid)
- {
- return Page();
- }
- await mailService.SendAsync(
- new SendData(
- ToAddress: ToAddress!,
- Subject: $"[TEST] SMTP 이메일 발송 테스트 - {DateTime.Now:yyyy-MM-dd HH:mm:ss}",
- MessageHtml: $"""
- <h3>테스트 메일</h3>
- <ul>
- <li>From: {Settings.SMTP.FromName} <{Settings.SMTP.FromEmail}></li>
- <li>To: {ToAddress}</li>
- <li>Host: {Settings.SMTP.Host}:{Settings.SMTP.Port}</li>
- <li>TLS: {Settings.SMTP.UseStartTls}</li>
- </ul>
- """,
- MessageText: $"TEST MAIL\nFrom={Settings.SMTP.FromEmail}\nTo={ToAddress}\nHost={Settings.SMTP.Host}:{Settings.SMTP.Port}\nTLS={Settings.SMTP.UseStartTls}"
- ),
- cancellationToken
- );
- TempData["SuccessMessage"] = "테스트 이메일을 발송했습니다.";
- return RedirectToPage();
- }
- public sealed class InputModel
- {
- public ConfigDto.BasicConfigDto Basic { get; set; } = new();
- public static InputModel From(ConfigDto config)
- {
- return new()
- {
- Basic = config.Basic
- };
- }
- public UpdateConfigCommand ToCommand()
- {
- return new(
- Basic
- );
- }
- }
- }
- }
|