// Configure email address.services.AddEmail("from@example.com")/* Setup one of email clients here. */;// Configure email address of a sender with name and email templates root directory path.services.AddEmail(newEmailAddress("from@example.com","Example"), options => {options.TemplateRootPath="C:\\Templates"; })/* Setup one of email clients here. */;
AddEmail() extension method performs registration email service IEmailSender. Additionally configure IEmailSender service used EmailOptions class. EmailOptions properties:
Property name
Property type
Description
From
EmailAddress
Email address of a sender. Required option, which configuring with AddEmail() method.
TemplateRootPath
string?
Root directory path with email templates (needed if using email templates), default value is null.
Email service uses different email clients. For example: File-client, SMTP-client, clients for cloud-based email delivery platform (like SendGrid, MailChimp etc.) or something else. You can use one of default implemented email client from list:
Additionally configure SMTP-client used SmtpOptions class. SmtpOptions properties:
Property name
Property type
Description
Host
string
Domain name or IP address (IPv4) of the host used for SMTP transactions. Required option, which configuring with AddSmtpClient() method.
Port
int
Port used for SMTP transactions. Required option, which configuring with AddSmtpClient() method, default value is 25.
User
string?
User name used to authenticate a sender, default value is null.
Password
string?
User password used to authenticate a sender, default value is null.
IsSslEnabled
bool
Specify whether the SMTP-client uses SSL to encrypt a connection, default value is false.
File-client
An File-client allows preserving of email notifications to file system. This behavior can be useful in development environment for example. Setup email service with File-client:
services.AddEmail("from@example.com") .AddFileClient("C:\\Emails"); // All sending emails preserves in "C:\\Emails" directory.
You also can use clients depending on the environment. For example you can use File-client for development environment and SMTP-client for production environment.
You can use EmailTemplateMessage, EmailTemplateFileMessage classes to sending email notifications generating via templates. By default templates constructing via "{{" and "}}", for example "{{UserName}}".
Example of using dynamic template:
// Define Model class for template.classRecipient{publicstring Name { get; set; }}// Create instance of Model class.var model =newRecipient { Name ="Peter Parker" };// Create messagevar message =newEmailTemplateMessage("to@example.com","Hello, {{Name}}!", model);// Send email notification with text "Hello, Peter Parker!".await_emailSender.SendEmailAsync(message);
Example of using static template:
// Define Model class for template.classRecipient{publicstring Name { get; set; }}// Define static templateclassRecipientEmailTemplateMessage:EmailTemplateMessage<Recipient>{publicoverridestring Template =>"Hello, {{Name}}!";publicRecipientEmailTemplateMessage(string to,Recipient model) : base(newEmailAddress(to), model) {}}// Create instance of Model class.var model =newRecipient { Name ="Peter Parker" };// Create messagevar message =newRecipientEmailTemplateMessage("to@example.com", model);// Send email notification with text "Hello, Peter Parker!".await_emailSender.SendEmailAsync(message);
See example of using template file in Razor section.