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

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 = “”;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
lblText.Text = “Message Sent Succesfully”;
catch (Exception ex)
lblText.Text = ex.ToString();

Liked it? Take a second to support admin on Patreon!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.