Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

send xml file as attachment

Former Member
0 Kudos

Hi,

    I'm using ABAP to send e-mail and xml & txt file as attachment ,  when I open the file I recieved in the email inbox I found xml file content has been truncated or the content of txt file is space. Then I send some other files, it's OK. I've used value 'BIN'  and  ' '  as parameter of "i_attachment_type " ,neither of them worked fine.

  

     I'm a newer in ABAP, any help will be appreciate.

     FYI, my source code:

*&---------------------------------------------------------------------*

*& Report  ZSENDMAILTEST

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT  ZSENDMAILTEST.

* This example shows how to send

*   - a simple text provided in an internal table of text lines

*   - and an attached MS word document provided in internal table

*   - to some internet email address.

*

* All activities done via facade CL_BCS!

DATA: send_request       TYPE REF TO cl_bcs.

DATA: text               TYPE bcsy_text.

data: hex_content        type solix_tab.

*DATA: hex_content       TYPE soli_tab.

DATA: document           TYPE REF TO cl_document_bcs.

DATA: sender             TYPE REF TO cl_sapuser_bcs.

DATA: recipient          TYPE REF TO if_recipient_bcs.

DATA: bcs_exception      type ref to cx_bcs.

data: sent_to_all        type os_boolean.

START-OF-SELECTION.

   PERFORM main.

*---------------------------------------------------------------------*

*       FORM main                                                     *

*---------------------------------------------------------------------*

FORM main.

   try.

*     -------- create persistent send request ------------------------

       send_request = cl_bcs=>create_persistent( ).

*     -------- create and set document with attachment ---------------

*     create document from internal table with text

       APPEND 'Hello world!' TO text.

       document = cl_document_bcs=>create_document(

                       i_type    = 'RAW'

                       i_text    = text

                       i_length  = '12'

                       i_subject = 'test created by BCS_EXAMPLE_2' ).

       DATA: file TYPE string.

       CALL FUNCTION 'FILE_GET_NAME'

        EXPORTING

          LOGICAL_FILENAME              = 'TEST-3'

          PARAMETER_1                    = '.txt'             "     change to '.xml' when sending xml file

        IMPORTING

          FILE_NAME                     = file

        EXCEPTIONS

          FILE_NOT_FOUND                = 1

          OTHERS                        = 2 .

       IF SY-SUBRC <> 0.

           write / 'cannot find phisical file name'.

       ENDIF.

      DATA : wa_bin type SOLIX.

*      DATA : wa_bin type SOLI.

       FIELD-SYMBOLS <hex_container> TYPE x.

*      FIELD-SYMBOLS <hex_container> TYPE c.

*      OPEN DATASET file FOR INPUT IN TEXT MODE ENCODING UTF-8.

       OPEN DATASET file FOR INPUT IN BINARY MODE.

       DO.

         ASSIGN wa_bin TO <hex_container> CASTING.

         READ DATASET file INTO <hex_container>.

         IF sy-subrc = 0.

           append wa_bin to hex_content.

         ELSE.

           EXIT.

         ENDIF.

       ENDDO.

*     write: / wa_bin.

       CLOSE DATASET file.

*     add attachment to document

*     BCS expects document content here e.g. from document upload

*     binary_content = .

       CALL METHOD document->add_attachment

         EXPORTING

           i_attachment_type    = ''

           i_attachment_subject = 'test.txt'      "when sending xml file change to test.xml

           i_att_content_hex    = hex_content.

*     add document to send request

       CALL METHOD send_request->set_document( document ).

*     --------- set sender -------------------------------------------

*     note: this is necessary only if you want to set the sender

*           different from actual user (SY-UNAME). Otherwise sender is

*           set automatically with actual user.

       sender = cl_sapuser_bcs=>create( sy-uname ).

       CALL METHOD send_request->set_sender

         EXPORTING

           i_sender = sender.

*     --------- add recipient (e-mail address) -----------------------

*     create recipient - please replace e-mail address !!!

       recipient = cl_cam_address_bcs=>create_internet_address(

                                         'testrecipient@bwtest.com' ).

*     add recipient with its respective attributes to send request

       CALL METHOD send_request->add_recipient

         EXPORTING

           i_recipient = recipient

           i_express   = 'X'.

*     ---------- send document ---------------------------------------

       CALL METHOD send_request->send(

         EXPORTING

           i_with_error_screen = 'X'

         RECEIVING

           result              = sent_to_all ).

       if sent_to_all = 'X'.

         write text-003.

       endif.

       COMMIT WORK.

* -----------------------------------------------------------

* *                     exception handling

* -----------------------------------------------------------

* * replace this very rudimentary exception handling

* * with your own one !!!

* -----------------------------------------------------------

     catch cx_bcs into bcs_exception.

       write: 'Fehler aufgetreten.'(001).

       write: 'Fehlertyp:'(002), bcs_exception->error_type.

       exit.

   endtry.

ENDFORM.

--------------------------------------------------------------------------------------------------


content of test.xml


<?xml version="1.0"?>

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"

xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">

<Worksheet ss:Name="Tabelle1">

<Table>

<Row>

<Cell><Data ss:Type="String">CPDEMO</Data></Cell>

<Cell><Data ss:Type="String">20110711</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">CTVBC02</Data></Cell>

<Cell><Data ss:Type="String">20110602</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">CTVBC01</Data></Cell>

<Cell><Data ss:Type="String">20110628</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">CTVPUR01</Data></Cell>

<Cell><Data ss:Type="String">20110520</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">CTVCE01</Data></Cell>

<Cell><Data ss:Type="String">20110520</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">BWIT01</Data></Cell>

<Cell><Data ss:Type="String">20110603</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">ALEREMOTE</Data></Cell>

<Cell><Data ss:Type="String">20131213</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">BWIT02</Data></Cell>

<Cell><Data ss:Type="String">20110711</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">BEXATBOE</Data></Cell>

<Cell><Data ss:Type="String">20130701</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">DBA01</Data></Cell>

<Cell><Data ss:Type="String">20110621</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">DDIC</Data></Cell>

<Cell><Data ss:Type="String">20140108</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">SAP*</Data></Cell>

<Cell><Data ss:Type="String">20110419</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">SAPCPIC</Data></Cell>

<Cell><Data ss:Type="String">00000000</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">TMSADM</Data></Cell>

<Cell><Data ss:Type="String">20110401</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">CPADMIN</Data></Cell>

<Cell><Data ss:Type="String">20110616</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">SNDADMIN</Data></Cell>

<Cell><Data ss:Type="String">20130114</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">BWREMOTE</Data></Cell>

<Cell><Data ss:Type="String">00000000</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">17124</Data></Cell>

<Cell><Data ss:Type="String">20131216</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">F3228773</Data></Cell>

<Cell><Data ss:Type="String">20140108</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">SNDBWIT</Data></Cell>

<Cell><Data ss:Type="String">20140104</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">SAPBIMETA</Data></Cell>

<Cell><Data ss:Type="String">20111020</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">9041</Data></Cell>

<Cell><Data ss:Type="String">20130807</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">FSK001</Data></Cell>

<Cell><Data ss:Type="String">20130815</Data></Cell>

</Row>

<Row>

<Cell><Data ss:Type="String">F6969539</Data></Cell>

<Cell><Data ss:Type="String">20110801</Data></Cell>

</Row>

</Table>

</Worksheet>

</Workbook>

11 REPLIES 11

deepika_chandrasekar
Active Contributor
0 Kudos

Hi,

Try to conver your XML to XSTRING using transformation and then convert it to solix using below code.

Txt file convert String to Xstring using FM SCMS_STRING_TO_XSTRING then convert it to solix.

lv_type = ''RAW'.

lt_content = cl_document_bcs=>xstring_to_solix(

                      ip_xstring = lv_xstr_content ).

               TRY.
                  document->add_attachment(
                  EXPORTING
                  i_attachment_type = 'XML'
                  i_attachment_subject = lv_atta_sub
                  i_att_content_hex = lt_content ).
                CATCH cx_document_bcs INTO document_bcs.
              ENDTRY.

     send_request->set_document( document ).

Regards,

Deepika.

0 Kudos

Hi Deepika,

     I really appreciate for your reply.

     As I said, I'm a new abaper, I don't know how to use transformation to convert xml to xstring, I've suveyed something about the 'ST', but it not help, or maybe I've missed something. Anyway, I can't sovlve this problem right now, would you give me sample code or some resources to sovle the problem.

Best regards,

JiaMing

0 Kudos

Hi,

From where you are getting XML file? You are creating a XML file or getting it from local system?

Regards,

Deepika.

0 Kudos

Hi,

     The xml file which I'm using is strored in application server, as well as the txt file . I need to send the file as attachment in an email.

Best Regards,

JiaMing

0 Kudos

Can you give code how you are reading xml file content from server.

Regards,

Deepika.

0 Kudos

Deepika,

     My source code and xml file content were attached after the question in the thread . Can u see?

    

     Thx!

Best Regards,

JiaMing

0 Kudos

Hi,

Did you debug and see whether hex_conten is filled or not?

if it is filled values should come in attachment

Regards,

Deepika.

0 Kudos

Deepika,

        In my source code, I've used field symbol <hex_container> which wa_bin assign to it, if the <hex_container> changed(the red part of the code below), the content of wa_bin also changed.

Regards,

JiaMing

   

      DATA : wa_bin type SOLIX.

*      DATA : wa_bin type SOLI.

       FIELD-SYMBOLS <hex_container> TYPE x.

*      FIELD-SYMBOLS <hex_container> TYPE c.

*      OPEN DATASET file FOR INPUT IN TEXT MODE ENCODING UTF-8.

       OPEN DATASET file FOR INPUT IN BINARY MODE.

       DO.

         ASSIGN wa_bin TO <hex_container> CASTING.

         READ DATASET file INTO <hex_container>.

         IF sy-subrc = 0.

           append wa_bin to hex_content.

         ELSE.

           EXIT.

         ENDIF.

       ENDDO.

*     write: / wa_bin.

       CLOSE DATASET file.

0 Kudos

Hi,

Yeah did you debug and checked hex_content is filled?

Regards,

Deepika.


0 Kudos

Hi

Use SCMS_BINARY_TO_XSTRING to change binary to Xstring for xml and then change into solib using class method( see below code) and then attach.

lt_content = cl_document_bcs=>xstring_to_solix(

                      ip_xstring = lv_xstr_content ).

TRY.

                  document->add_attachment(

                  EXPORTING

                  i_attachment_type = 'XML'

                  i_attachment_subject = lv_atta_sub

                  i_att_content_hex = lt_content ).

                CATCH cx_document_bcs INTO document_bcs.

              ENDTRY. 

     send_request->set_document( document ).

Regards,

Deepika.

0 Kudos

Deepika,

     I have solved my issue, cause when read the DATASET to <hex_container>, I used 'sy-subrc' to depend whether the DATASET be read or not, if the left content of the file less than 255 bytes, it will be readed, otherwise, it will be aborted.

     So 1)I use FM 'WS_QUERY' to get the size of the file, the length will depend how many times should the program read the file content to <hex_container>;

           2) cause the size of the content be read at the last is less equal than 255 bytes, but the size of <hex_content> is 255 bytes, so the left part of <hex_content> will be filled with '0' s;

           3) the '0's should not append to the file, so when add the attachment, we should use the real size of the file as value of parameter I_attachment_size, it will truncate the '0's;

Thanks again for you reply,

JiaMing

          FYI, below is my code:

        DATA: file(100) TYPE c.

      CALL FUNCTION 'FILE_GET_NAME'
        EXPORTING
          LOGICAL_FILENAME = 'TEST-3'
          PARAMETER_1      = '.xml'
        IMPORTING
          FILE_NAME        = file
        EXCEPTIONS
          FILE_NOT_FOUND   = 1
          OTHERS           = 2.
      IF SY-SUBRC <> 0.
      ENDIF.

      DATA : wa_bin type SOLIX.
      DATA: file_len type i.
      DATA: tim type i.

      CALL FUNCTION 'WS_QUERY'
        EXPORTING
          FILENAME = file
          QUERY    = 'FL'
        IMPORTING
          RETURN   = file_len.
      IF SY-SUBRC <> 0.
* Implement suitable error handling here
*        write:/ 'file cannot find'.
      ENDIF.

      tim = file_len DIV 255 .

      FIELD-SYMBOLS: <hex_container> type x.

      OPEN DATASET file FOR INPUT IN BINARY MODE.

** plus 1 to append all content into hex_content,
** but the length of the hex_content will longer
** than the real length of the file with '0's
      DO tim + 1 times.

        ASSIGN wa_bin TO <hex_container> CASTING.
        READ DATASET file INTO <hex_container>.

        append wa_bin to hex_content.

      ENDDO.

      CLOSE DATASET file.

      data: file_len_ch like SOOD-OBJLEN.

*     the real length of the file will be the parameter
*     of I_ATTACHMENT_SIZE
      file_len_ch = file_len.

      document->add_attachment(
      EXPORTING
      i_attachment_type = ''
      i_attachment_subject = 'test1.doc'
      I_ATTACHMENT_SIZE = file_len_ch
      i_att_content_hex = hex_content ).