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
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
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
[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.
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.
Thanks for sharing @Duilio