In this recipe, we will see how we can add attachments to our e-mail message (we will use the program created in the previous recipe). We will also use the code fragment from the XML String (for Excel data) generated in the Downloading into Excel made easy using simple transformations recipe of Chapter 10, Simple Transformations chapter.
We will copy the program of the last recipe and add the transformation code for the conversion into binary file of the XML string. We will then write the code for attaching the XML file to the e-mail message. The recipe shows the additional code required to attach a file to the e-mail.
The necessary steps for defining the send request and the subsequent steps are the same. This recipe code will be inserted after the create_document
method call of the cl_document_bcs
class and before the set_document
method call of the cl_bcs
class.
We will now perform the steps shown as follows:
CALL TRANSFORMATION
statement, we will declare internal table attached_xml
based on the table type solix_tab
. We then call the static method string_to_solix
of the cl_bcs_convert
class for creating binary content out of the XML string. The solix_tab
type is an internal table in binary format. The code page for Unicode UTF-16LE
is passed 4103
. The result is then stored in converted form in the attached_xml
file.add_attachment
of the class cl_bcs_document
. For the parameter attachment type I_ATTACHMENT_TYPE
, we pass the value BIN
. For the name of the file (i_attachment_subject
), we specify the name XML Excel Data.XLS
. The parameter i_att_content_hex
is for passing the internal table containing the file to be attached.After the CALL TRANSFORMATION
statement, we have the file content in the form of a large XML string. This string needs to be converted to an Excel format. We convert this into a binary format (an internal table based on dictionary structure SOLIX) using the static method string_to_solix
of the class cl_bcs_convert
. The structure of SOLIX is as follows:
For converting the XML string into binary data, we use the code page 4103
. Excel files expect the data to be in the UTF-16 LE
format, that is, 4103
.
The code is then added to insert the attachment to the created document using the add_attachment
method of the cl_bcs_document
class.
The attachment subject is the name of the file (in our case XML Excel Data.XLS
) as it should appear in the receiver's inbox, the type is BIN
meaning binary. For the content of the file, the
i_att_content_hex
parameter is supplied with the converted Excel file data. The attachment appears as shown in the following screenshot:
The file when opened in Excel looks like following screenshot:
18.220.88.62