Send automated email in C#
This C# program uses smtp to send emails. It utilizes the tool Mailtrap to send and receive test emails.
On this page
Prerequisite β
The example code below utilizes Mailtrap SMTP and MailKit
.Net Library.
MailTrap is a useful tool for testing email functionality. It traps email so they’re not delivered to the real recipients.
Here’s how to setup and use Mailtrap.
- Go to Mailtrap and signup for free. Fill out the necessary details.
- When asked “What do you want to use Mailtrap for?”, just tick “Testing Emails”.
- Click “Start Testing”.
- There’ll be a number of ways to integrate and use Mailtrap, for now, we’ll use SMTP.
- Take note of the following SMTP settings from MailTrap as you’ll be using them in the code below:
- SMTP Server:
sandbox.smtp.mailtrap.io
- Port:
2525
- Username: Your Mailtrap SMTP Username
- Password: Your Mailtrap SMTP Password
- SMTP Server:
- Take note of the following SMTP settings from MailTrap as you’ll be using them in the code below:
MailKit
is a .Net Library used for sending, receiving, and managing emails using different email protocols. In this particular case, we’ll be using SMTP.
here’s MailKit repository and documentation
To add MailKit to C# code, install the Library from Nuget Package.
- From Visual Studio’s menu bar, select “Tools”. Then go down to “Nuget Package Manager” and select “Manage Nuget Packages for Solution”.
- Browse for the “MailKit” Library.
- “Install” the Library to your project.
Source Code π»
using MailKit.Net.Smtp;
using MimeKit;
namespace new_email_tool
{
internal class Program
{
static void Main(string[] args)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress("FromMyNotes", "do-not-reply@frommynotes.com"));
message.To.Add(new MailboxAddress("User", "user@example.com"));
message.Subject = "Thanks for Subscribing!";
message.Body = new TextPart("html")
{
Text = "<h1>Hi, User!</h1>" +
"<p>Thank you for subscribing to frommynotes newsletter.</p>" +
"<p><strong>Welcome!</strong></p>"
};
using (var client = new SmtpClient())
{
try
{
client.Connect("sandbox.smtp.mailtrap.io", 2525, MailKit.Security.SecureSocketOptions.StartTls);
client.Authenticate("your-mailtrap-inbox-username-here", "your-mailtrap-inbox-password-here");
client.Send(message);
Console.WriteLine("Email sent successfully through Mailtrap.");
}
catch (Exception ex)
{
Console.WriteLine($"Error sending email: {ex.Message}");
}
finally
{
client.Disconnect(true);
}
}
}
}
}
EXPLANATION
- MimeKit Library is also used in the code above. MimeKit is designed for creating, parsing, and processing MIME (Multipurpose Internet Mail Extensions) messages.
- It represents the email message you’re sending, which includes
From
,To
,Subject
, andBody
.
- It represents the email message you’re sending, which includes
SmtpClient
is used to send the email. It connects to Mailtrap’s SMTP Server (smtp.mailtrap.io) on port 2525 with StartTls encryption for secure communication.
OUPUT
To check that the email has been sent. Go to your MailTrap’s account. Navigate to the “Email Testing” menu, then “Inbox”. You should see the sent email there.
NEXT STEPSβ
Instead of hardcoding the credentials (username, password), we’ll store them in a configuration file.
π end of document π