Skip to content

Commit

Permalink
Merge pull request #140 from Encamina/@lmarcos/email_improvements
Browse files Browse the repository at this point in the history
Added conditional SMTP authentication support
  • Loading branch information
LuisM000 authored Oct 4, 2024
2 parents 19ad3dd + c5ba5c1 commit abc854e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
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

0 comments on commit abc854e

Please sign in to comment.