In Kentico version 12.0.101, there was a change to the underlying implementation of the email provider. Namely, support for OAuth 2.0 was added. On the surface, this change appears to be beneficial; however, this introduces a breaking change. In environments that use unsecure SMTP servers, emails were failing. They were being routed through the new MailKit implementation. By default, MailKit will require the connection made to the SMTP server to use TLS/SSL.
Currently, Kentico version 12.0.102 has been released and provides ISmtpClientFactory. This interface allows us to create a MailKit client. If you have a more advanced use case, the following solution will likely not be beneficial for you. Instead, you can upgrade to version 12.0.102 and create the fix that fits your needs.
As a quick fix, what we’ve done is override the email provider to only use the original SMTP client from the System.Net.Mail namespace. The methods SendEmailInternal and SendEmailAsyncInternal are where we override the logic. Since the internal email provider has limited accessibility levels, we needed to copy various methods from the original provider into our custom provider.
///
/// Forcefully sends emails through the .NET SMTP Client.
///
[assembly: RegisterCustomProvider(typeof(SmtpEmailProvider))]
namespace YourSolution.Providers
{
public class SmtpEmailProvider : EmailProvider
{
private readonly SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
private readonly ConcurrentDictionary mailKitClients = new ConcurrentDictionary();
protected override void SendEmailInternal(string siteName, MailMessage message, SMTPServerInfo smtpServer)
{
SendWithSmtpClient(message, smtpServer);
}
protected async override void SendEmailAsyncInternal(string siteName, MailMessage message, SMTPServerInfo smtpServer, EmailToken emailToken)
{
await SendWithSmtpClientAsync(message, smtpServer, emailToken).ConfigureAwait(continueOnCapturedContext: false);
}
private static void SendWithSmtpClient(MailMessage message, SMTPServerInfo smtpServer)
{
using (SmtpClient smtpClient = GetSMTPClient(smtpServer))
{
smtpClient.Send(message);
}
}
private async Task SendWithSmtpClientAsync(MailMessage message, SMTPServerInfo smtpServer, EmailToken emailToken)
{
SmtpClient sMTPClient = GetSMTPClient(smtpServer);
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.CancelAfter(60000);
TaskCompletionSource
In conclusion, the introduction of support for OAuth 2.0 in Kentico version 12.0.101 was a welcome change, but it also brought about an unintended consequence for environments that use unsecure SMTP servers. This led to email failures when routing through the new MailKit implementation, which required TLS/SSL connections to the SMTP server. A quick fix for the issue is to override the email provider to only use the original SMTP client, which requires copying various methods into a custom provider due to limited accessibility levels. Overall, this experience highlights the importance of careful consideration and testing of changes to underlying implementations in software updates.
We love to make cool things with cool people. Have a project you’d like to collaborate on? Let’s chat!
Stay up to date on what BizStream is doing and keep in the loop on the latest in marketing & technology.