Skip to content
Arsenty Politov edited this page Mar 15, 2019 · 1 revision

Mail services

Provides IEmailService that can be used to send email messages using various email providers.

Dependencies

  • MailKit NuGet package. Email messages are composed using MailKit package.

Related packages

Supported providers

  • 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.

Developing email messages

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;
    }
}

Sending email messages

public async Task Example1(IEmailService emailService, String recipient, String resetUrl)
{
    var emailMessage = await emailService.SendAsync(new ForgotPasswordEmail
    {
        Recipient = recipient,
        Link = resetUrl,
    });
}

Configuration

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"
        }
    }
}

Adding services

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.

Clone this wiki locally