Email Service Configuration
The MailService provides email functionality using SMTP protocol. It supports:
Account activation emails
Password reset emails
Welcome emails
Configuration Options
Configure email settings in appsettings.json:
{
"Security": {
"Email": {
"From": "your-app@localhost",
"BaseUrl": "http://127.0.0.1:8080",
"Smtp": {
"Host": "localhost",
"Port": 25,
"Username": "",
"Password": "",
"Protocol": "smtp",
"UseSsl": true,
"Timeout": 5000,
"Debug": true
}
}
}
}
Development Configuration
For local development, you can use:
MailHog - Recommended for Mac/Linux
Papercut SMTP - Windows alternative
Production Configuration
Common SMTP providers:
Gmail SMTP:
"Smtp": { "Host": "smtp.gmail.com", "Port": 587, "UseSsl": true }
Amazon SES:
"Smtp": { "Host": "email-smtp.us-east-1.amazonaws.com", "Port": 587, "UseSsl": true }
Usage Examples
Activation Email:
await _mailService.SendActivationEmail(
email: "user@example.com",
name: "John Doe",
activationKey: "activation-key-here"
);
Password Reset:
await _mailService.SendPasswordResetMail(
email: "user@example.com",
name: "John Doe",
resetKey: "reset-key-here"
);
Welcome Email:
await _mailService.SendCreationEmail(
email: "user@example.com",
name: "John Doe"
);
Security Best Practices
SMTP Security
Enable TLS/SSL encryption (
UseSsl: true)Use environment variables for credentials:
"Username": "${SMTP_USER}", "Password": "${SMTP_PASSWORD}"
Rotate SMTP credentials regularly
Use dedicated sending domains
Content Security
Sanitize all user input in templates
Include SPF/DKIM records for your domain
Set up DMARC policy
Include unsubscribe links (GDPR compliance)
Error Handling
Implement exponential backoff for retries
Log delivery failures with correlation IDs
Monitor bounce rates and spam reports
Testing
Unit Testing:
[Fact]
public async Task Should_Send_Activation_Email()
{
// Arrange
var mockMailService = new Mock<IMailService>();
// Act
await mailService.SendActivationEmail("test@example.com", "Test User", "key123");
// Assert
mockMailService.Verify(x => x.SendActivationEmail(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>()
), Times.Once);
}
Integration Testing:
[Fact]
public async Task Should_Connect_To_SMTP_Server()
{
// Use MailHog for integration tests
var mailConfig = new MailConfiguration
{
Host = "localhost",
Port = 1025
};
}
Troubleshooting Guide
Connection Issues
Verify network connectivity to SMTP server
Check firewall rules for SMTP ports (25, 465, 587)
Validate SSL certificate if using TLS
Authentication Problems
Ensure credentials are correctly encoded
Check for IP-based restrictions
Verify OAuth2 settings if applicable
Delivery Issues
Check SPF, DKIM, and DMARC records
Monitor email logs for bounce reasons
Verify recipient address format
Dependencies
MailKit: SMTP client library
<PackageReference Include="MailKit" Version="4.1.0" />
MimeKit: Email composition
<PackageReference Include="MimeKit" Version="4.1.0" />