TestController.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Diagnostics;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Authorization;
  4. using bitforum.Models;
  5. using bitforum.Repository;
  6. using bitforum.Services;
  7. namespace bitforum.Controllers.System
  8. {
  9. [Authorize]
  10. [Route("System")]
  11. public class TestController : Controller
  12. {
  13. private readonly ILogger<TestController> _logger;
  14. private readonly IConfigRepository _configRepository;
  15. private readonly IMailService _mailService;
  16. private readonly string _ViewPath = "~/Views/System/Test.cshtml";
  17. public TestController(ILogger<TestController> logger, IConfigRepository configRepository, IMailService mailService)
  18. {
  19. _logger = logger;
  20. _configRepository = configRepository;
  21. _mailService = mailService;
  22. }
  23. [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  24. public IActionResult Error()
  25. {
  26. return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  27. }
  28. [HttpGet("Test")]
  29. public IActionResult Index()
  30. {
  31. ViewBag.Config = _configRepository.GetAll();
  32. return View(_ViewPath);
  33. }
  34. [HttpPost("Test")]
  35. public async Task<IActionResult> Send(string? toAddress)
  36. {
  37. try
  38. {
  39. if (string.IsNullOrEmpty(toAddress))
  40. {
  41. TempData["ErrorMessages"] = "이메일 주소를 입력해주세요.";
  42. return Index();
  43. }
  44. string subject = "Test E-mail";
  45. string content = "현재 이메일을 보고 있거나 받았다면 이메일이 정상적으로 수신된 것입니다.";
  46. await _mailService.SendEmailAsync(new SendData
  47. {
  48. ToAddress = toAddress,
  49. Subject = subject,
  50. Message = content
  51. });
  52. string message = "이메일이 전송되었습니다.";
  53. TempData["SuccessMessage"] = message;
  54. _logger.LogInformation(message);
  55. }
  56. catch (Exception e)
  57. {
  58. TempData["ErrorMessages"] = e.Message;
  59. _logger.LogError(e.Message);
  60. }
  61. return Index();
  62. }
  63. }
  64. }