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.
I cant get the smtp.log file as access is denied at my work place.
There are 2 types of "SSL":
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.
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.
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.
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.