Email.cshtml.cs 2.5 KB

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