Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
MailKit: The handshake failed due to an unexpected packet format

I got exception when I try to connect to my SMTP server using MailKit SmtpClient. BUT my mails have been sent successfully if I use System.Net.Mail.SmtpClient with the same parameters!

The exception message: An error occurred while attempting to establish an SSL or TLS connection.

The inner exception message: The handshake failed due to an unexpected packet format.

Questions

  • Why does MailKit.Net.Smtp.SmtpClient throw exception but System.Net.Mail.SmtpClient doesn't? What is the difference between them?
  • How to fix it?

Code

Initialize the parameters required for mail sending:

var host = "myhost.com";
var port = 2525;
var from = "from@mydomain.com";
var to = "to@mydomain.com";
var username = "from@mydomain.com";
var password = "myPassword";
var enableSsl = true;

Sending mail using System.Net.Mail.SmtpClient:

var client = new System.Net.Mail.SmtpClient
{
    Host = host,
    Port = port,
    EnableSsl = enableSsl,
    Credentials = new NetworkCredential(username, password)
};

client.Send(from, to, "subject", "body"); // success.

But when I try to connect to the host using MailKit with the same host and port, I got the exception:

var mailKitClient = new MailKit.Net.Smtp.SmtpClient();
mailKitClient.Connect(host, port, enableSsl); // it throws the exception.
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The problem is that you are connecting to a plain-text port and expecting SSL.

In MailKit, the true/false useSsl parameter is used to decide whether or not to connect in SSL mode or plain-text mode.

In System.Net.Mail, they don't support connecting in SSL mode, they only support upgrading a plain-text connection to SSL mode using the STARTTLS command once the connection has been established.

To overcome this, MailKit has a different Connect() method that takes an enum value SecureSocketOptions.

What you want is SecureSocketOptions.StartTls:

var mailKitClient = new MailKit.Net.Smtp.SmtpClient();
mailKitClient.Connect(host, port, SecureSOcketOptions.StartTls);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda