-
Notifications
You must be signed in to change notification settings - Fork 0
Provides IEmailService that can be used to send email messages using various email providers.
- MailKit NuGet package. Email messages are composed using MailKit package.
- DevGuild.AspNetCore.Services.Mail
- DevGuild.AspNetCore.Services.Mail.AmazonSes
- DevGuild.AspNetCore.Services.Mail.Smtp
- SMTP provider (via DevGuild.AspNetCore.Services.Mail.Smtp package) that sends email messages using configured SMTP server.
- Amazon SES provider (via DevGuild.AspNetCore.Services.Mail.AmazonSes package) that sends email messages using Amazon Simple Email Service.
- None provider (built-in) that does not actually send email messages, but they are still processed and recorded.
Email messages are implemented by creating a class that implements IEmail interface:
public class ForgotPasswordEmail : IEmail
{
public String Recipient { get; set; }
public String Link { get; set; }
public MimeMessage CreateMessage()
{
var message = new MimeMessage();
message.To.Add(new MailboxAddress(this.Recipient));
message.Subject = "Reset Password";
var bodyBuilder = new BodyBuilder();
bodyBuilder.TextBody = $"Please reset your password by navigating this link: {this.Link}";
bodyBuilder.HtmlBody = $"Please reset your password by clicking here: <a href=\"{HtmlEncoder.Default.Encode(this.Link)}\">link</a>.";
message.Body = bodyBuilder.ToMessageBody();
return message;
}
}
public async Task Example1(IEmailService emailService, String recipient, String resetUrl)
{
var emailMessage = await emailService.SendAsync(new ForgotPasswordEmail
{
Recipient = recipient,
Link = resetUrl,
});
}
Configuration of email service is done in appsettings.json configuration file. You can define multiple configurations by adding multiple properties to Mail section:
"Mail": {
"Configuration1": {
},
"Configuration2": {
}
}
Each configuration should define following properties:
- Type - type of the email provider that should be used to send email messages.
- Sender - sender email address.
- BlindCopy - email address (or comma-separated list of email addresses) of recipients that should receive a blind copy of each email message sent.
- DebugMode - if true, sends all messages to BlindCopy instead of actual addresses (requires BlindCopy to be configured).
- Options - provider-related options.
Example configuration:
"Mail": {
"System": {
"Type": "Smtp",
"Sender": "[email protected]",
"BlindCopy": "[email protected]",
"DebugMode": false,
"Options": {
"Host": "smtp.example.com",
"Port": 465,
"UseSsl": true,
"UserName": "sender",
"Password": "P@ssw0rd"
}
}
}
To add email service, add following code to ConfigureServices method of Startup class:
services.AddMail(this.Configuration.GetSection("Mail")) // Adding email service with "Mail" as configuration root.
.AddSmtp() // Adding SMTP provider
.RegisterConfiguration("System"); // Registering "System" configuration
This will add SMTP email provider and register configuration "System" from "Mail" application configuration section.
In addition to adding and configuring services, EmailMessage class should be registered within the repository. If EntityFramework is used, you just need to add DbSet<EmailMessage> EmailMessages { get; set; } to the application DbContext.