From 2cbc2e9fcd52d40bb9c8f83f9c2998c28e772fe5 Mon Sep 17 00:00:00 2001 From: Luis Marcos Rivera Date: Thu, 3 Oct 2024 17:26:18 +0200 Subject: [PATCH 1/3] Added conditional SMTP authentication --- .../Encamina.Enmarcha.Email.Abstractions.csproj | 1 + .../SmtpClientOptions.cs | 16 ++++++++++++---- .../EmailService.cs | 7 ++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Encamina.Enmarcha.Email.Abstractions/Encamina.Enmarcha.Email.Abstractions.csproj b/src/Encamina.Enmarcha.Email.Abstractions/Encamina.Enmarcha.Email.Abstractions.csproj index 25f205b..c60a147 100644 --- a/src/Encamina.Enmarcha.Email.Abstractions/Encamina.Enmarcha.Email.Abstractions.csproj +++ b/src/Encamina.Enmarcha.Email.Abstractions/Encamina.Enmarcha.Email.Abstractions.csproj @@ -13,6 +13,7 @@ + diff --git a/src/Encamina.Enmarcha.Email.Abstractions/SmtpClientOptions.cs b/src/Encamina.Enmarcha.Email.Abstractions/SmtpClientOptions.cs index d88be85..16e7142 100644 --- a/src/Encamina.Enmarcha.Email.Abstractions/SmtpClientOptions.cs +++ b/src/Encamina.Enmarcha.Email.Abstractions/SmtpClientOptions.cs @@ -1,6 +1,7 @@ using System.ComponentModel.DataAnnotations; using System.Net.Security; +using Encamina.Enmarcha.Core.DataAnnotations; using Encamina.Enmarcha.Entities.Abstractions; namespace Encamina.Enmarcha.Email.Abstractions; @@ -12,6 +13,11 @@ public class SmtpClientOptions : INameable { private string? name = null; + /// + /// Gets or sets a value indicating whether authentication is not required to connect to the SMTP service. + /// + public bool AuthenticationNotRequired { get; set; } + /// /// Gets or sets the host name of the SMTP service. /// @@ -28,9 +34,10 @@ public string Name /// /// Gets or sets the password credential required to connect to the SMTP service. + /// This property is only required if is set to . /// - [Required(AllowEmptyStrings = false)] - public string Password { get; set; } + [RequiredIf(nameof(AuthenticationNotRequired), conditionalValue: false, allowEmpty: false)] + public string? Password { get; set; } /// /// Gets or sets the port for the SMTP service. Default value is 587. @@ -48,9 +55,10 @@ public string Name /// /// Gets or sets the user credential required to connect to the SMTP service. + /// This property is only required if is set to . /// - [Required(AllowEmptyStrings = false)] - public string User { get; set; } + [RequiredIf(nameof(AuthenticationNotRequired), conditionalValue: false, allowEmpty: false)] + public string? User { get; set; } /// /// Gets or sets a value indicating whether SSL should be use. Default is . diff --git a/src/Encamina.Enmarcha.Email.MailKit/EmailService.cs b/src/Encamina.Enmarcha.Email.MailKit/EmailService.cs index 95c3e35..1418f76 100644 --- a/src/Encamina.Enmarcha.Email.MailKit/EmailService.cs +++ b/src/Encamina.Enmarcha.Email.MailKit/EmailService.cs @@ -108,7 +108,12 @@ public async Task SendAsync(CancellationToken cancellationToken) } await smtpClient.ConnectAsync(SmtpClientOptions.Host, SmtpClientOptions.Port, SmtpClientOptions.UseSSL, cancellationToken); - await smtpClient.AuthenticateAsync(SmtpClientOptions.User, SmtpClientOptions.Password, cancellationToken); + + if (!SmtpClientOptions.AuthenticationNotRequired) + { + await smtpClient.AuthenticateAsync(SmtpClientOptions.User, SmtpClientOptions.Password, cancellationToken); + } + await smtpClient.SendAsync(mailMessage, cancellationToken); await smtpClient.DisconnectAsync(true, cancellationToken); } From cab7c143ff9f7f86bcb635d6587f7ce9e2132ca1 Mon Sep 17 00:00:00 2001 From: Luis Marcos Rivera Date: Thu, 3 Oct 2024 17:34:57 +0200 Subject: [PATCH 2/3] Updated to version 8.1.9-preview-01 and updated CHANGELOG.md --- CHANGELOG.md | 6 ++++++ Directory.Build.props | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c898fb..9ef19d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,12 @@ Also, any bug fix must start with the prefix �Bug fix:� followed by the desc Previous classification is not required if changes are simple or all belong to the same category. +## [8.1.9] + +### Minor Changes + +- Added `AuthenticationNotRequired` property to `SmtpClientOptions.cs`, which can be set to `true` when the SMTP server does not require authentication (i.e., no username/password is needed). + ## [8.1.8] ### Breaking Changes diff --git a/Directory.Build.props b/Directory.Build.props index 94924f7..fcdc636 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -16,8 +16,8 @@ - 8.1.8 - + 8.1.9 + preview-01