DEV Community

Jack Lin
Jack Lin

Posted on • Edited on

Send mail from Outlook using SmtpClient

From James' comment:
DO NOT USE SMTPCLIENT IN PRODUCTION. It doesn't support modern protocols and is now obsolete. Use Mailkit or FluentEmail instead.
Read the Remarks section of Microsoft's docs for more info


Execution environment: Windows 11, .NET 6.0

Create a console application, paste the code in Program.cs and replace the following strings:

using System.Net.Mail;
using System.Net;

var message = new MailMessage();
message.From = new MailAddress("sender@outlook.com");
message.To.Add(new MailAddress("receiver@gmail.com"));
message.IsBodyHtml = true;
message.Subject = "My first smtp email!";
message.Body =
@"
<!DOCTYPE html>
<html>
<body>
    <h3>Hello Jack:</h3>
    <p>This is a Hello World email!</p>
</body>
</html>
";

try
{
    var client = new SmtpClient("smtp-mail.outlook.com", 587);
    client.Credentials = new NetworkCredential("sender@outlook.com", "123abc!");
    client.EnableSsl = true;
    client.Send(message);
}
catch (Exception e)
{
    Console.WriteLine(e);
}
Enter fullscreen mode Exit fullscreen mode

Note that the letter may be considered spam the first time it is sent, so it will need to be manually moved to the inbox. The letter looks like the following:

Image description

Top comments (2)

Collapse
 
henryjs profile image
James • Edited

DO NOT USE SMTPCLIENT IN PRODUCTION. It doesn't support modern protocols and is now obsolete. Use Mailkit or FluentEmail instead.
Read the Remarks section of Microsoft's docs for more info

Collapse
 
blueskyson profile image
Jack Lin • Edited

Thanks for the information!