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

  • Thread starter Thread starter Nicsi
  • Start date Start date
N

Nicsi

Guest
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.

Continue reading...
 
Back
Top