Skip to main content

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.

warning

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​

OptionTypeDefaultDescription
HoststringnullSMTP server address.
Portint0SMTP server port.
SecureSocketModeSecureSocketOptionsAutoSSL/TLS mode used by MailKit.
EnableSSLboolInferred from SecureSocketModeConfiguration compatibility: false forces SecureSocketMode to None, true resets it to Auto if it was None.
UserNamestringnullIdentifier used to authenticate to the SMTP server.
PasswordstringnullSMTP password.
SenderstringnullDefault sender address.
CheckCertificateRevocationbooltrueIndicates whether the client verifies certificate revocation.
IgnoreCertificateRevocationboolfalseDisables certificate revocation verification.
ContentTransfertEncodingContentEncodingBase64MIME content encoding.
LogFilestringnullPath to the SMTP protocol log file, or Console.
LogToConsoleboolfalseWrites the SMTP protocol log to the console by setting LogFile to Console.
MailModestringSMTPSMTP for direct sending, Daemon for writing mail files to a folder.
MailDaemonFolderstringnullFolder used in Daemon mode.
MailDaemonEncodingstringiso-8859-1 if absentEncoding used to save Daemon mode files.
UseDaemonFolderboolfalseEnables 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().

Server/Services/Registry.cs
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​

TasksController.cs
[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 cc recipients;
  • attachments as file paths, byte[], or Stream;
  • an explicit sender or the default Sender.

To send to multiple recipients in a single string, separate addresses with commas.