Sending mails in the Cloud

Today on Twitter there was a question on how to send mails in the online version of Business Central.

In the below two options I would like to present to you how to send emails with a nice HTML body that is stored in the Word layout report. Also, I will try to show you how to easily add the attachment to the email (at least in one option).

Before showing you the examples remember to set the SMTP Mail Setup because both of them are using it.

Mail Body

As I wrote above I will use the Word Report for it. Such a report can be modified by customers or consultants later without problems. For that, I created a simple report which you can see below.

Option 1: Email Item Table

Using Email Item Table is quite easy for sending mails. It has a very easy structure and nice error handling. However, has also some limitations. The biggest one is not possible to add an attachment when developing for the cloud, at least I was not able to do so. This is because in the table there is no function to add attachments other than specifying attachment path – and that is not possible for the cloud.

To create an mail you can add a variable for the table – use it as a temporary. Then you need to fill the fields such as Send to, Send CC, Send BCC, Subject, and Body.

The Body field is type Blob. Which allows, without any additional work, to add the body from the report. Simple save report to HTML using method SaveAs. The OutStream will contain an email body. In the end, you just need to run function Send which is in the table.

If the mail, from some reason, would not be sent then the user will be notified and will be able to save the mail body.

Option 2: SMTP Mail Codeunit

This option, in my opinion, is a little more complicated but in some cases, it will be better. First of all, you need to have a list of recipients. This is not a simple text but a list.

Then to create a body from the Word report you will need to use Temp Blob Codeunit and create OutStream. And then convert InStream to text since the body needs to be pass to the CreateMessage function as text.

The next step is to use function CreateMessage. But at this moment you need to check the parameters of the function directly in the object. Why? There are two functions with the same name in the Codeunit and one is marked as obsolete. Unfortunately, the wrong one is shown when you try to use it in the Visual Studio Code.

And below you can find correct parameters for the function.

To use the function you need to specify From Name and From Address (this could be omitted when using Email Item Table). Also, you should specify a list of recipients, subject and body – in text.

If you need to add the attachment you can use function AddAttachmentStream which in parameter takes the InStream with the attachment. And because it is not the file it is possible to use it in the cloud.

In the end, you just need to run function Send which is in the codeunit.

In this case, if there will be some issue with sending mail user will not be able to get the mail body as previously.

Result and summary

In both cases, the mail will have the same body. However, when using the SMTP Mail Codeunit to the mail there will be also attached file.

Which option is better? It depends on how complicated is your functionality. But for simple mails, you can use Mail Item Table. But if you need some more complicated functionality you should use SMTP Mail Codeunit.

You can find all examples (without errors and errors) on the GitHub:


Add a Comment

Your email address will not be published.