Mono BLOG

Blog about Microsoft technologies (.NET, ASP.NET Core, Blazor, EF Core, WPF, TypeScript, etc.)

Modern Email Handling in .NET with MailKit: A Comprehensive Guide

in .NET

A practical implementation guide for sending emails in .NET applications using the recommended MailKit library, featuring HTML content embedding and dependency injection patterns.

Core Implementation

1. NuGet Package Installation

Install-Package MailKit

2. Email Service Interface

using MimeKit;
using System.Threading.Tasks;

public interface IEmailService
{
    /// <summary>
    /// Sends an email message
    /// </summary>
    /// <param name="message">Constructed MIME message</param>
    Task SendEmailAsync(MimeMessage message);
}

3. MailKit Implementation

using MailKit.Net.Smtp;
using MimeKit;
using System.Threading.Tasks;

public class EmailService : IEmailService
{
    private const string SmtpHost = "smtp.example.com";
    private const int SmtpPort = 465;
    private const string SenderEmail = "noreply@example.com";
    private const string SenderPassword = "your_password";
    private const string SenderName = "Notification Service";

    public async Task SendEmailAsync(MimeMessage message)
    {
        using var client = new SmtpClient();
      
        // Configure SSL connection
        await client.ConnectAsync(SmtpHost, SmtpPort, true);
      
        // Remove OAuth2 authentication if not needed
        client.AuthenticationMechanisms.Remove("XOAUTH2");
      
        // Authenticate with SMTP server
        await client.AuthenticateAsync(SenderEmail, SenderPassword);
      
        // Send constructed message
        await client.SendAsync(message);
        await client.DisconnectAsync(true);
    }
}

Implementation Patterns

1. Basic Email Composition

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Service Name", "service@example.com"));
message.To.Add(new MailboxAddress("Recipient Name", "recipient@example.com"));

message.Subject = "System Notification";
message.Body = new TextPart("html") 
{
    Text = "<h1>Important Update</h1>" +
           $"<p>Current server time: {DateTime.Now:HH:mm:ss}</p>"
};

2. Dependency Injection Setup

// In Startup.cs or Program.cs
services.AddSingleton<IEmailService, EmailService>();

3. Email Client Usage

public class NotificationService
{
    private readonly IEmailService _emailService;

    public NotificationService(IEmailService emailService)
    {
        _emailService = emailService;
    }

    public async Task SendAlertAsync(string recipient)
    {
        var message = new MimeMessage();
        // Message configuration
        await _emailService.SendEmailAsync(message);
    }
}

Advanced Features

1. Embedded Images

var builder = new BodyBuilder();
var image = builder.LinkedResources.Add("path/to/logo.png");
image.ContentId = MimeUtils.GenerateMessageId();

builder.HtmlBody = $@"<img src='cid:{image.ContentId}'>
                     <p>Embedded corporate logo</p>";

var message = new MimeMessage 
{
    Body = builder.ToMessageBody()
};

2. Batch Recipients Handling

var recipients = new List<MailboxAddress>
{
    new MailboxAddress("Tech Team", "tech@example.com"),
    new MailboxAddress("Support", "support@example.com")
};

message.To.AddRange(recipients);

Security Recommendations

  1. Credential Management:

    // Store in secure configuration
    var config = new ConfigurationBuilder()
        .AddUserSecrets<EmailService>()
        .Build();
    
  2. Connection Security:

    // Always use SSL for production
    await client.ConnectAsync(host, port, SecureSocketOptions.SslOnConnect);
    
  3. Content Validation:

    // Sanitize HTML content
    var sanitizedContent = HtmlSanitizer.Sanitize(rawHtml);
    

Key Advantages: ✅ Modern replacement for obsolete SmtpClient ✅ Full support for MIME standards ✅ Async/Await pattern implementation ✅ Cross-platform compatibility ✅ Comprehensive security features

Production Notes:

  • Implement proper error handling with retry policies
  • Use IOptions pattern for configuration management
  • Consider email queueing for high-volume systems
  • Implement DKIM/SPF records validation

Official Resources: