Send Microsoft Office 365 Email SMTP Testing to .net

I have an email account with Exchange Online. Now I'm trying to check if I can send letters to clients (on varoius domains and in Microsoft Office 365) using a C # application

I tried to execute the code below, but I get an error

"The remote certificate is invalid according to the validation procedure."

MailMessage mail = null; mail = new MailMessage(); string[] strToList = " abc@gmail.com " foreach (string strID in strToList) { if (strID != null) { mail.To.Add(new MailAddress(strID)); } } mail.From = " demo@onmicrosoft.com "; mail.Subject = "testing" mail.IsBodyHtml = true; mail.Body = "mail body"; SmtpClient client = new SmtpClient("smtp.outlook.office365.com"); client.Port = 587; client.EnableSsl = true; client.UseDefaultCredentials = false; NetworkCredential cred = new System.Net.NetworkCredential(" demo@onmicrosoft.com ", "mypassword"); client.Credentials = cred; client.Send(mail); 

I ask for advice if I do something wrong. Thank you very much in advance.

+6
source share
6 answers

this works for me (edited from source )


  ThreadPool.QueueUserWorkItem(t => { SmtpClient client = new SmtpClient("smtp.office365.com",587); client.EnableSsl = true; client.Credentials = new System.Net.NetworkCredential(" xxx@yyy.com ", "password"); MailAddress from = new MailAddress(" xxx@yyy.com ", String.Empty, System.Text.Encoding.UTF8); MailAddress to = new MailAddress(" xxx@yyy.com "); MailMessage message = new MailMessage(from, to); message.Body = "The message I want to send."; message.BodyEncoding = System.Text.Encoding.UTF8; message.Subject = "The subject of the email"; message.SubjectEncoding = System.Text.Encoding.UTF8; // Set the method that is called back when the send operation ends. client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); // The userState can be any object that allows your callback // method to identify this send operation. // For this example, I am passing the message itself client.SendAsync(message, message); }); private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) { // Get the message we sent MailMessage msg = (MailMessage)e.UserState; if (e.Cancelled) { // prompt user with "send cancelled" message } if (e.Error != null) { // prompt user with error message } else { // prompt user with message sent! // as we have the message object we can also display who the message // was sent to etc } // finally dispose of the message if (msg != null) msg.Dispose(); } 
+11
source

Try using:

 ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; 

This code will allow you to accept invalid certificates.

+2
source

Try smtp.office365.com instead of smtp.outlook.office365.com

+2
source

In some cases, TLS authentication can cause problems using smtp.office365.com as SMTP from C #. Try the following line before the Send (msg) statement (override .TargetName):

 client.TargetName = "STARTTLS/smtp.office365.com"; 

It works for me

+2
source

Error

The SMTP server requires a secure connection or the client was not authenticated. Server response: 5.7.1 The client has not been authenticated

often happens when the associated user account password has expired or the account is locked. Try setting "Never expire user password" in Active Directory if this does not violate your company password policy :) This happened to me during testing with o365 Exchange Online A / c.

+1
source

This is also the best way to send mail. I tried this in my project and worked great.

  SmtpClient client = new SmtpClient("smtp.office365.com", 587); client.EnableSsl = true; client.Credentials = new System.Net.NetworkCredential(" From@mail.com ", " sdsd@12345 "); MailAddress from = new MailAddress("From Address Ex From@mail.com ", String.Empty, System.Text.Encoding.UTF8); MailAddress to = new MailAddress("From Address Ex To@mail.com "); MailMessage message = new MailMessage(from, to); message.Body = "This is your body message"; message.BodyEncoding = System.Text.Encoding.UTF8; message.Subject = "Subject"; message.SubjectEncoding = System.Text.Encoding.UTF8; client.Send(message); 
0
source

All Articles