Mailkit: Want to use SSL for sending emails using outlook in smtp mailkit

Created on 27 Sep 2017  路  3Comments  路  Source: jstedfast/MailKit

I am using the code below for connecting outlook, IMAP works well but when it comes to SMTP it throws error:-

using (var smtpClient = new MailKit.Net.Smtp.SmtpClient())
{
try
{
smtpClient.Connect(host, port, true);
smtpClient.Authenticate(fromemail, password);
smtpClient.Send(message);
smtpClient.Disconnect(true);
return String.Empty;
}

Above code is throwing various error such as "remote forcibly closed the connection", and sometimes "handshake failed error" and sometimes "A connection took much time to respond", like these error when I try using different ways based on your posts on google, stack overflows and gits.

Now I am using this code which is working fine ,

using (var smtpClient = new MailKit.Net.Smtp.SmtpClient())
{
try
{
if (host.Contains("outlook"))
{
smtpClient.ServerCertificateValidationCallback = (s, c, h, e) => true;
smtpClient.Connect(host, 587, false);
smtpClient.AuthenticationMechanisms.Remove("XOAUTH2");
}
else
{
smtpClient.Connect(host, port, true);
}

                    smtpClient.Authenticate(fromemail, password);
                    smtpClient.Send(message);
                    smtpClient.Disconnect(true);
                    return String.Empty;
                }

But I want to use SSL while sending emails please help me in detail.

question

Most helpful comment

Just to add a thought:

I believe the mismatching API design between .NET SmtpClient and MailKits SmtpClient class is causing some confusion for people moving from .NET SmtpClient to MailKit SmtpClient.

.NET SmtpClient.EnableSsl is not equivalent of MailKits SmtpClient.Connect() useSsl parameter.

  • .NET SmtpClient.EnableSsl: true means negotiating SSL/STARTTLS
  • MailKit SmtpClient.Connect() useSsl parameter: true means SSL only

I found a bug in Papercut which uses MailKit which very well may be there because of this confusion. I would have done this mistake as well, to be honest.

All 3 comments

I cant get the smtp.log file as access is denied at my work place.

There are 2 types of "SSL":

  1. SSL/TLS immediately on connect (i.e. no plain-text commands/responses/greetings are ever sent between client/server)
  2. SSL/TLS via the STARTTLS command.

Port 25 and 587 use mode 2. Port 465 uses mode 1.

The Connect() method that takes a useSsl refers to type 1 (i.e. true means use mode 1 while false means use mode 2 if-and-only-if-it-is-available).

Just to add a thought:

I believe the mismatching API design between .NET SmtpClient and MailKits SmtpClient class is causing some confusion for people moving from .NET SmtpClient to MailKit SmtpClient.

.NET SmtpClient.EnableSsl is not equivalent of MailKits SmtpClient.Connect() useSsl parameter.

  • .NET SmtpClient.EnableSsl: true means negotiating SSL/STARTTLS
  • MailKit SmtpClient.Connect() useSsl parameter: true means SSL only

I found a bug in Papercut which uses MailKit which very well may be there because of this confusion. I would have done this mistake as well, to be honest.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

syneex picture syneex  路  4Comments

atiqi36 picture atiqi36  路  7Comments

dbogatov picture dbogatov  路  7Comments

madu2003 picture madu2003  路  7Comments

SpiritBob picture SpiritBob  路  4Comments