web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Active Discussion

How to preserve line breaks when sending directly Email confirmations via SMTP with Dynamics NAV and 365 Business Central Windows Client

(1) ShareShare
ReportReport
Posted on by Microsoft Employee

With Dynamics 365 Business Central 14.x, and earlier versions of its predecessor Dynamics NAV, it is possible to send email confirmations with attachments directly through SMTP.

It is possible, then, to write a custom form and send it directly without wasting some clicks to edit in Outlook and send it over. See below an example email

pastedimage1600858369452v1.png

When the email is sent through, the recipient might notice that the line breaks are not respected and everything is pack up in one single line as shown below

pastedimage1600858600289v2.png

This behavior is the result of a missing feature that has been implemented when editing the body in Outlook while it has not been implemented when sending email directly through SMTP. 

If you need to have the same behavior, it is fairly easy to have this implemented. How this could be achieved then?

SHORT ANSWER

Implement the following subscriber in a codeunit 

pastedimage1600858892258v3.png

[EventSubscriber] OnBeforeCreateMessage(VAR SenderName : Text;VAR SenderAddress : Text;VAR Recipients : Text;VAR Subject : Text;VAR Body : Text;HtmlFormatted : Boolean)

IF HtmlFormatted THEN

  Body := Mail.FormatTextForHtml(Body);

 

And the email sent will respect the lines as they are added in the HTML body.

pastedimage1600858944295v4.png

LONG ANSWER

When choosing not to open in Outlook but send the email message directly and with the attachment, the application calls Codeunit 400 SMTP Mail instead of Codeunit 397 Mail.

If you choose to open in Outlook, you will see that the line breaks are respected.

This happens because of the application calls Codeunit 397 Mail and use FormatTextForHtml function to replace CR LF characters with the equivalent html <BR /> as shown below

…

[External] FormatTextForHtml(Text : Text) : Text

IF Text <> '' THEN BEGIN

  Char13 := 13;

  Char10 := 10;

  String := Text;

  EXIT(String.Replace(FORMAT(Char13) + FORMAT(Char10),'<br />'));

END;

 

EXIT('');

…

 

Therefore, by simply subscribing the event OnBeforeCreateMessage exposed by Codeunit 400 SMTP Mail and using the same function contained in Codeunit 397 Mail, you will get the same result

 

Body := Mail.FormatTextForHtml(Body);

 

Please note that Body text has to be assigned after function execution because FormatTextForHtml is not set by reference within the function signature.

  • Bilal Haider Profile Picture
    487 on at

    Thanks for sharing @Duilio

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 569 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 375 Super User 2026 Season 2

#3
Grigorios Mavrogeorgis Profile Picture

Grigorios Mavrogeorgis 228 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans