Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added conditional SMTP authentication support #140

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 the `AuthenticationRequired` property to `SmtpClientOptions.cs`, which is set to `true` by default. This indicates that authentication is required to connect to the SMTP server. If set to `false`, the server does not require authentication, meaning no username or password is needed for the connection.

## [8.1.8]

### Breaking Changes
Expand Down
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
</PropertyGroup>

<PropertyGroup>
<VersionPrefix>8.1.8</VersionPrefix>
<VersionSuffix></VersionSuffix>
<VersionPrefix>8.1.9</VersionPrefix>
<VersionSuffix>preview-01</VersionSuffix>
</PropertyGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Encamina.Enmarcha.Core\Encamina.Enmarcha.Core.csproj" />
<ProjectReference Include="..\Encamina.Enmarcha.Entities.Abstractions\Encamina.Enmarcha.Entities.Abstractions.csproj" />
</ItemGroup>

Expand Down
19 changes: 15 additions & 4 deletions src/Encamina.Enmarcha.Email.Abstractions/SmtpClientOptions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,6 +13,14 @@ public class SmtpClientOptions : INameable
{
private string? name = null;

/// <summary>
/// Gets a value indicating whether authentication is required to connect to the SMTP service.
/// </summary>
/// <remarks>
/// The default value is <see langword="true"/>. Set this to <see langword="false"/> if the SMTP service does not require authentication.
/// </remarks>
public bool AuthenticationRequired { get; init; } = true;

/// <summary>
/// Gets or sets the host name of the SMTP service.
/// </summary>
Expand All @@ -28,9 +37,10 @@ public string Name

/// <summary>
/// Gets or sets the password credential required to connect to the SMTP service.
/// <para>This property is only required if <see cref="AuthenticationRequired"/> is set to <see langword="false"/>.</para>
/// </summary>
[Required(AllowEmptyStrings = false)]
public string Password { get; set; }
[RequiredIf(nameof(AuthenticationRequired), conditionalValue: true, allowEmpty: false)]
public string? Password { get; set; }

/// <summary>
/// Gets or sets the port for the SMTP service. Default value is <c>587</c>.
Expand All @@ -48,9 +58,10 @@ public string Name

/// <summary>
/// Gets or sets the user credential required to connect to the SMTP service.
/// <para>This property is only required if <see cref="AuthenticationRequired"/> is set to <see langword="false"/>.</para>
/// </summary>
[Required(AllowEmptyStrings = false)]
public string User { get; set; }
[RequiredIf(nameof(AuthenticationRequired), conditionalValue: true, allowEmpty: false)]
public string? User { get; set; }

/// <summary>
/// Gets or sets a value indicating whether SSL should be use. Default is <see langword="false"/>.
Expand Down
7 changes: 6 additions & 1 deletion src/Encamina.Enmarcha.Email.MailKit/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.AuthenticationRequired)
{
await smtpClient.AuthenticateAsync(SmtpClientOptions.User, SmtpClientOptions.Password, cancellationToken);
}

await smtpClient.SendAsync(mailMessage, cancellationToken);
await smtpClient.DisconnectAsync(true, cancellationToken);
}
Expand Down
Loading