Skip to content

Commit

Permalink
added CustomAuthenticationMethod to EmailOptions for #44
Browse files Browse the repository at this point in the history
  • Loading branch information
danzuep committed Nov 20, 2023
1 parent 867cc22 commit e943d9c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<Authors>Daniel Collingwood</Authors>
Expand All @@ -13,9 +13,11 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/danzuep/MailKitSimplified</RepositoryUrl>
<PackageProjectUrl>https://github.com/danzuep/MailKitSimplified</PackageProjectUrl>
<RepositoryUrl>https://github.com/danzuep/MailKitSimplified</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageReleaseNotes>
2.9.0 .NET 8.0 Target Framework added
2.7.0 Non-fluent usages marked obsolete
2.5.0 Reuse existing IImapClient
2.4.0 MailReader Query
Expand Down
12 changes: 9 additions & 3 deletions source/MailKitSimplified.Receiver/Models/EmailReceiverOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ public string MailFolderName
public ImapCapabilities CapabilitiesToRemove { get; set; } = ImapCapabilities.None;
public NetworkCredential ImapCredential { get; set; } = new NetworkCredential();
public SaslMechanism AuthenticationMechanism { get; set; } = null;
public Func<IImapClient, Task> CustomAuthenticationMethod { get; set; } = null;
public TimeSpan Timeout { get; set; } = TimeSpan.FromMinutes(1);

public ProtocolLoggerOptions ProtocolLogger { get; set; } = new ProtocolLoggerOptions();

//[Obsolete("Use ProtocolLogger.FileWrite.FileWritePath or ILogger instead.")]
//[Obsolete("Use ProtocolLogger.FileWriter.FilePath or ILogger instead.")]
public string ProtocolLog
{
get => ProtocolLogger.FileWriter.FilePath;
set => ProtocolLogger.FileWriter.FilePath = value;
}

//[Obsolete("Use ProtocolLogger.FileWrite.AppendToExisting or ILogger instead.")]
//[Obsolete("Use ProtocolLogger.FileWriter.AppendToExisting or ILogger instead.")]
public bool ProtocolLogFileAppend
{
get => ProtocolLogger.FileWriter.AppendToExisting;
Expand Down Expand Up @@ -86,7 +87,10 @@ public async Task<IImapClient> CreateImapClientAsync(IProtocolLogger protocolLog
imapClient.Capabilities &= ~CapabilitiesToRemove;
if (imapClient.Capabilities.HasFlag(ImapCapabilities.Compress))
await imapClient.CompressAsync(cancellationToken).ConfigureAwait(false);
if (AuthenticationMechanism != null)

if (CustomAuthenticationMethod != null) // for XOAUTH2 and OAUTHBEARER
await CustomAuthenticationMethod(imapClient).ConfigureAwait(false);
else if (AuthenticationMechanism != null)
await imapClient.AuthenticateAsync(AuthenticationMechanism).ConfigureAwait(false);
else
{
Expand All @@ -97,8 +101,10 @@ public async Task<IImapClient> CreateImapClientAsync(IProtocolLogger protocolLog
else
await imapClient.AuthenticateAsync(ImapCredential, cancellationToken).ConfigureAwait(false);
}

if (MailFolderAccess != FolderAccess.None)
await imapClient.Inbox.OpenAsync(MailFolderAccess).ConfigureAwait(false);

return imapClient;
}

Expand Down
9 changes: 5 additions & 4 deletions source/MailKitSimplified.Receiver/Services/ImapReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public sealed class ImapReceiver : IImapReceiver
private Lazy<MailFolderClient> _mailFolderClient;
private Lazy<MailFolderReader> _mailFolderReader;
private Lazy<MailFolderMonitor> _mailFolderMonitor;
private Func<IImapClient, Task> _customAuthenticationMethod;
private bool _isClientInjected;
private IImapClient _imapClient;
private IProtocolLogger _imapLogger;
Expand Down Expand Up @@ -201,7 +200,7 @@ public ImapReceiver RemoveAuthenticationMechanism(string authenticationMechanism

public ImapReceiver SetCustomAuthentication(Func<IImapClient, Task> customAuthenticationMethod)
{
_customAuthenticationMethod = customAuthenticationMethod;
_receiverOptions.CustomAuthenticationMethod = customAuthenticationMethod;
return this;
}

Expand Down Expand Up @@ -261,13 +260,15 @@ internal async ValueTask ConnectImapClientAsync(CancellationToken cancellationTo
/// Authenticating via a SASL mechanism may be a multi-step process.
/// <see href="http://www.mimekit.net/docs/html/T_MailKit_Security_SaslMechanism.htm"/>
/// <seealso href="http://www.mimekit.net/docs/html/T_MailKit_Security_SaslMechanismOAuth2.htm"/>
/// <seealso href="https://github.com/jstedfast/MailKit/blob/master/ExchangeOAuth2.md"/>
/// <seealso href="https://github.com/jstedfast/MailKit/blob/master/GMailOAuth2.md"/>
/// </summary>
internal async ValueTask AuthenticateImapClientAsync(CancellationToken cancellationToken = default)
{
if (!_imapClient.IsAuthenticated)
{
if (_customAuthenticationMethod != null) // for XOAUTH2 and OAUTHBEARER
await _customAuthenticationMethod(_imapClient).ConfigureAwait(false);
if (_receiverOptions.CustomAuthenticationMethod != null) // for XOAUTH2 and OAUTHBEARER
await _receiverOptions.CustomAuthenticationMethod(_imapClient).ConfigureAwait(false);
else if (_receiverOptions.AuthenticationMechanism != null)
await _imapClient.AuthenticateAsync(_receiverOptions.AuthenticationMechanism).ConfigureAwait(false);
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<Authors>Daniel Collingwood</Authors>
Expand All @@ -13,9 +13,11 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/danzuep/MailKitSimplified</RepositoryUrl>
<PackageProjectUrl>https://github.com/danzuep/MailKitSimplified</PackageProjectUrl>
<RepositoryUrl>https://github.com/danzuep/MailKitSimplified</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageReleaseNotes>
2.9.0 .NET 8.0 Target Framework added
2.5.0 Reuse existing ISmtpClient
2.3.0 Forwarding and replies
0.1.3 First version of SmtpSender
Expand Down
5 changes: 4 additions & 1 deletion source/MailKitSimplified.Sender/Models/EmailSenderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class EmailSenderOptions
public SmtpCapabilities CapabilitiesToRemove { get; set; } = SmtpCapabilities.None;
public NetworkCredential SmtpCredential { get; set; } = null;
public SaslMechanism AuthenticationMechanism { get; set; } = null;
public Func<ISmtpClient, Task> CustomAuthenticationMethod { get; set; } = null;
public string ProtocolLog { get; set; } = null;
public bool ProtocolLogFileAppend { get; set; } = false;
public TimeSpan Timeout { get; set; } = TimeSpan.FromMinutes(1);
Expand Down Expand Up @@ -60,7 +61,9 @@ public async Task<ISmtpClient> CreateSmtpClientAsync(IProtocolLogger protocolLog
await smtpClient.ConnectAsync(SmtpHost, SmtpPort, SocketOptions, cancellationToken).ConfigureAwait(false);
if (CapabilitiesToRemove != SmtpCapabilities.None)
smtpClient.Capabilities &= ~CapabilitiesToRemove;
if (AuthenticationMechanism != null)
if (CustomAuthenticationMethod != null) // for XOAUTH2 and OAUTHBEARER
await CustomAuthenticationMethod(smtpClient).ConfigureAwait(false);
else if (AuthenticationMechanism != null)
await smtpClient.AuthenticateAsync(AuthenticationMechanism).ConfigureAwait(false);
else
{
Expand Down
7 changes: 3 additions & 4 deletions source/MailKitSimplified.Sender/Services/SmtpSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public sealed class SmtpSender : ISmtpSender
{
private CancellationTokenSource _cts = null;
private readonly ConcurrentQueue<MimeMessage> _sendQueue = new ConcurrentQueue<MimeMessage>();
private Func<ISmtpClient, Task> _customAuthenticationMethod;
private bool _isClientInjected;
private ISmtpClient _smtpClient;
private IProtocolLogger _smtpLogger;
Expand Down Expand Up @@ -166,7 +165,7 @@ public SmtpSender RemoveAuthenticationMechanism(string authenticationMechanismsN

public SmtpSender SetCustomAuthentication(Func<ISmtpClient, Task> customAuthenticationMethod)
{
_customAuthenticationMethod = customAuthenticationMethod;
_senderOptions.CustomAuthenticationMethod = customAuthenticationMethod;
return this;
}

Expand Down Expand Up @@ -264,8 +263,8 @@ internal async ValueTask AuthenticateAsync(CancellationToken cancellationToken =
{
if (_senderOptions.SmtpCredential != null && !_smtpClient.IsAuthenticated)
{
if (_customAuthenticationMethod != null) // for XOAUTH2 and OAUTHBEARER
await _customAuthenticationMethod(_smtpClient).ConfigureAwait(false);
if (_senderOptions.CustomAuthenticationMethod != null) // for XOAUTH2 and OAUTHBEARER
await _senderOptions.CustomAuthenticationMethod(_smtpClient).ConfigureAwait(false);
else if (_senderOptions.AuthenticationMechanism != null)
await _smtpClient.AuthenticateAsync(_senderOptions.AuthenticationMechanism).ConfigureAwait(false);
else
Expand Down

0 comments on commit e943d9c

Please sign in to comment.