Email.cshtml.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Application.Abstractions.Messaging.Email;
  2. using SharedKernel;
  3. using MediatR;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Mvc.RazorPages;
  6. using Microsoft.Extensions.Options;
  7. using System.ComponentModel;
  8. using System.ComponentModel.DataAnnotations;
  9. namespace Admin.Pages.Config.Test
  10. {
  11. public class EmailModel(IMediator mediator, IOptions<AppSettings> options, IMailService mailService) : PageModel
  12. {
  13. public readonly AppSettings Settings = options.Value;
  14. [BindProperty]
  15. [MaxLength(256)]
  16. [DataType(DataType.EmailAddress)]
  17. [DisplayName("To Address")]
  18. public string? ToAddress { get; set; }
  19. [BindProperty]
  20. public InputModel Input { get; set; } = new();
  21. public async Task OnGetAsync(CancellationToken ct)
  22. {
  23. var config = await mediator.Send(new GetConfig.Query(), ct);
  24. if (config is not null)
  25. {
  26. Input = InputModel.From(config);
  27. }
  28. }
  29. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  30. {
  31. if (!ModelState.IsValid)
  32. {
  33. return Page();
  34. }
  35. await mailService.SendAsync(
  36. new SendData(
  37. ToAddress: ToAddress!,
  38. Subject: $"[TEST] SMTP 이메일 발송 테스트 - {DateTime.Now:yyyy-MM-dd HH:mm:ss}",
  39. MessageHtml: $"""
  40. <h3>테스트 메일</h3>
  41. <ul>
  42. <li>From: {Settings.SMTP.FromName} &lt;{Settings.SMTP.FromEmail}&gt;</li>
  43. <li>To: {ToAddress}</li>
  44. <li>Host: {Settings.SMTP.Host}:{Settings.SMTP.Port}</li>
  45. <li>TLS: {Settings.SMTP.UseStartTls}</li>
  46. </ul>
  47. """,
  48. MessageText: $"TEST MAIL\nFrom={Settings.SMTP.FromEmail}\nTo={ToAddress}\nHost={Settings.SMTP.Host}:{Settings.SMTP.Port}\nTLS={Settings.SMTP.UseStartTls}"
  49. ),
  50. ct
  51. );
  52. TempData["SuccessMessage"] = "테스트 이메일을 발송했습니다.";
  53. return RedirectToPage();
  54. }
  55. public sealed class InputModel
  56. {
  57. public UpdateConfig.Request.BasicConfigDto Basic { get; set; } = new();
  58. public static InputModel From(GetConfig.Response config)
  59. {
  60. var req = UpdateConfig.Request.From(config);
  61. return new()
  62. {
  63. Basic = req.Basic
  64. };
  65. }
  66. }
  67. }
  68. }