In this blog we will demonstrate how to send an email with an attachment using both the Message Object and Message Service. Attachments are represented in the SAP Business One DI API from version 2005 via the Attachments2 object. This enables the user to copy files from a source folder to the Attachments folder.
Ensure first that you define the attachments folder in Administration -> System Initialisation -> General Settings. If this is not done you will receive an error such as “Source Files does not exist”.
The Message Object is the ‘old’ way of utilising the functionality. To add the attachment to the email we call the Attachments subobject of the Messages object and provide the location of the file name.
As you know SAP recommends that partners use Services in add-ons. All new functionality will in future be released as a service and not an object. To send an email using Message Services we first need to add the attachment.
In the path you defined in Administration->System Initialization->General
Setting->Path, every attachment will first be copied to this path. If there is already a file with the same name of the one you are attempting to attach the SAP Business One DI API will try to copy it first to the default path and will then return an error message that the file name exists.
Once the attachment is added we need to link the attachment/document to the email by setting the AttachmentEntry value. This corresponds to the AbsoluteEntry of the newly added attachment and in our example this is 2. This is stored in the the table ATC1.