DEV Community

Jack Lin
Jack Lin

Posted on • Edited on

2

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

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!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more