Class SMTPMailService
SMTP implementation of IMailService using MailKit for sending email messages.
public class SMTPMailService : IMailService
- Inheritance
-
SMTPMailService
- Implements
- Inherited Members
Remarks
This service uses the MailKit library to send emails via SMTP protocol. It supports HTML and plain text body content, custom headers, multiple recipients, file attachments and inline images. The SMTP connection is configured using settings from SmtpMailConfigurationSection, and the server TLS certificate is validated unless AllowInvalidCertificates is explicitly enabled.
Constructors
SMTPMailService(IOptions<SmtpMailConfigurationSection>)
SMTP implementation of IMailService using MailKit for sending email messages.
public SMTPMailService(IOptions<SmtpMailConfigurationSection> appMailConfiguration)
Parameters
appMailConfigurationIOptions<SmtpMailConfigurationSection>The mail configuration options containing SMTP server settings.
Remarks
This service uses the MailKit library to send emails via SMTP protocol. It supports HTML and plain text body content, custom headers, multiple recipients, file attachments and inline images. The SMTP connection is configured using settings from SmtpMailConfigurationSection, and the server TLS certificate is validated unless AllowInvalidCertificates is explicitly enabled.
Methods
SendAsync(MailMessage, CancellationToken)
Sends an email message asynchronously using the configured SMTP server.
public Task<MailSendResult> SendAsync(MailMessage request, CancellationToken cancellationToken = default)
Parameters
requestMailMessageThe mail message to send, including sender, recipients, subject, body, and optional attachments.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<MailSendResult>
The Message-ID assigned to the delivered message.
Remarks
This method constructs a MIME message from the provided request, connects to the SMTP server,
authenticates using the configured credentials, sends the message, and disconnects.
If the message includes attachments via Parts, they are added as MIME attachments
named after FileName, or embedded inline when ContentId is informed.
The sender address and display name can be overridden per message or fall back to configuration defaults.
Exceptions
- NotSupportedException
Thrown when the message requires provider features SMTP has no equivalent for, namely ScheduledFor, Tags and Variables. Failing is preferred over delivering the message immediately, untagged or with unresolved placeholders.