Skip to content
Open
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
12 changes: 8 additions & 4 deletions src/Serilog.Sinks.Email/Sinks/Email/EmailSinkOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using MailKit.Security;
using Serilog.Formatting;
Expand Down Expand Up @@ -63,7 +60,7 @@ public EmailSinkOptions()
public int Port { get; set; } = DefaultPort;

/// <summary>
/// Gets or sets the credentials used for authentication.
/// Gets or sets the credentials used for authentication. See also <see cref="SaslMechanism"/> for OAuth2 credentials.
/// </summary>
public ICredentialsByHost? Credentials { get; set; }

Expand Down Expand Up @@ -97,4 +94,11 @@ public EmailSinkOptions()
/// Provides a method that validates server certificates.
/// </summary>
public System.Net.Security.RemoteCertificateValidationCallback? ServerCertificateValidationCallback { get; set; }

/// <summary>
/// A <see cref="MailKit.Security.SaslMechanism"/> for performing OAuth2 authentication against services such as
/// Microsoft 365. See <a href="https://github.com/jstedfast/MailKit/blob/master/ExchangeOAuth2.md#authenticating-a-web-service-with-oauth2">these instructions</a>
/// for how to create this object.
/// </summary>
public SaslMechanism? SaslMechanism { get; set; }
}
15 changes: 10 additions & 5 deletions src/Serilog.Sinks.Email/Sinks/Email/MailKitEmailTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public async Task SendMailAsync(EmailMessage emailMessage)
? new BodyBuilder { HtmlBody = emailMessage.Body }.ToMessageBody()
: new BodyBuilder { TextBody = emailMessage.Body }.ToMessageBody();

using var smtpClient = OpenConnectedSmtpClient();
using var smtpClient = await OpenConnectedSmtpClientAsync();
await smtpClient.SendAsync(mimeMessage);
await smtpClient.DisconnectAsync(quit: true);
}

SmtpClient OpenConnectedSmtpClient()
async Task<SmtpClient> OpenConnectedSmtpClientAsync()
{
var smtpClient = new SmtpClient();

Expand All @@ -48,15 +48,20 @@ SmtpClient OpenConnectedSmtpClient()
smtpClient.ServerCertificateValidationCallback += options.ServerCertificateValidationCallback;
}

smtpClient.Connect(options.Host, options.Port, options.ConnectionSecurity);
await smtpClient.ConnectAsync(options.Host, options.Port, options.ConnectionSecurity);

if (options.Credentials != null)
if (options.SaslMechanism != null)
{
smtpClient.Authenticate(
await smtpClient.AuthenticateAsync(options.SaslMechanism);
}
else if (options.Credentials != null)
{
await smtpClient.AuthenticateAsync(
Encoding.UTF8,
options.Credentials.GetCredential(
options.Host, options.Port, "smtp"));
}

return smtpClient;
}

Expand Down