MailKit: Unable to send an email useSsl =false at local smtp server

I m try to send an email using my smpt live server port 465 useSsl=true with below code it run correctly and email sent but when i try to send an email using my local smpt server port 25 useSsl=false.

Environment language C# window 10

Below is the code i m trying it throw an exception

MailKit.Net.Smtp.SmtpClient client = null;
using (client = new MailKit.Net.Smtp.SmtpClient())
                {
                   
                    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                    client.Connect(IpAddress, 25, false);                  
                    client.Send("here is object of  MimeMessage");
                    client.Disconnect(true);
                    client.Dispose();                  
                } 

Exception at below message: The SMTP server has unexpectedly disconnected.

StackTrace:   at MailKit.Net.Smtp.SmtpStream.<ReadAheadAsync>d__47.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MailKit.Net.Smtp.SmtpStream.<ReadResponseAsync>d__54.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MailKit.Net.Smtp.SmtpStream.ReadResponse(CancellationToken cancellationToken)
   at MailKit.Net.Smtp.SmtpClient.<DataAsync>d__97.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at MailKit.Net.Smtp.SmtpClient.<SendAsync>d__99.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MailKit.Net.Smtp.SmtpClient.Send(FormatOptions options, MimeMessage message, CancellationToken cancellationToken, ITransferProgress progress)
   at MailKit.MailTransport.Send(MimeMessage message, CancellationToken cancellationToken, ITransferProgress progress)

Please reply what is going wrong i want to fix that

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 19 (9 by maintainers)

Most upvoted comments

The useSsl argument only tells MailKit whether the port that you are connecting to is an SSL-wrapped port, e.g. port 465 for SMTP, port 993 for IMAP, or port 995 for POP3.

If you do not want MailKit to use the STARTTLS command when connecting to a plain-text port, then you need to use:

client.Connect (host, port, SecureSocketOptions.None);

Try this:

using (var client = new MailKit.Net.Smtp.SmtpClient())
{
    client.Connect(host, port, ssl);
    client.Capabilities &= ~SmtpCapabilities.Pipelining;
    client.Send(mailMessage);
    client.Disconnect(true);
}