-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removed references to the appsettings.secrets.json file, refactored S…
…mtpEmailSender
- Loading branch information
Showing
21 changed files
with
117 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
src/Core/Logistics.Application.Core/Options/GoogleRecaptchaOptions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 0 additions & 84 deletions
84
src/Core/Logistics.Application.Core/Services/EmailSender.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/Core/Logistics.Application.Core/Services/Implementations/GoogleRecaptchaOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Logistics.Application.Core.Services.Implementations; | ||
|
||
internal class GoogleRecaptchaOptions | ||
{ | ||
public string? SiteKey { get; set; } | ||
public string? SecretKey { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/Core/Logistics.Application.Core/Services/Implementations/SmtpEmailSender.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Net; | ||
using System.Net.Mail; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Logistics.Application.Core.Services.Implementations; | ||
|
||
internal sealed class SmtpEmailSender : IEmailSender | ||
{ | ||
private readonly SmtpOptions _options; | ||
private readonly ILogger<SmtpEmailSender> _logger; | ||
|
||
public SmtpEmailSender( | ||
SmtpOptions options, | ||
ILogger<SmtpEmailSender> logger) | ||
{ | ||
_options = options ?? throw new ArgumentNullException(nameof(options)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
|
||
ArgumentException.ThrowIfNullOrEmpty(options.SenderName); | ||
ArgumentException.ThrowIfNullOrEmpty(options.SenderEmail); | ||
ArgumentException.ThrowIfNullOrEmpty(options.Host); | ||
ArgumentException.ThrowIfNullOrEmpty(options.UserName); | ||
ArgumentException.ThrowIfNullOrEmpty(options.Password); | ||
} | ||
|
||
public async Task<bool> SendEmailAsync(string recipient, string subject, string htmlBody) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(recipient); | ||
ArgumentException.ThrowIfNullOrEmpty(subject); | ||
ArgumentException.ThrowIfNullOrEmpty(htmlBody); | ||
|
||
try | ||
{ | ||
var from = new MailAddress(_options.SenderEmail!, _options.SenderName); | ||
using var mailMessage = new MailMessage(from, new MailAddress(recipient)); | ||
mailMessage.Subject = subject; | ||
mailMessage.Body = htmlBody; | ||
mailMessage.IsBodyHtml = true; | ||
mailMessage.Priority = MailPriority.High; | ||
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.Delay | | ||
DeliveryNotificationOptions.OnFailure | | ||
DeliveryNotificationOptions.OnSuccess; | ||
|
||
using var smtpClient = new SmtpClient(_options.Host, _options.Port); | ||
smtpClient.Port = _options.Port; | ||
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; | ||
smtpClient.UseDefaultCredentials = false; | ||
smtpClient.Host = _options.Host!; | ||
smtpClient.EnableSsl = true; | ||
smtpClient.Timeout = 5000; | ||
smtpClient.Credentials = new NetworkCredential(_options.UserName, _options.Password); | ||
|
||
await smtpClient.SendMailAsync(mailMessage); | ||
_logger.LogInformation("Email has been sent to {Mail}, subject: \'{Subject}\'", recipient, subject); | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogWarning( | ||
"Could not send email to {Mail}, subject: \'{Subject}\'. \nThrown exception: {Exception}", | ||
recipient, subject, ex.ToString()); | ||
return false; | ||
} | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...cation.Core/Options/EmailSenderOptions.cs → ...e/Services/Implementations/SmtpOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.