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 #139

Closed
wants to merge 2 commits into from
Closed
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 `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
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
16 changes: 12 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,11 @@ public class SmtpClientOptions : INameable
{
private string? name = null;

/// <summary>
/// Gets or sets a value indicating whether authentication is not required to connect to the SMTP service.
/// </summary>
public bool AuthenticationNotRequired { get; set; }

/// <summary>
/// Gets or sets the host name of the SMTP service.
/// </summary>
Expand All @@ -28,9 +34,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="AuthenticationNotRequired"/> is set to <see langword="false"/>.</para>
/// </summary>
[Required(AllowEmptyStrings = false)]
public string Password { get; set; }
[RequiredIf(nameof(AuthenticationNotRequired), conditionalValue: false, 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 +55,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="AuthenticationNotRequired"/> is set to <see langword="false"/>.</para>
/// </summary>
[Required(AllowEmptyStrings = false)]
public string User { get; set; }
[RequiredIf(nameof(AuthenticationNotRequired), conditionalValue: false, 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.AuthenticationNotRequired)
{
await smtpClient.AuthenticateAsync(SmtpClientOptions.User, SmtpClientOptions.Password, cancellationToken);
}

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