textlize pricing account
Sending Email Correctly in .NET
Cover

00:20:55

Sending Professional Emails in .NET: Beyond the Obsolete SmtpClient

Core Insight: Modern .NET email requires abandoning System.Net.Mail.SmtpClient in favor of robust libraries like MailKit and MimeKit that support contemporary protocols and MIME standards.

Why SmtpClient Is No Longer Viable

Microsoft's documentation explicitly marks SmtpClient as obsolete for new development. Key limitations include:

  • Lacks support for modern authentication protocols
  • Missing critical security features required by contemporary email providers
  • Inadequate handling of MIME formatting standards

The Modern Alternative: MailKit + MimeKit

These .NET Foundation-backed MIT-licensed libraries provide:

MimeKit

Creates and parses MIME-compliant messages with proper:

  • Encoding for attachments
  • Character set handling
  • Multi-part content structures

MailKit

Handles SMTP communication with support for:

  • Modern authentication (OAuth2)
  • Secure connections (SSL/TLS)
  • Async operations

Implementing Email in .NET: Step-by-Step

1. Project Setup

dotnet new console -o MailDemo
cd MailDemo
dotnet add package MailKit

2. Core Message Construction

using MimeKit;
using MailKit.Net.Smtp;

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Sender", "sender@domain.com"));
message.To.Add(new MailboxAddress("Recipient", "recipient@domain.com"));
message.Subject = "Email Subject";

// Plain text body
message.Body = new TextPart(TextFormat.Plain) { 
  Text = "This is a plain text email body" 
};

3. Sending with MailKit

using var client = new SmtpClient();
await client.ConnectAsync("smtp.server.com", 587, SecureSocketOptions.StartTls);
await client.AuthenticateAsync("username", "password");
await client.SendAsync(message);
await client.DisconnectAsync(true);

Advanced MIME Techniques

Multi-part Messages

Simultaneous plain text and HTML versions:

var builder = new BodyBuilder();
builder.TextBody = "Plain text content";
builder.HtmlBody = "<p>HTML content</p>";
message.Body = builder.ToMessageBody();

Attachment Handling

builder.Attachments.Add("filepath.pdf");
// For inline images:
var image = builder.LinkedResources.Add("image.jpg");
image.ContentId = MimeUtils.GenerateMessageId();
builder.HtmlBody = $@"<img src='cid:{image.ContentId}'>";

Testing with MailPit

The open-source MailPit tool provides:

  • Local SMTP server (port 1025)
  • Web interface for inspecting emails (port 8025)
  • Zero external dependencies

Test Configuration:

await client.ConnectAsync("localhost", 1025);

Production Considerations

  • Templating: Use Razor or MJML for maintainable HTML emails
  • Background Processing: Offload sending to background services
  • Security: Implement SPF, DKIM, and DMARC records
  • Testing: Unit test email content generation logic

Key Takeaways:

  • MailKit/MimeKit replace obsolete .NET email components
  • Proper MIME structure is essential for modern email delivery
  • Local testing tools like MailPit accelerate development
  • Production deployment requires DNS configuration and security protocols
© 2025 textlize.com. all rights reserved. terms of services privacy policy