1

I'm on this bug since fews days and maybe someone can give some some clues about this issue :

MailKit.Net.Smtp.SmtpStream.ReadAheadAsync(Boolean doAsync, CancellationToken cancellationToken) at MailKit.Net.Smtp.SmtpStream.ReadResponseAsync(Boolean doAsync, CancellationToken cancellationToken) at MailKit.Net.Smtp.SmtpClient.ConnectAsync(String host, Int32 port, SecureSocketOptions options, Boolean doAsync, CancellationToken cancellationToken) at SafetyStudio.Services.Courriels.EmailService.Send(EmailMessage emailMessage) in C:\Users\Louis\Source\Repos\SafetyStudio\SafetyStudio\Services\Courriels\EmailService.cs:line 58 | The SMTP server has unexpectedly disconnected. ! MailKit

        //Be careful that the SmtpClient class is the one from Mailkit not the framework!
        using (var emailClient = new SmtpClient())
        {
            try
            {               
                //The last parameter here is to use SSL (Which you should!)
                await emailClient.ConnectAsync(_emailConfiguration.SmtpServer, _emailConfiguration.SmtpPort, MailKit.Security.SecureSocketOptions.None);
                //Remove any OAuth functionality as we won't be using it. 
                emailClient.AuthenticationMechanisms.Remove("XOAUTH2");
                await emailClient.AuthenticateAsync(_emailConfiguration.SmtpUsername, _emailConfiguration.SmtpPassword);
                await emailClient.SendAsync(message);
                await emailClient.DisconnectAsync(true);
                emailClient.Dispose();
            }
            catch (Exception ex)
            {
                this.logger.LogError($"Erreur dans l'envoi d'un courriel {ex}");
                return ex;
            }
            return null;
        }

I try with port 25 and same error. 587 for TSL. I have a valid certificate.

  "EmailConfiguration": {
    "SmtpServer": "servername",
    "SmtpPort": 587,
    "SmtpUsername": "aaaaaaaaaaa",
    "SmtpPassword": "ssssssssss!",
    "PopServer": "popserver",
    "PopPort": 995,
    "PopUsername": "popusername",
    "PopPassword": "poppassword"
jps
  • 20,041
  • 15
  • 75
  • 79
Serge
  • 51
  • 4

1 Answers1

-2

An unexpected disconnection is just part and parcel of writing network apps.

You just have to deal with it and reconnect.

If it continues to fail, then it might be because the server is experiencing problems or it might even be that it has blocked you for connecting to it "too often" (some public SMTP servers do this to effectively block spammers from abusing their service).

jstedfast
  • 35,744
  • 5
  • 97
  • 110