Table of Contents

Class SendMailJob

Namespace
Quartz.Jobs
Assembly
Quartz.Jobs.dll

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

credentials ICredentialsByHost

What 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

string

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

string

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

string

PropertyMessage

The e-mail message body. REQUIRED.

public const string PropertyMessage = "message"

Field Value

string

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

string

PropertyRecipient

The e-mail address to send the mail to. REQUIRED.

public const string PropertyRecipient = "recipient"

Field Value

string

PropertyReplyTo

The e-mail address the message should say to reply to. Optional.

public const string PropertyReplyTo = "reply_to"

Field Value

string

PropertySender

The e-mail address to claim the mail is from. REQUIRED.

public const string PropertySender = "sender"

Field Value

string

PropertySmtpHost

The host name of the smtp server. REQUIRED.

public const string PropertySmtpHost = "smtp_host"

Field Value

string

PropertySmtpPort

The port of the smtp server. Optional.

public const string PropertySmtpPort = "smtp_port"

Field Value

string

PropertySubject

The subject to place on the e-mail. REQUIRED.

public const string PropertySubject = "subject"

Field Value

string

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

string

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

options SendMailOptions

Returns

MailMessage

Execute(IJobExecutionContext, CancellationToken)

Executes the job.

public virtual ValueTask Execute(IJobExecutionContext context, CancellationToken cancellationToken = default)

Parameters

context IJobExecutionContext

The job execution context.

cancellationToken CancellationToken

The cancellation instruction.

Returns

ValueTask

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

mailInfo MailInfo
cancellationToken CancellationToken

Returns

ValueTask