none
Unable to send email on Outlook 365 using SmtpClient & .Net 4.x RRS feed

  • Question

  • Hi, I write it today because I have an SMTP related bug that's stumbling me.

    In fact, when I try to use System.Net.Mail (or other external mail plugins) with .NET 4.x runtime support to send mail on office365 SMTP server I get that error :

    System.Net.Mail.SmtpException: Message could not be sent. ---> System.AggregateException: One or more errors occurred. ---> System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception.

    However, when I set my project in .NET 3.x support that error disappear.
    Likewise, when I try another SMTP server as Gmail one, it works perfectly with 4.x support.

    Here is my code :

    void SendMailTest() { try { MailMessage mail = new MailMessage(); #region MailLayout mail.From = new System.Net.Mail.MailAddress("myadress"); mail.To.Add("youradress"); mail.Subject = "test_sub"; mail.Body = "test_body"; mail.Attachments.Add(new Attachment("myPDF")); #endregion #region SMTPConfig SmtpClient smtpServer = new SmtpClient("smtp.office365.com"); smtpServer.Port = 587; smtpServer.Credentials = new NetworkCredential("myAdress", "myPassword") as ICredentialsByHost; smtpServer.EnableSsl = true; ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; #endregion smtpServer.Send(mail); } catch (Exception e){ Debug.Log(e.Message); } }

    Thank you for reading, I hope that someone would assist me.


    Thursday, July 26, 2018 9:16 AM

All replies

  • An AggregateException is a wrapper around one or more actual errors. Use the InnerExceptions property to get to the error(s) that occurred.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, July 26, 2018 1:50 PM
    Moderator
  • Thank you, Michael, after using InnerExceptions, it seems that there are 3 consecutive exceptions.

    First of all a TlsException: Handshake failed is thrown.

    After it, I got an AuthenticationException: A call to SSPI failed.

    Finally, I got a SmtpException: Message could not be sent.

    Have you any idea?


    Thursday, July 26, 2018 2:16 PM
  • TLS exception means you're using a version of SSL that it doesn't support. Almost all web apps have switched to TLS 1.2. So you'll need to modify your code to work with it. .NET 3.x didn't support that so I'm not sure why it would work in your 3.x version at all after the switch over. In versions of .NET prior to 4.7 had to be opted in. With .NET 4.7 or higher it'll use whatever the OS is configured to use. Here's an article on what you need to do for your target framework.

    I don't know about your certificate logic. Do you need that? I don't know that I've ever seen anyone have to send certs to Office365. Setting the host, port, credentials and SSL should be sufficient.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, July 26, 2018 2:33 PM
    Moderator