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

Community site session details

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

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

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.

I have the same question (0)
  • Bilal Haider Profile Picture
    408 on at
    RE: How to preserve line breaks when sending directly Email confirmations via SMTP with Dynamics NAV and 365 Business Central Windows Client

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 4,220

#2
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 3,958

#3
Sumit Singh Profile Picture

Sumit Singh 2,961

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans