|
| 1 | +## Email Service Configuration |
| 2 | + |
| 3 | +The MailService provides email functionality using SMTP protocol. It supports: |
| 4 | +- Account activation emails |
| 5 | +- Password reset emails |
| 6 | +- Welcome emails |
| 7 | + |
| 8 | +### Configuration Options |
| 9 | + |
| 10 | +Configure email settings in `appsettings.json`: |
| 11 | + |
| 12 | +```json |
| 13 | +{ |
| 14 | + "Security": { |
| 15 | + "Email": { |
| 16 | + "From": "your-app@localhost", |
| 17 | + "BaseUrl": "http://127.0.0.1:8080", |
| 18 | + "Smtp": { |
| 19 | + "Host": "localhost", |
| 20 | + "Port": 25, |
| 21 | + "Username": "", |
| 22 | + "Password": "", |
| 23 | + "Protocol": "smtp", |
| 24 | + "UseSsl": true, |
| 25 | + "Timeout": 5000, |
| 26 | + "Debug": true |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +#### Development Configuration |
| 34 | +For local development, you can use: |
| 35 | +- [MailHog](https://github.com/mailhog/MailHog) - Recommended for Mac/Linux |
| 36 | +- [Papercut SMTP](https://github.com/ChangemakerStudios/Papercut-SMTP) - Windows alternative |
| 37 | + |
| 38 | +#### Production Configuration |
| 39 | +Common SMTP providers: |
| 40 | +- Gmail SMTP: |
| 41 | + ```json |
| 42 | + "Smtp": { |
| 43 | + "Host": "smtp.gmail.com", |
| 44 | + "Port": 587, |
| 45 | + "UseSsl": true |
| 46 | + } |
| 47 | + ``` |
| 48 | +- Amazon SES: |
| 49 | + ```json |
| 50 | + "Smtp": { |
| 51 | + "Host": "email-smtp.us-east-1.amazonaws.com", |
| 52 | + "Port": 587, |
| 53 | + "UseSsl": true |
| 54 | + } |
| 55 | + ``` |
| 56 | + |
| 57 | +### Usage Examples |
| 58 | + |
| 59 | +1. Activation Email: |
| 60 | +```csharp |
| 61 | +await _mailService.SendActivationEmail( |
| 62 | + |
| 63 | + name: "John Doe", |
| 64 | + activationKey: "activation-key-here" |
| 65 | +); |
| 66 | +``` |
| 67 | + |
| 68 | +2. Password Reset: |
| 69 | +```csharp |
| 70 | +await _mailService.SendPasswordResetMail( |
| 71 | + |
| 72 | + name: "John Doe", |
| 73 | + resetKey: "reset-key-here" |
| 74 | +); |
| 75 | +``` |
| 76 | + |
| 77 | +3. Welcome Email: |
| 78 | +```csharp |
| 79 | +await _mailService.SendCreationEmail( |
| 80 | + |
| 81 | + name: "John Doe" |
| 82 | +); |
| 83 | +``` |
| 84 | + |
| 85 | +### Security Best Practices |
| 86 | + |
| 87 | +1. **SMTP Security** |
| 88 | + - Enable TLS/SSL encryption (`UseSsl: true`) |
| 89 | + - Use environment variables for credentials: |
| 90 | + ```json |
| 91 | + "Username": "${SMTP_USER}", |
| 92 | + "Password": "${SMTP_PASSWORD}" |
| 93 | + ``` |
| 94 | + - Rotate SMTP credentials regularly |
| 95 | + - Use dedicated sending domains |
| 96 | + |
| 97 | +2. **Content Security** |
| 98 | + - Sanitize all user input in templates |
| 99 | + - Include SPF/DKIM records for your domain |
| 100 | + - Set up DMARC policy |
| 101 | + - Include unsubscribe links (GDPR compliance) |
| 102 | + |
| 103 | +3. **Error Handling** |
| 104 | + - Implement exponential backoff for retries |
| 105 | + - Log delivery failures with correlation IDs |
| 106 | + - Monitor bounce rates and spam reports |
| 107 | + |
| 108 | +### Testing |
| 109 | + |
| 110 | +1. Unit Testing: |
| 111 | +```csharp |
| 112 | +[Fact] |
| 113 | +public async Task Should_Send_Activation_Email() |
| 114 | +{ |
| 115 | + // Arrange |
| 116 | + var mockMailService = new Mock<IMailService>(); |
| 117 | + |
| 118 | + // Act |
| 119 | + await mailService.SendActivationEmail("[email protected]", "Test User", "key123"); |
| 120 | + |
| 121 | + // Assert |
| 122 | + mockMailService.Verify(x => x.SendActivationEmail( |
| 123 | + It.IsAny<string>(), |
| 124 | + It.IsAny<string>(), |
| 125 | + It.IsAny<string>() |
| 126 | + ), Times.Once); |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +2. Integration Testing: |
| 131 | +```csharp |
| 132 | +[Fact] |
| 133 | +public async Task Should_Connect_To_SMTP_Server() |
| 134 | +{ |
| 135 | + // Use MailHog for integration tests |
| 136 | + var mailConfig = new MailConfiguration |
| 137 | + { |
| 138 | + Host = "localhost", |
| 139 | + Port = 1025 |
| 140 | + }; |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +### Troubleshooting Guide |
| 145 | + |
| 146 | +1. **Connection Issues** |
| 147 | + - Verify network connectivity to SMTP server |
| 148 | + - Check firewall rules for SMTP ports (25, 465, 587) |
| 149 | + - Validate SSL certificate if using TLS |
| 150 | + |
| 151 | +2. **Authentication Problems** |
| 152 | + - Ensure credentials are correctly encoded |
| 153 | + - Check for IP-based restrictions |
| 154 | + - Verify OAuth2 settings if applicable |
| 155 | + |
| 156 | +3. **Delivery Issues** |
| 157 | + - Check SPF, DKIM, and DMARC records |
| 158 | + - Monitor email logs for bounce reasons |
| 159 | + - Verify recipient address format |
| 160 | + |
| 161 | +### Dependencies |
| 162 | + |
| 163 | +- **MailKit**: SMTP client library |
| 164 | + ```xml |
| 165 | + <PackageReference Include="MailKit" Version="4.1.0" /> |
| 166 | + ``` |
| 167 | +- **MimeKit**: Email composition |
| 168 | + ```xml |
| 169 | + <PackageReference Include="MimeKit" Version="4.1.0" /> |
| 170 | + ``` |
| 171 | + |
| 172 | +### Additional Resources |
| 173 | + |
| 174 | +- [JHipster Email Configuration Guide](https://www.jhipster.tech/tips/011_tip_configuring_email_in_jhipster.html) |
| 175 | +- [MailKit Documentation](https://github.com/jstedfast/MailKit#documentation) |
| 176 | +- [SMTP Security Best Practices](https://www.m3aawg.org/published-documents) |
0 commit comments