2

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

jabir saidi
  • 106
  • 2
  • 8
  • 1
    https://sendgrid.com/docs/for-developers/sending-email/v2-csharp-code-example/ – mjwills Aug 27 '19 at 14:20
  • 2
    That's not quite what I need since I'm using Mailkit. I'm starting to think that maybe SendGrid isn't the most appropriate service for what I'm trying to do. – jabir saidi Aug 27 '19 at 14:35

2 Answers2

3

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.

EzLo
  • 12,897
  • 10
  • 28
  • 33
  • And unlike the Telnet example on their site you *don't* need to Base64 encode either username or API key. – Alan B Apr 21 '21 at 13:57
1

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!

jabir saidi
  • 106
  • 2
  • 8