jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.

Home Page:http://www.mimekit.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to connect to smtp.office365.com

stevenxi opened this issue · comments

Describe the bug
Got SslHandshakeException when attempt to connect to office 365 smpt server at the stagel of "ConnectAsync()"

Platform (please complete the following information):

  • OS: Windows
  • .NET Runtime: .NET Framework
  • .NET Framework: .NET Framework 4.6.1
  • MailKit Version: 4.2.0

Exception
If you got an exception, please include the exception Message and StackTrace.

MailKit.Security.SslHandshakeException
HResult=0x80131500
Message=An error occurred while attempting to establish an SSL or TLS connection.

The server's SSL certificate could not be validated for the following reasons:
• The server certificate has the following errors:
• The revocation function was unable to check revocation for the certificate.

Source=MailKit
StackTrace:
at MailKit.Net.Smtp.SmtpClient.d__8.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at ConsoleApp1.Program.

d__23.MoveNext() in D:\Code\NoSourceControl\ConsoleApp1\ConsoleApp1\Program.cs:line 454

This exception was originally thrown at this call stack:
System.Net.Security.SslState.InternalEndProcessAuthentication(System.Net.LazyAsyncResult)
System.Net.Security.SslState.EndProcessAuthentication(System.IAsyncResult)
System.Net.Security.SslStream.EndAuthenticateAsClient(System.IAsyncResult)
System.Threading.Tasks.TaskFactory.FromAsyncCoreLogic(System.IAsyncResult, System.Func<System.IAsyncResult, TResult>, System.Action<System.IAsyncResult>, System.Threading.Tasks.Task, bool)
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Threading.Tasks.Task)
MailKit.Net.Smtp.SmtpClient.ConnectAsync(string, int, MailKit.Security.SecureSocketOptions, System.Threading.CancellationToken)

Inner Exception 1:
AuthenticationException: The remote certificate is invalid according to the validation procedure.

To Reproduce
Steps to reproduce the behavior:
Execute the code snippets.

Expected behavior
Connect to the smtp server

Code Snippets

            using (var client = new SmtpClient())
            {
                await client.ConnectAsync("smtp.office365.com", 587, SecureSocketOptions.StartTls);

                await client.DisconnectAsync(true);
            }

Protocol Logs
Please include a protocol log (scrubbed of any authentication data), especially
if you got an exception such as Syntax error in XYZ. Unexpected token: ....

To get a protocol log, follow one of the following code snippets:

Connected to smtp://smtp.office365.com:587/?starttls=always
S: 220 AM8P189CA0004.outlook.office365.com Microsoft ESMTP MAIL Service ready at Thu, 14 Sep 2023 15:42:19 +0000
C: EHLO DESKTOP-LOD3TBH
S: 250-AM8P189CA0004.outlook.office365.com Hello [139.28.110.98]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-STARTTLS
S: 250-8BITMIME
S: 250 SMTPUTF8
C: STARTTLS
S: 220 2.0.0 SMTP server ready

The SslHandshakeException explains that it failed because it was unable to check revocation for the certificate (likely the Certificate Authority server that generated the SSL certificate is down or otherwise unreachable right now).

This can happen.

If you want to avoid revocation checks, you can do this:

using (var client = new SmtpClient())
{
    client.CheckCertificateRevocation = false;
    await client.ConnectAsync("smtp.office365.com", 587, SecureSocketOptions.StartTls);

    await client.DisconnectAsync(true);
}

hi @jstedfast ,

Thanks for the quick response. Yes it works.
Although it's a bit strange, I don't think Microsoft's Office 365 SLL certificate server is down all the time.