Home Send mail from Outlook using SmtpClient
Post
Cancel

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:

  • sender@outlook.com: The Outlook account to send the letter.
  • 123abc!: Outlook account password.
  • receiver@gmail.com: The email address of the recipient.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
}

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

This post is licensed under CC BY 4.0 by the author.

Single-Responsibility Principle (SRP)

Using Uart on Stm32f407g

Comments powered by Disqus.