EmailSender.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using bitforum.Repository;
  2. using Microsoft.AspNetCore.Identity.UI.Services;
  3. using System.Net;
  4. using System.Net.Mail;
  5. namespace bitforum.Services
  6. {
  7. public class EmailSender : IEmailSender
  8. {
  9. private readonly ILogger _logger;
  10. private readonly ConfigRepository _configRepository;
  11. private readonly string _smtpServer;
  12. private readonly int _smtpPort;
  13. private readonly string _fromEmail;
  14. private readonly string _emailPassword;
  15. public EmailSender(IConfiguration configuration, ILogger<EmailSender> logger, ConfigRepository configRepository)
  16. {
  17. _configRepository = configRepository;
  18. _logger = logger;
  19. // 1차로 Config 값 SMTP 조회
  20. _smtpServer = _configRepository.GetByKey("smtp_server")?.Value ?? string.Empty;
  21. _smtpPort = int.TryParse(_configRepository.GetByKey("smtp_port")?.Value, out var port) ? port : 0;
  22. _fromEmail = _configRepository.GetByKey("smtp_from_email")?.Value ?? string.Empty;
  23. _emailPassword = _configRepository.GetByKey("smtp_email_password")?.Value ?? string.Empty;
  24. // 2차로 appsettings.json 값 SMTP 조회
  25. var emailSettings = configuration.GetSection("EmailSettings");
  26. if (string.IsNullOrEmpty(_smtpServer))
  27. {
  28. _smtpServer = emailSettings["SmtpServer"] ?? string.Empty;
  29. }
  30. if (_smtpPort <= 0)
  31. {
  32. _smtpPort = int.TryParse(emailSettings["SmtpPort"], out var smtpPort) ? smtpPort : 0;
  33. }
  34. if (string.IsNullOrEmpty(_fromEmail))
  35. {
  36. _fromEmail = emailSettings["FromEmail"] ?? string.Empty;
  37. }
  38. if (string.IsNullOrEmpty(_emailPassword))
  39. {
  40. _emailPassword = emailSettings["EmailPassword"] ?? string.Empty;
  41. }
  42. }
  43. public async Task SendEmailAsync(string email, string subject, string message)
  44. {
  45. try
  46. {
  47. var client = new SmtpClient(_smtpServer, _smtpPort)
  48. {
  49. Credentials = new NetworkCredential(_fromEmail, _emailPassword),
  50. EnableSsl = true
  51. };
  52. var mailMessage = new MailMessage
  53. {
  54. From = new MailAddress(_fromEmail),
  55. Subject = subject,
  56. Body = message,
  57. IsBodyHtml = true
  58. };
  59. mailMessage.To.Add(email);
  60. _logger.LogInformation($"Sending email to {email} with subject {subject}.");
  61. await client.SendMailAsync(mailMessage);
  62. _logger.LogInformation($"Email sent successfully to {email}.");
  63. }
  64. catch (SmtpException smtpEx)
  65. {
  66. Console.WriteLine($"SMTP error: {smtpEx.Message}");
  67. _logger.LogError($"SMTP error: {smtpEx.StatusCode} - {smtpEx.Message}");
  68. throw;
  69. }
  70. catch (Exception ex)
  71. {
  72. Console.WriteLine($"Email sending failed: {ex.Message}");
  73. _logger.LogError($"Failed to send email to {email}: {ex.Message}");
  74. throw;
  75. }
  76. }
  77. }
  78. }