Overview
AndanDo uses SMTP to send transactional emails including password resets, booking confirmations, and notifications. The email service is built on MailKit and configured through theSmtp section in appsettings.json.
The email service supports all major SMTP providers including Gmail, SendGrid, AWS SES, and custom SMTP servers.
Configuration
Email settings are configured in theSmtp section of appsettings.json:
appsettings.json
Configuration Properties
Host
The SMTP server hostname.- Type:
string - Required: Yes
- Examples:
smtp.gmail.com,smtp.sendgrid.net,email-smtp.us-east-1.amazonaws.com
Port
The SMTP server port number.- Type:
int - Required: Yes
- Common Ports:
587- Submission port with STARTTLS (recommended)465- SMTP over SSL (legacy)25- Standard SMTP (usually blocked by ISPs)2525- Alternative submission port
UseSsl
Enable SSL/TLS from the start of the connection (port 465).- Type:
bool - Required: No
- Default:
false - Use Case: Set to
trueonly for port 465 connections
UseStartTls
Upgrade to TLS after initial plain text connection (port 587).- Type:
bool - Required: No
- Default:
false - Use Case: Set to
truefor port 587 (most common)
SSL vs STARTTLS:
- Use
UseSsl: truefor port 465 (SSL from the start) - Use
UseStartTls: truefor port 587 (upgrade to TLS) - Never use both simultaneously
User
The SMTP username for authentication.- Type:
string - Required: Yes (for most providers)
- Format: Usually an email address or API key
Password
The SMTP password or API token.- Type:
string - Required: Yes (for most providers)
- Format: App-specific password or API key
FromEmail
The sender email address that appears in the “From” field.- Type:
string - Required: Yes
- Format: Valid email address
- Best Practice: Use a dedicated email like
noreply@,notifications@, orsupport@
FromName
The sender name that appears in the “From” field.- Type:
string - Required: No
- Default:
"AndanDo"(defined inSmtpOptions.cs:12) - Example: Users see “AndanDO <[email protected]>“
SmtpOptions Class
The configuration is bound to theSmtpOptions class:
Services/Email/SmtpOptions.cs
EmailService Implementation
TheEmailService uses MailKit to send emails:
Services/Email/EmailService.cs
Service Registration
The email service is registered inProgram.cs:
Program.cs
Provider-Specific Configuration
Gmail Setup Guide
Using Gmail with App Passwords
Using Gmail with App Passwords
- Enable 2-Factor Authentication on your Google account
- Go to Google Account Security
- Under “Signing in to Google”, select App passwords
- Generate a new app password for “Mail”
- Use the generated 16-character password in your configuration
Usage Example
Sending an email from your code:Environment-Specific Configuration
Development (User Secrets)
Development (User Secrets)
Store SMTP credentials in User Secrets:Verify:
Production (Environment Variables)
Production (Environment Variables)
Use environment variables:
Testing (Disable or Use Mailhog)
Testing (Disable or Use Mailhog)
For testing without sending real emails, use Mailhog:Configure for Mailhog:View emails at: http://localhost:8025
Troubleshooting
Invalid SMTP configuration
Invalid SMTP configuration
Error:
InvalidOperationException: La configuración SMTP no es válida.Cause: Missing or invalid Host or Port configuration.Solutions:- Verify
Hostis not empty - Ensure
Portis greater than 0 - Check for typos in configuration keys
Authentication failed
Authentication failed
Error:
AuthenticationException: 535 Authentication failedSolutions:- Verify username and password are correct
- For Gmail, use App Passwords, not your account password
- Check if 2FA is required for your email provider
- Ensure the account isn’t locked or suspended
Connection refused
Connection refused
Error:
SocketException: Connection refusedSolutions:- Verify the
HostandPortare correct - Check firewall settings allow outbound SMTP connections
- Ensure the SMTP server is accessible from your network
- Try using telnet to test connectivity:
telnet smtp.gmail.com 587
SSL/TLS negotiation failed
SSL/TLS negotiation failed
Error:
SslHandshakeException: An error occurred while attempting to establish an SSL or TLS connectionSolutions:- Verify
UseSslandUseStartTlssettings match your port - For port 587, use
UseStartTls: true, UseSsl: false - For port 465, use
UseSsl: true, UseStartTls: false - Update MailKit NuGet package to the latest version
Sender address rejected
Sender address rejected
Error:
SmtpCommandException: 550 Sender address rejectedSolutions:- Verify the
FromEmailis valid and matches your authenticated account - For Gmail,
FromEmailmust match the authenticated Gmail account - For SendGrid/SES, verify the sender email is verified in the provider
- Check SPF/DKIM records are configured for your domain