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
1
| Install-Package MailKit
|
2. Email Service Interface
1
2
3
4
5
6
7
8
9
10
11
| 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
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 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
1
2
3
4
5
6
7
8
9
10
| 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
1
2
| // In Startup.cs or Program.cs
services.AddSingleton<IEmailService, EmailService>();
|
3. Email Client Usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| 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
1
2
3
4
5
6
7
8
9
10
11
| 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
1
2
3
4
5
6
7
| var recipients = new List<MailboxAddress>
{
new MailboxAddress("Tech Team", "tech@example.com"),
new MailboxAddress("Support", "support@example.com")
};
message.To.AddRange(recipients);
|
Security Recommendations
Credential Management:
1
2
3
4
| // Store in secure configuration
var config = new ConfigurationBuilder()
.AddUserSecrets<EmailService>()
.Build();
|
Connection Security:
1
2
| // Always use SSL for production
await client.ConnectAsync(host, port, SecureSocketOptions.SslOnConnect);
|
Content Validation:
1
2
| // 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: