bcquality/community/knowledge/architecture/prefer-email-module-over-codeunit-mail-397.md
Michael Dieringer 17e8856b35 Add 4 community knowledge articles: posting-routine pattern, email module, Word report layout, defensive/offensive blast radius
Contributed from Curabis's own BCQuality custom layer, rewritten to the
community contribution contract (no fenced code in the .md, AL samples as
sibling .good.al/.bad.al files, under 100 lines per article).
2026-08-27 18:11:04 +02:00

1.8 KiB

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

New email-sending code should use the Email module, not Codeunit Mail (397)

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

Description

Business Central's current extensibility model for sending email is Codeunit Email, table Email Message, enum Email Scenario, and the Email Account/Email Connector interface — documented under "Extend Email Capabilities" and "Set up email." Older AL code, and some LLM training data, still reaches for the earlier Codeunit Mail (397) (CreateMessage/Send/GetErrorDesc), which is hard-coupled to one SMTP-style connector and leaves no record behind once a message is sent. New AL development that sends email should build on the Email module, not Codeunit Mail (397).

Best Practice

Create the message through Codeunit "Email Message" and send it through Codeunit Email, routed by an Email Scenario rather than a hard-coded account. This keeps the calling code independent of which connector (Microsoft 365, Current User, SMTP, or a partner connector) the customer has configured, gives every message a tracked Sent/Outbox/Draft status, and lets different document types route through different accounts without the caller needing to know which one.

Anti Pattern

Calling Codeunit Mail's CreateMessage/Send/GetErrorDesc in new code still compiles and runs, but it inherits SMTP-era assumptions, leaves no Sent/Outbox trail, and hard-codes the connector choice into the calling code. A reviewer can spot it by any new reference to Codeunit Mail (397) outside of already-existing legacy call sites.