bcquality/microsoft/knowledge/breaking-changes/prefer-email-module.md
Michael Dieringer 057e17c202 Add 18 community AL/BC patterns across style, data-modeling, web-services, appsource, breaking-changes, performance, and testing
Contributed by CURABIS ApS, generalized from patterns observed across real AppSource/PTE development. Each article follows the knowledge file format (frontmatter, Description/Best Practice/Anti Pattern, sibling .good.al/.bad.al samples).
2026-09-21 22:24:07 +02:00

1.4 KiB

bc-version domain keywords technologies countries application-area
all
breaking-changes
email
codeunit-mail
email-message
email-scenario
email-account
smtp
sending-email
al
w1
all

Send email through the Email module, not Codeunit Mail (397)

Contributions welcome — open a PR to refine or extend this article.

Description

Older AL code sends email by calling Codeunit Mail (397). Business Central's current extensibility model is a different, richer object set — Codeunit Email, table Email Message, enum "Email Scenario", and the Email Account/Email Connector interface (Microsoft 365, Current User, SMTP, or a custom connector). New code built on Codeunit Mail inherits its SMTP-era, single-connector assumptions and leaves no Sent/Outbox trail behind.

Best Practice

Build on Codeunit Email and table Email Message. Route the message through an Email Scenario so different document types can use different accounts without the calling code needing to know which account that is, and get a tracked Sent/Outbox/Draft record for free.

See sample: prefer-email-module.good.al.

Anti Pattern

Calling Codeunit Mail's CreateMessage/Send/GetErrorDesc. It still runs, but it is hard-coupled to whatever SMTP setup exists, and leaves no queryable record of what was sent.

See sample: prefer-email-module.bad.al.