Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send SMTP email using System.Net.Mail via Exchange Online (Office 365)

We are testing the new Office 365 beta, and i have a mail account on the Exchange Online service. Now I'm trying to connect a LOB application that can send smtp emails from my test account.

However the Exchange 365 platform requires TLS encryption on port 587, and there is a 'feature' of System.Net.Mail that does not permit Implicit SSL encryption.

Has anyone managed to get C# sending mails via this platform?

I have the following basic code that should send the mail - any advice would be appreciated.

SmtpClient server = new SmtpClient("ServerAddress"); server.Port = 587; server.EnableSsl = true; server.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); server.Timeout = 5000; server.UseDefaultCredentials = false;  MailMessage mail = new MailMessage(); mail.From = new MailAddress("recipent@anyaddress"); mail.To.Add("[email protected]"); mail.Subject = "test out message sending"; mail.Body = "this is my message body"; mail.IsBodyHtml = true;  server.Send(mail); 
like image 212
Adam Stewart Avatar asked Jun 05 '11 17:06

Adam Stewart


People also ask

Can I use Office 365 as an SMTP server?

Microsoft 365 or Office 365 SMTP relay uses a connector to authenticate the mail sent from your device or application. This authentication method allows Microsoft 365 or Office 365 to relay those messages to your own mailboxes and external recipients.

How do I enable SMTP in Exchange Online?

Open the Microsoft 365 admin center and go to Users > Active users. Select the user, and in the flyout that appears, click Mail. In the Email apps section, click Manage email apps. Verify the Authenticated SMTP setting: unchecked = disabled, checked = enabled.


2 Answers

Fixed a few typos in the working code above:

MailMessage msg = new MailMessage(); msg.To.Add(new MailAddress("[email protected]", "SomeOne")); msg.From = new MailAddress("[email protected]", "You"); msg.Subject = "This is a Test Mail"; msg.Body = "This is a test message using Exchange OnLine"; msg.IsBodyHtml = true;  SmtpClient client = new SmtpClient(); client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("your user name", "your password"); client.Port = 587; // You can use Port 25 if 587 is blocked (mine is!) client.Host = "smtp.office365.com"; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.EnableSsl = true; try {     client.Send(msg);     lblText.Text = "Message Sent Succesfully"; } catch (Exception ex) {     lblText.Text = ex.ToString(); } 

I have two web applications using the above code and both work fine without any trouble.

like image 71
Sanjeev Avatar answered Sep 20 '22 17:09

Sanjeev


In year of 2020, these code seems to return exception as

System.Net.Mail.SmtpStatusCode.MustIssueStartTlsFirst or The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

This code is working for me.

            using (SmtpClient client = new SmtpClient()             {                 Host = "smtp.office365.com",                 Port = 587,                 UseDefaultCredentials = false, // This require to be before setting Credentials property                 DeliveryMethod = SmtpDeliveryMethod.Network,                 Credentials = new NetworkCredential("[email protected]", "password"), // you must give a full email address for authentication                  TargetName = "STARTTLS/smtp.office365.com", // Set to avoid MustIssueStartTlsFirst exception                 EnableSsl = true // Set to avoid secure connection exception             })             {                  MailMessage message = new MailMessage()                 {                     From = new MailAddress("[email protected]"), // sender must be a full email address                     Subject = subject,                     IsBodyHtml = true,                     Body = "<h1>Hello World</h1>",                     BodyEncoding = System.Text.Encoding.UTF8,                     SubjectEncoding = System.Text.Encoding.UTF8,                  };                 var toAddresses = recipients.Split(',');                 foreach (var to in toAddresses)                 {                     message.To.Add(to.Trim());                 }                  try                 {                     client.Send(message);                 }                 catch (Exception ex)                 {                     Debug.WriteLine(ex.Message);                 }             } 
like image 36
Jirapong Avatar answered Sep 20 '22 17:09

Jirapong