jabir saidi
jabir saidi

Reputation: 122

Using Mailkit : "The SMTP server has unexpectedly disconnected."

I am trying to use a free SMTP relay from SendGrid to send emails from my ASP.NET application. I can connect to the server, but when I try to authenticate, I get this error : "The SMTP server has unexpectedly disconnected."

using (var client = new SmtpClient())
        {
            client.ServerCertificateValidationCallback =
                (sender, certificate, certChainType, errors) => true;
            client.AuthenticationMechanisms.Remove("XOAUTH2");

            // connection
            client.Connect("smtp.host", 465, true);
            client.Authenticate("UserName", "Password");//error occurs here

            client.Send(email);
            client.Disconnect(true);
        }

Once again, I can connect without any problem, but when I try to authenticate, I get the previously mentionned error...

Any suggestions?

Cheers

Upvotes: 3

Views: 25754

Answers (4)

Sum1Unknown
Sum1Unknown

Reputation: 1060

In my case this was an IP address problem in my own set security level for my IIS smtp.

Because I had a whitelist of IPs that were allowed to connect and relay in my company I forgot to set an IP for my VPN tunnel while working from home.

After allowing my Laptops IP in the connection and relay preferences on the IIS SMTP I got rid of the error.

Upvotes: 0

Digbyswift
Digbyswift

Reputation: 10400

I've had this issue several times in the past (including today) and it has always been down to the SendGrid's IP access rejecting attempts to send mail. So really this is mainly a reminder to myself because I keep ending up back here! :)

Check the IP access here https://app.sendgrid.com/settings/access

Upvotes: 1

EzLo
EzLo

Reputation: 14189

You have to supply:

  • Username: is apikey (as a hard-coded value 'apikey').
  • Password: is the apikey you generated from the web, which is a big hashy-like string.

You can find this on their docs. But it was hard to find.

Upvotes: 9

jabir saidi
jabir saidi

Reputation: 122

I solved my issue changing from SendGrid to gooogle's free SMTP service for all of their users. Simply follow the steps here and you should be good to go!

Upvotes: 0

Related Questions