How to Send Email from ASP.NET Applications
Updated: This guide covers both .NET Core/8/9/10 and .NET Framework 4.x email sending.
SMTP Settings for Adaptive Web Hosting
| Setting | Value |
|---|---|
| SMTP Server | mail.yourdomain.com |
| Port (TLS) | 587 |
| Port (SSL) | 465 |
| Username | Full email address |
| Authentication | Required |
ASP.NET Core / .NET 8/9/10 (Recommended)
Option 1: Using MailKit (Recommended)
First, install the NuGet package:
dotnet add package MailKit
Then send email:
using MailKit.Net.Smtp;
using MimeKit;
public async Task SendEmailAsync(string to, string subject, string body)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Your Name", "from@yourdomain.com"));
message.To.Add(new MailboxAddress("", to));
message.Subject = subject;
message.Body = new TextPart("html") { Text = body };
using var client = new SmtpClient();
await client.ConnectAsync("mail.yourdomain.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
await client.AuthenticateAsync("from@yourdomain.com", "your-password");
await client.SendAsync(message);
await client.DisconnectAsync(true);
}
Option 2: Using System.Net.Mail (Built-in)
using System.Net;
using System.Net.Mail;
public async Task SendEmailAsync(string to, string subject, string body)
{
using var message = new MailMessage();
message.From = new MailAddress("from@yourdomain.com", "Your Name");
message.To.Add(new MailAddress(to));
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = true;
using var client = new SmtpClient("mail.yourdomain.com", 587)
{
Credentials = new NetworkCredential("from@yourdomain.com", "your-password"),
EnableSsl = true
};
await client.SendMailAsync(message);
}
Configuration in appsettings.json
{
"EmailSettings": {
"SmtpServer": "mail.yourdomain.com",
"Port": 587,
"Username": "from@yourdomain.com",
"Password": "your-password",
"FromEmail": "from@yourdomain.com",
"FromName": "Your Application"
}
}
Email Service with Dependency Injection
// In Program.cs
builder.Services.Configure<EmailSettings>(
builder.Configuration.GetSection("EmailSettings"));
builder.Services.AddTransient<IEmailService, EmailService>();
// EmailService.cs
public class EmailService : IEmailService
{
private readonly EmailSettings _settings;
public EmailService(IOptions<EmailSettings> settings)
{
_settings = settings.Value;
}
public async Task SendEmailAsync(string to, string subject, string body)
{
// Use MailKit or SmtpClient as shown above
}
}
ASP.NET Framework 4.x (Legacy)
using System.Net;
using System.Net.Mail;
MailMessage message = new MailMessage();
message.From = new MailAddress("from@yourdomain.com");
message.To.Add(new MailAddress("to@example.com"));
message.Subject = "Test Email";
message.Body = "This is a test email.";
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.yourdomain.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("from@yourdomain.com", "your-password");
smtp.Send(message);
Web.config Configuration
<system.net>
<mailSettings>
<smtp from="from@yourdomain.com">
<network host="mail.yourdomain.com"
port="587"
userName="from@yourdomain.com"
password="your-password"
enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
Troubleshooting
| Error | Solution |
|---|---|
| Authentication failed | Verify email and password; use full email as username |
| Connection timeout | Try port 465 with SSL instead of 587 with TLS |
| Certificate error | Ensure SSL/TLS is enabled; check certificate validity |
| Relay denied | Must authenticate; from address must match account |
Security Best Practices
- Never hardcode passwords in code - use configuration
- Always use TLS/SSL encryption (port 587 or 465)
- Use environment variables for production credentials
- Implement rate limiting to prevent abuse