|
@@ -42,7 +42,13 @@ namespace MTWorkHR.Infrastructure.EmailService
|
|
|
|
|
|
// return response.IsSuccessStatusCode;
|
|
|
//}
|
|
|
- public async Task<bool> SendEmail(EmailMessage email)
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Send Email using an azure email address, smtpUserName and password
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="email"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<bool> SendEmailAzure(EmailMessage email)
|
|
|
{
|
|
|
var mailTo = email.To;
|
|
|
var settings = _configuration.MailSettings;
|
|
@@ -99,22 +105,16 @@ namespace MTWorkHR.Infrastructure.EmailService
|
|
|
_logger.LogError(ex, "Authentication failed for username {SmtpUsername}: {Message}",
|
|
|
settings.SmtpUsername, ex.Message);
|
|
|
throw ex;
|
|
|
- Console.WriteLine($"Authentication failed: {ex.Message}, Inner Exception: {ex.InnerException?.Message}");
|
|
|
- return false;
|
|
|
}
|
|
|
catch (SmtpCommandException ex)
|
|
|
{
|
|
|
_logger.LogError(ex, "SMTP error: {Message}, Status: {StatusCode}", ex.Message, ex.StatusCode);
|
|
|
throw ex;
|
|
|
- Console.WriteLine($"SMTP error: {ex.Message}, Status: {ex.StatusCode}, Inner Exception: {ex.InnerException?.Message}");
|
|
|
- return false;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogError(ex, "Error sending email to {To}: {Message}", mailTo, ex.Message);
|
|
|
throw ex;
|
|
|
- Console.WriteLine($"Error sending email: {ex.Message}, Inner Exception: {ex.InnerException?.Message}");
|
|
|
- return false;
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
@@ -128,7 +128,9 @@ namespace MTWorkHR.Infrastructure.EmailService
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
- public async Task<bool> SendEmailSMTP(EmailMessage email)
|
|
|
+
|
|
|
+ //Send Email using an email address and password
|
|
|
+ public async Task<bool> SendEmail(EmailMessage email)
|
|
|
{
|
|
|
var mailTo = email.To;
|
|
|
var settings = _configuration.MailSettings;
|
|
@@ -158,25 +160,32 @@ namespace MTWorkHR.Infrastructure.EmailService
|
|
|
using var smtp = new SmtpClient(logger);
|
|
|
try
|
|
|
{
|
|
|
-
|
|
|
-
|
|
|
- smtp.Connect(_configuration.MailSettings.Host, _configuration.MailSettings.Port, MailKit.Security.SecureSocketOptions.StartTls);
|
|
|
- smtp.Authenticate(_configuration.MailSettings.FromAddress, _configuration.MailSettings.Password);
|
|
|
+ await smtp.ConnectAsync(_configuration.MailSettings.Host, _configuration.MailSettings.Port, MailKit.Security.SecureSocketOptions.StartTls);
|
|
|
+ await smtp.AuthenticateAsync(_configuration.MailSettings.FromAddress, _configuration.MailSettings.Password);
|
|
|
+ _logger.LogInformation("Sending email to {To} with subject {Subject}", mailTo, email.Subject);
|
|
|
await smtp.SendAsync(emailObj);
|
|
|
- //smtp.Disconnect(true);
|
|
|
+ _logger.LogInformation("Email sent successfully to {To}", mailTo);
|
|
|
+
|
|
|
}
|
|
|
- catch (MailKit.Security.AuthenticationException ex)
|
|
|
+ catch (AuthenticationException ex)
|
|
|
{
|
|
|
- Console.WriteLine($"Authentication failed: {ex.Message}");
|
|
|
- throw;
|
|
|
+ _logger.LogError(ex, "Authentication failed for username {SmtpUsername}: {Message}",
|
|
|
+ settings.SmtpUsername, ex.Message);
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ catch (SmtpCommandException ex)
|
|
|
+ {
|
|
|
+ _logger.LogError(ex, "SMTP error: {Message}, Status: {StatusCode}", ex.Message, ex.StatusCode);
|
|
|
+ throw ex;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- Console.WriteLine($"Error sending email: {ex.Message}");
|
|
|
- throw;
|
|
|
+ _logger.LogError(ex, "Error sending email to {To}: {Message}", mailTo, ex.Message);
|
|
|
+ throw ex;
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
+ _logger.LogInformation("Disconnected from SMTP server");
|
|
|
await smtp.DisconnectAsync(true);
|
|
|
}
|
|
|
return true;
|