Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now

Send an Email via SMTP with MailKit in .Net 6.0

 

In this Article we will see how to send a email in dotnet 6.0 using MailKit email client library.

 

Step 1:- Install MailKit via NuGet

.NET Core CLI: dotnet add package MailKit

Visual Studio Package Manager Console: Install-Package MailKit

Step 2:- Send an HTML email in .NET 6.0

This code sends a simple HTML email using the Ethereal fake SMTP service, for quick testing you can create a temporary inbox at https://ethereal.email/ and copy the SMTP configuration options. There are instructions further below on how to use a few other popular SMTP providers - Gmail, Hotmail, Office 365 and AWS SES.

 

// create email message

var email = new MimeMessage();

email.From.Add(MailboxAddress.Parse("from_address@example.com"));

email.To.Add(MailboxAddress.Parse("to_address@example.com"));

email.Subject = "Test Email Subject";

email.Body = new TextPart(TextFormat.Html) { Text = "<h1>Example HTML Message Body</h1>" };

 

// send email

using var smtp = new SmtpClient();

smtp.Connect("smtp.ethereal.email", 587, SecureSocketOptions.StartTls);

smtp.Authenticate("[USERNAME]", "[PASSWORD]");

smtp.Send(email);

smtp.Disconnect(true);

 

Send a plain text email in .NET 6.0

This code sends the same email as above with a plain text body.

 

// create email message

var email = new MimeMessage();

email.From.Add(MailboxAddress.Parse("from_address@example.com"));

email.To.Add(MailboxAddress.Parse("to_address@example.com"));

email.Subject = "Test Email Subject";

email.Body = new TextPart(TextFormat.Plain) { Text = "Example Plain Text Message Body" };

 

// send email

using var smtp = new SmtpClient();

smtp.Connect("smtp.ethereal.email", 587, SecureSocketOptions.StartTls);

smtp.Authenticate("[USERNAME]", "[PASSWORD]");

smtp.Send(email);

smtp.Disconnect(true);

 

Send SMTP email with Gmail, Hotmail, Office 365 or AWS SES

To change the above code to use a different email provider simply update the host parameter (the first parameter) passed to the smtp.Connect() method, for example:

 

// gmail

smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);

 

// hotmail

smtp.Connect("smtp.live.com", 587, SecureSocketOptions.StartTls);

 

// office 365

smtp.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);

 

// aws ses (simple email service)

smtp.Connect("email-smtp.[AWS REGION].amazonaws.com", 587, SecureSocketOptions.StartTls)

 

 

Send email using Email Service interface

To encapsulate the email sending functionality so it's easy to send email from anywhere in your .NET app you can create an EmailService class and IEmailService interface like below.

namespace WebApi.Services;

 

using MailKit.Net.Smtp;

using MailKit.Security;

using Microsoft.Extensions.Options;

using MimeKit;

using MimeKit.Text;

using WebApi.Helpers;

 

public interface IEmailService

{

    void Send(string to, string subject, string html, string from = null);

}

 

public class EmailService : IEmailService

{

    private readonly AppSettings _appSettings;

 

    public EmailService(IOptions<AppSettings> appSettings)

    {

        _appSettings = appSettings.Value;

    }

 

    public void Send(string to, string subject, string html, string from = null)

    {

        // create message

        var email = new MimeMessage();

        email.From.Add(MailboxAddress.Parse(from ?? _appSettings.EmailFrom));

        email.To.Add(MailboxAddress.Parse(to));

        email.Subject = subject;

        email.Body = new TextPart(TextFormat.Html) { Text = html };

 

        // send email

        using var smtp = new SmtpClient();

        smtp.Connect(_appSettings.SmtpHost, _appSettings.SmtpPort, SecureSocketOptions.StartTls);

        smtp.Authenticate(_appSettings.SmtpUser, _appSettings.SmtpPass);

        smtp.Send(email);

        smtp.Disconnect(true);

    }

}

For more info on MailKit see https://github.com/jstedfast/MailKit.

 

------------------------------------------------------------------------------------------------

Share this

Related Posts

Previous
Next Post »

Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now