Office 365: Failure sending mail in C#

This post show you How to solve office 365 SMTP Error: Failure sending mail in C#

If you are facing problem sending mail with office365 in c#. You spend a lot of time to solve, the following article will guide you how to handle it.

How to: Programmatically send email - Visual Studio

Use Visual Studio to programmatically send an email from office 365 mailbox in c# using SMTP like me below. If your source code is the same as mine then you don't need to create anything else.

//failure sending mail c# smtpclient
public void SendMail(string subject, string to, string displayName, string message)
    var credentials = new NetworkCredential(account.Email, account.Password);
    var mail = new MailMessage()
        From = new MailAddress(account.Email, displayName),
        Subject = subject,
        Body = message,
    mail.IsBodyHtml = true;//If you want to send mail in html format
    mail.To.Add(new MailAddress(to));
    var client = new SmtpClient() { Port = account.Port ?? 0, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Host = account.Server, EnableSsl = true, Credentials = credentials };

If you try to send mail via office365 smtp server but it prompts with "Failure sending mail". You are unable to send mail via office 365 smtp.

office 365 smtp settings

account.Email: [your email account]

account.Password: [your email password]

Port: 587


To solve the problem "Unable to read data from the transport connection: net_io_connectionclosed" you need to enable Tls.

//Unable to send email from Office 365 using c# SmtpClient
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

Just add the code above at the start of your function. Because TLS 1.0/1.1 is deprecated in 2022, you need to switch to TLS 1.2