Skip to Content

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.

 

image

 

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.

 

image

 

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.

 

image

To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

    1. Lisa Mulchinock Post author
      Hi Manish,

      As you can see in our example we first add an attachment using the Attachment2 object – in this case we add an attchment called Log.txt.

      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 it is 2. You can find this value by querying the table ATC1.

      Cheers,
      Lisa

      (0) 
  1. Mathias Andreasson
    Hi Lisa,

    Good to see that you are still doing the work for us:)

    Is there a way to attach a filestream to the message service? Or any document without saving it to B1 DB first?

    The thing is, i have an DI-addon using the B1 message service as e-mailer and I want to attach a document to the e-mail, but I don’t want to save it in the DB, we are sending additional information in PDF:s as attachments for every order in B1. Saving them to the DB will be a bit to heavy.

    Thanx in advance.

    Mathias

    (0) 
    1. Mathias Andreasson
      Hello Again,

      I was a bit quick around the corners as usual. I can see that we are not saving to the DB, only copying the file to the SAP directory. This gives me another issue.

      If I want to send a file with the same name, I can’t save it as an attachment, since it is already a file with that name in the SAP attachment folder. So, I thought I could remove the file after sending it, but how do I remove an attachment file via the DI? Is it possible or do I have to walk around it?

      Kind Regards
      Mathias

      (0) 
      1. Lisa Mulchinock Post author
        Hey Mathias,

        Good to hear from you 🙂
        With the the Attachments2 object there is no delete method so maybe a workaround is to delete the file via the System.IO Namespace?

        Obviously you have to be careful with this so you don’t delete files you actually need! Also bear in mind you won’t be able to restore it from the recycle bin so extra danger!

        Here’s the code you need to do this:

                Dim FileToDelete As String
                FileToDelete = “C:\test.txt”

                If System.IO.File.Exists(FileToDelete) = True Then
                    System.IO.File.Delete(FileToDelete)
                End If

        Another option is to use the Move instead of Delete so you have a backup! Again its using the System.IO and the syntax is System.IO.File.Move( sourceFileName As String,destFileName As String)

        Hope that helps.
        Take care,
        Lisa

        (0) 
  2. Alvaro Gonzalez
    Hi, I have two questions….

    1.-I have to configure some SMTP server? I tried to copy the code and I just sent a message but the mail never comes

    2.-If I want to send a messege to business partners, can I use this code?

    Thanks

    (0) 

Leave a Reply