Search code examples
c#.netsmtptimeoutmailkit

Exchange Server Timout issue in .Net Mail Kit


Mail Kit package connect method throw operation timeout exception but EASendMail package send email successfully with same properties in .Net app. You can see written c# code under below.

var host = "mail.company.com";
var port = 25;
var userName = "username";
var domain = "domain";
var password = "password";
var senderEmail = "[email protected]";
var senderName = "Sender Name";

MailKit:

var message = new MimeMessage();
message.From.Add(new MailboxAddress("", senderEmail));
message.To.Add(new MailboxAddress("","[email protected]"));
message.Subject = "test email";
MailKit.Net.Smtp.SmtpClient smtp = new MailKit.Net.Smtp.SmtpClient();
BodyBuilder body = new BodyBuilder();
smtp.ServerCertificateValidationCallback =
(s, certificate, chain, sslPolicyErrors) => true;
smtp.Connect(host,port,SecureSocketOptions.Auto); 
smtp.Authenticate(userName, password);
smtp.Send(message);

EASendMail

 SmtpMail oMail = new SmtpMail("TryIt");
 oMail.From = senderEmail;
 oMail.To = "[email protected]";
 oMail.Subject = "test email for test";
 oMail.TextBody = "testBody";
 SmtpServer oServer = new SmtpServer(host,port);
 oServer.User = userName;
 oServer.Password = password;
 oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
 oServer.Protocol = ServerProtocol.ExchangeEWS;
 EASendMail.SmtpClient oSmtp = new EASendMail.SmtpClient();
 oSmtp.SendMail(oServer, oMail);

Any idea?

I expected email sent with Mail kit. In additional I tried send email with System.Net.Mail but its give timeout exception again.


Solution

  • It's failing for MailKit and System.Net.Mail because you are using the SMTP protocol for those.

    It works with EASendMail because you are using the EWS protocol instead of SMTP:

    oServer.Protocol = ServerProtocol.ExchangeEWS;
    

    This suggests that the SMTP protocol on your Exchange server is disabled.