Class SendMailJob
A Job which sends an e-mail with the configured content to the configured recipient.
public class SendMailJob : IJob
- Inheritance
-
SendMailJob
- Implements
- Inherited Members
Remarks
The message is configured through the job data keys below. SendMailOptions names them all, and UsingSendMailOptions<TConfigurator>(TConfigurator, SendMailOptions) writes them, so the settings can be given as a value rather than as string keys; the keys stay the persisted form either way.
The SMTP credential is the exception, and is taken from the container: register an ICredentialsByHost and the job authenticates with it. Job data is durable, cluster-replicated and visible in the dashboard, which is no place for a password. The PropertyUsername and PropertyPassword keys are still read when nothing is registered, so a job scheduled by an earlier version keeps sending — with a warning.
Constructors
SendMailJob(ICredentialsByHost?)
Initializes a new instance of the SendMailJob class.
public SendMailJob(ICredentialsByHost? credentials = null)
Parameters
credentialsICredentialsByHostWhat to authenticate to the SMTP server with, taken from the container. A NetworkCredential covers one account; a CredentialCache covers several servers. null falls back to the credential in job data, if there is one, and otherwise sends unauthenticated.
Fields
PropertyCcRecipient
The e-mail address to cc the mail to. Optional.
public const string PropertyCcRecipient = "cc_recipient"
Field Value
PropertyEnableSsl
Whether to negotiate TLS with the SMTP server (STARTTLS). Optional; defaults to
false, which is SmtpClient's own default.
public const string PropertyEnableSsl = "smtp_enable_ssl"
Field Value
Remarks
Off by default because turning it on fails outright against a server that does not offer TLS,
and a job that has been sending for years through a relay on the same host would stop. Turn it
on for anything that crosses a network you do not own, and for anything that authenticates:
SMTP AUTH LOGIN is base64, which is not encryption.
PropertyEncoding
The message subject and body content type. Optional.
public const string PropertyEncoding = "encoding"
Field Value
PropertyMessage
The e-mail message body. REQUIRED.
public const string PropertyMessage = "message"
Field Value
PropertyPassword
Password for authenticated session. Optional.
Legacy, and a credential written to QRTZ_JOB_DETAILS in whatever the configured
serializer emits. Register an ICredentialsByHost with the container instead.
public const string PropertyPassword = "smtp_password"
Field Value
PropertyRecipient
The e-mail address to send the mail to. REQUIRED.
public const string PropertyRecipient = "recipient"
Field Value
PropertyReplyTo
The e-mail address the message should say to reply to. Optional.
public const string PropertyReplyTo = "reply_to"
Field Value
PropertySender
The e-mail address to claim the mail is from. REQUIRED.
public const string PropertySender = "sender"
Field Value
PropertySmtpHost
The host name of the smtp server. REQUIRED.
public const string PropertySmtpHost = "smtp_host"
Field Value
PropertySmtpPort
The port of the smtp server. Optional.
public const string PropertySmtpPort = "smtp_port"
Field Value
PropertySubject
The subject to place on the e-mail. REQUIRED.
public const string PropertySubject = "subject"
Field Value
PropertyUsername
Username for authenticated session. Password must also be set if username is used. Optional.
Legacy. Register an ICredentialsByHost with the container instead — job data is persisted, replicated to every node and readable from the dashboard.
public const string PropertyUsername = "smtp_username"
Field Value
Methods
BuildMessage(SendMailOptions)
Builds the message to send. Override to add to it — an attachment, a header — or to build a different one entirely.
protected virtual MailMessage BuildMessage(SendMailOptions options)
Parameters
optionsSendMailOptions
Returns
Execute(IJobExecutionContext, CancellationToken)
Executes the job.
public virtual ValueTask Execute(IJobExecutionContext context, CancellationToken cancellationToken = default)
Parameters
contextIJobExecutionContextThe job execution context.
cancellationTokenCancellationTokenThe cancellation instruction.
Returns
Send(MailInfo, CancellationToken)
Sends the built message. Override to route mail through something other than SmtpClient.
protected virtual ValueTask Send(MailInfo mailInfo, CancellationToken cancellationToken = default)
Parameters
mailInfoMailInfocancellationTokenCancellationToken