-1

I'm trying to send an E-Mail via Gmail using MailKit. This is my code:

using MailKit.Net.Smtp;

using (var client = new SmtpClient())
{
    client.Connect("smtp.gmail.com", 587, false);
    //...
}

But no matter what I try I always get the following exception:

System.IO.IOException: The authentication or decryption has failed.

I already tried to enable "unsafe apps" in my Google account, but that did not help. So far I could not find any working example that shows how to send mail via Gmail.

Edit: I just tried running the Code on WIndows (using .NET), and it worked perfectly. The E-Mail got sent right away. Only when I run it on Linux/OSX using Mono, it does not work.

Boris
  • 8,551
  • 25
  • 67
  • 120
  • Possible duplicate: http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail? – Lews Therin May 03 '16 at 19:45
  • the error is giving you all the hints.. `authentication` where are you passing the username and password for gmail account.. also there are `TONS of working examples on how to do this using standard .net's smtp class.. try going some google searches on how to send email to gmail using smtp – MethodMan May 03 '16 at 19:45
  • @MethodMan: The username and password are provided AFTER calling the connect method. Have you ever used MailKit? As for the .NET Smtp class: I tried four different examples found here on Stack Overflow. NONE of them worked. They also produce a similar authentification exception. – Boris May 03 '16 at 19:51
  • @Sylverac: How can it be a duplicate if it uses a completely different framework (MailKit, not .NET SMTP classes)? – Boris May 03 '16 at 19:56
  • 1
    Perhaps this helps. http://www.mimekit.net/docs/html/M_MailKit_Net_Smtp_SmtpClient_Connect_1.htm, client.Connect ("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect); – Jose Luis May 03 '16 at 19:58
  • @Jose: No, it still throws "The authentication or decryption has failed". – Boris May 04 '16 at 04:27

1 Answers1

1

I just found the answer! Adding the following line of code to the beginning of my program solved it:

System.Net.ServicePointManager.ServerCertificateValidationCallback +=
(o, certificate, chain, errors) => true;

Now MailKit sends the E-Mails as it should. So does the .NET/Mono SMTP class.

Boris
  • 8,551
  • 25
  • 67
  • 120