# Schedule work and submit mail reliably

Separate cron calculation, durable jobs, templates and SMTP acceptance.

Foundation supplies calculation, rendering and transport adapters. Products own
durable execution, retry policy and business recurrence.

## Calculate an occurrence

CronScheduleCalculator.Next accepts a five-field cron expression, explicit system
timezone and UTC reference instant. It returns the next UTC occurrence exclusively
after that instant. Define handling for DST, timezone-data updates and month ends
in the product's scheduling contract.

Alternate weeks/months and invoice-draft lead time are business rules. A cron timer
does not by itself preserve an invoice recurrence or prevent duplicate drafts.

## Persist before delivery

Write the domain change and an outbox record in one database transaction. A durable
worker leases the occurrence, rechecks current app/license/workload rights, renders
content and submits mail. Persist an outcome and retry transient failures under
an explicit policy. Never hold the business database transaction across SMTP.

## Render reviewed templates

ITemplateRenderer accepts reviewed Scriban text and JSON data. Unknown variables
fail, HTML-mode strings are escaped and output/execution bounds apply. It does not
expose arbitrary CLR services or filesystem includes. Reviewed HTML remains trusted
content; this adapter is not a hostile-template sandbox.

## Understand SMTP success

IMailTransport.SubmitAsync reports SMTP acceptance, not inbox delivery. Required
STARTTLS is the default; implicit TLS is explicit. Cleartext to a named trusted relay and relaxed certificate validation require
separate explicit per-connection policies; neither is an automatic fallback.
Keep strict validation for normal provider connections. See
[SMTP trust and certificate policy](/foundation/0.19.0/guides/connected-mail/#smtp-certificate-policy)
and [runtime configuration](/foundation/0.19.0/guides/secret-protection/).

Use a stable MessageId for traceability, but do not promise exactly-once delivery
from MessageId alone. A disconnect after server acceptance creates an uncertain
outcome; the worker must preserve that uncertainty.

For an end-to-end setup path, follow [SMTP or Graph mail](/foundation/0.19.0/guides/mail-setup/).

Next: [Cronos API](/foundation/0.19.0/reference/fabricators-scheduling-cronos/),
[MailKit API](/foundation/0.19.0/reference/fabricators-mailing-mailkit/) and
[template API](/foundation/0.19.0/reference/fabricators-templating-scriban/).
