Office 365: Failure sending mail in C#
By FoxLearn 1/14/2024 10:41:51 AM 8.03K
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 }; client.Send(mail); }
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
Host: smtp.office365.com
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
- How to make a file copy progress in C#
- How to set DataSource for RDLC Report using Report Viewer in C#
- How to Change report in ReportViewer at runtime in C#
- How to encode and decode a base64 string in C#
- Windows Forms: How to Search data in Database using C#
- Windows Forms: How to check if file exists on Server in C#