Mail Sending
Mail sending is performed from the Server project, for example from a controller or server service. It is not possible to send an email directly from the Client project.
Configure an SMTP server
The Lagoon:Smtp section in the appsettings.json file configures the ISmtp service.
The Sender key defines the default sender and must be set if application calls do not explicitly provide a sender.
{
"Lagoon": {
"Smtp": {
"Host": "smtp.provider.com",
"Port": 587,
"EnableSSL": true,
"UserName": "username",
"Password": "smtpPassword",
"Sender": "app@desirade.fr",
"CheckCertificateRevocation": true
}
}
}
CheckCertificateRevocation is optional. It can be set to false if a certificate revocation verification error occurs. The actual reverse option is IgnoreCertificateRevocation.
Lagoon:Smtp Options
| Option | Type | Default | Description |
|---|---|---|---|
Host | string | null | SMTP server address. |
Port | int | 0 | SMTP server port. |
SecureSocketMode | SecureSocketOptions | Auto | SSL/TLS mode used by MailKit. |
EnableSSL | bool | Inferred from SecureSocketMode | Configuration compatibility: false forces SecureSocketMode to None, true resets it to Auto if it was None. |
UserName | string | null | Identifier used to authenticate to the SMTP server. |
Password | string | null | SMTP password. |
Sender | string | null | Default sender address. |
CheckCertificateRevocation | bool | true | Indicates whether the client verifies certificate revocation. |
IgnoreCertificateRevocation | bool | false | Disables certificate revocation verification. |
ContentTransfertEncoding | ContentEncoding | Base64 | MIME content encoding. |
LogFile | string | null | Path to the SMTP protocol log file, or Console. |
LogToConsole | bool | false | Writes the SMTP protocol log to the console by setting LogFile to Console. |
MailMode | string | SMTP | SMTP for direct sending, Daemon for writing mail files to a folder. |
MailDaemonFolder | string | null | Folder used in Daemon mode. |
MailDaemonEncoding | string | iso-8859-1 if absent | Encoding used to save Daemon mode files. |
UseDaemonFolder | bool | false | Enables the use of daemon folder. |
Daemon folder mode
Daemon mode saves emails to a folder instead of sending them directly via SMTP.
{
"Lagoon": {
"Smtp": {
"MailMode": "Daemon",
"MailDaemonFolder": "C:\\Temp\\PendingMails",
"Sender": "app@desirade.fr"
}
}
}
Register the service
In the Server project, register the service via AddSmtp().
public static class Registry
{
public static void RegisterServices(IServiceCollection services)
{
// Activates the server-side mail sending service.
services.AddSmtp();
}
}
Send an email from a controller
[ApiController]
[Route("api/[controller]")]
public class TasksController : LgControllerBase
{
private readonly ISmtp _smtp;
public TasksController(ISmtp smtp)
{
_smtp = smtp;
}
[Authorize(Roles = AppRoles.Admin)]
[HttpPost("CreateTask")]
public async Task Create(UserTask task)
{
// Creates the user task.
// Sends an email after task creation.
await _smtp.SendEmailAsync(
"admin@provider.com",
"New task created",
"Hello,<br />A new task has been created.");
}
}
Recipients and attachments
ISmtp exposes overloads for:
- a single recipient or multiple recipients
IEnumerable<string>; - a list of carbon copy
ccrecipients; - attachments as file paths,
byte[], orStream; - an explicit sender or the default
Sender.
To send to multiple recipients in a single string, separate addresses with commas.