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: 

Attachment in mail not opening

Former Member
0 Kudos

Dear Guys,

We need to send a mail to external mail ID with word doc as attachment. so we are using the function module SO_NEW_DOCUMENT_ATT_SEND_API1 .

I am getting the mail with attachment. But when i click on the attachment an error pops up telling the file content is corrupted.The following is the code used

DATA : it_packing_list TYPE TABLE OF sopcklsti1,
         gd_doc_data TYPE sodocchgi1,
         wa_packing_list LIKE LINE OF  it_packing_list,
         psubject(90) TYPE c,
         it_message TYPE STANDARD TABLE OF solisti1,
         wa_it_message LIKE LINE OF it_message,
         c1(99) TYPE c,
         c2(15) TYPE c,
         lv_studentmailid type PA0105-USRID_LONG,
         num_lines TYPE i,
         it_receivers TYPE TABLE OF somlreci1,
         wa_it_receivers LIKE LINE OF  it_receivers,
        it_contents_bin type table of SOLISTI1,"SOLIX,
        wa_contents_bin  like line of it_contents_bin ,
          it_BINARY_TAB    type table of sdokcntbin,
         wa_BINARY_TAB like line of it_BINARY_TAB.

*Set receiver's address
  FREE wa_it_receivers.
  wa_it_receivers-receiver = lv_studentmailid.
  wa_it_receivers-rec_type = 'U'.
  APPEND wa_it_receivers TO it_receivers.


  DESCRIBE TABLE it_receivers LINES num_lines.
  IF  num_lines IS NOT INITIAL.

*Set Subject of Mail
    psubject ='Booked'.

*Set Content of mail
    CLEAR wa_it_message.
    c1 = 'Dear'.
    c2 =  lv_name."STUDENTNAME .
    CONCATENATE c1 c2 ',' INTO
    wa_it_message-line   SEPARATED BY space.
    APPEND wa_it_message TO it_message.

    CLEAR wa_it_message.
    c1 = 'Susscess.'.
    wa_it_message-line = c1.
    APPEND wa_it_message TO it_message.

 
*&  SEND_EMAIL_MESSAGE
    gd_doc_data-doc_size = 1.
*Populate the subject/generic message attributes
    gd_doc_data-obj_langu = sy-langu.
    gd_doc_data-obj_name = 'SAPRPT'.
    gd_doc_data-obj_descr = psubject.
    gd_doc_data-sensitivty = 'F'.

*Describe the body of the message
    CLEAR wa_packing_list.
    REFRESH it_packing_list.
    wa_packing_list-transf_bin = space.
    wa_packing_list-head_start = 1.
    wa_packing_list-head_num = 0.
    wa_packing_list-body_start = 1.

    DESCRIBE TABLE it_message LINES wa_packing_list-body_num.
    wa_packing_list-doc_type = 'INT'."RAW'.
    APPEND wa_packing_list TO it_packing_list.

data : wa_file type Z_ATTA,
      lv_xstring type xstring,
      wa_CONTENTS_HEX type SOLIX,
      it_CONTENTS_HEX type standard table of SOLIX.
CALL FUNCTION 'Z_GET_ATTACHMENTS'
  EXPORTING
    DOUBTNO          = '00000011'
    LINEITEMNO       = '2'
 IMPORTING
   FILE             = wa_file.


*Attachment packing list
      wa_packing_list-TRANSF_BIN = 'X'.
      wa_packing_list-HEAD_START = 1.
      wa_packing_list-HEAD_NUM = 1.
      wa_packing_list-BODY_START = 1.
      wa_packing_list-BODY_NUM = 1.
      wa_packing_list-doc_type = 'doc'.
      wa_packing_list-obj_name = 'ATTACHMENT'.
      wa_packing_list-OBJ_DESCR =  'desc'.

      APPEND wa_packing_list to it_packing_list.

       

CALL FUNCTION 'HR_KR_STRING_TO_XSTRING'
        EXPORTING
          UNICODE_STRING         = wa_file-filecontent"string format
       IMPORTING
         XSTRING_STREAM         = lv_xstring.


*Attachment
clear it_binary_tab.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
        EXPORTING
          BUFFER                =  lv_xstring
       TABLES
          BINARY_TAB            = it_binary_tab.



    loop at it_BINARY_TAB into wa_BINARY_TAB.
      wa_contents_bin-line = wa_BINARY_TAB-line.
      append wa_contents_bin to it_contents_bin.
    endloop.

loop at it_binary_tab into wa_binary_tab.
   wa_CONTENTS_HEX-line = wa_binary_tab-lINE.
 append  wa_CONTENTS_HEX to it_CONTENTS_HEX.
endloop.

* Call the FM to post the message to SAPMAIL
    CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
      EXPORTING
        document_data              = gd_doc_data
        put_in_outbox              = 'X'
        commit_work                = 'X'
      TABLES
        packing_list               = it_packing_list
        contents_txt               = it_message
*        contents_bin            = it_contents_bin
         CONTENTS_HEX   = it_CONTENTS_HEX
        receivers                   = it_receivers
      EXCEPTIONS
        too_many_receivers         = 1
        document_not_sent          = 2
        document_type_not_exist    = 3
        operation_no_authorization = 4
        parameter_error            = 5
        x_error                    = 6
        enqueue_error              = 7
        OTHERS                     = 8.

The attachmen is currupted...please correct me where i am going wrong...

Regards,

Shamila

1 REPLY 1

kai_mattern2
Explorer
0 Kudos

I know this thread is very old but I'm having a simmilar problem (just with PDF not Word).

I want to send a timesheet from FB HRXSS_TIM_GET_TIMESTATEMENT to the user.

I get the timesheet (length is about 50k chars), i convert to binarry with 'CMS_XSTRING_TO_BINARY and i get 50x 1024 lines in the table.

When i now loop it into the it_contents_bin its 50x 255 chars and i think because of that its corrupted.

Im totally new to Abap but i have to solve this issue. Can anybody help me with this?

Here is the code (its a patchwork of different solution for sending attachements vvia SAP)

To test the code you have to fill in an existing HRFORM a personnel number and a email address.

Thanks you.

REPORT  Z_H5_ZEITNACHWEIS_PER_MAIL.


DATA: OBJPACK     LIKE SOPCKLSTI1 OCCURS  2 WITH HEADER LINE.
DATA: OBJHEAD     LIKE SOLISTI1   OCCURS  1 WITH HEADER LINE.
DATA: OBJBIN      LIKE SOLISTI1   OCCURS 10 WITH HEADER LINE.
DATA: OBJTXT      LIKE SOLISTI1   OCCURS 10 WITH HEADER LINE.
DATA: RECLIST     LIKE SOMLRECI1  OCCURS  5 WITH HEADER LINE.
DATA: DOC_CHNG    LIKE SODOCCHGI1.
DATA: TAB_LINES   LIKE SY-TABIX.
DATA: PERNR       TYPE PERNR-PERNR.
DATA: BEGIN_DATE  TYPE DATS.
DATA: END_DATE    TYPE DATS.
DATA: DISPLAY     TYPE XFELD.
DATA: HRFORM_NAME TYPE HRF_NAME_T.
DATA: PDF_DOC     TYPE XSTRING.
DATA: MESSAGE     TYPE BAPIRET2.

DATA: it_binary_tab type table of sdokcntbin.
DATA: wa_binary_tab like line of it_binary_tab.
DATA: it_contents_bin type table of SOLISTI1.
DATA: wa_contents_bin  like line of it_contents_bin.
DATA: wa_CONTENTS_HEX type SOLIX.
DATA: it_CONTENTS_HEX type standard table of SOLIX.

* Erstellen des zu versendenden Dokuments
DOC_CHNG-OBJ_NAME = 'Aktueller Zeitnachweis'.
DOC_CHNG-OBJ_DESCR = 'Zeitnachweis'.
OBJTXT = 'Bodytext'.
APPEND OBJTXT.
OBJTXT = 'more body text'.
APPEND OBJTXT.
OBJTXT = 'and even more body text'.
APPEND OBJTXT.
DESCRIBE TABLE OBJTXT LINES TAB_LINES.
READ TABLE OBJTXT INDEX TAB_LINES.
DOC_CHNG-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( OBJTXT ).

* Erstellen des Eintrags zum komprimierten Dokument
CLEAR OBJPACK-TRANSF_BIN.
OBJPACK-HEAD_START = 1.
OBJPACK-HEAD_NUM   = 0.
OBJPACK-BODY_START = 1.
OBJPACK-BODY_NUM   = TAB_LINES.
OBJPACK-DOC_TYPE   = 'RAW'.
APPEND OBJPACK.

* Content des Zeitnachweises holen
pernr      = '<an existing personnel number >'.
begin_date = '20080101'.
end_date   = '20080131'.
display    = ' '.
hrform_name = '<a existing form>'.

CALL FUNCTION 'HRXSS_TIM_GET_TIMESTATEMENT'
    EXPORTING
      pernr       = pernr
      begin_date  = begin_date
      end_date    = end_date
      display     = display
      hrform_name = hrform_name
    IMPORTING
      pdf_doc     = pdf_doc
      message     = message.

* timestatement is in XString now - change it to binarry
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
        EXPORTING
          BUFFER     =  pdf_doc
       TABLES
          BINARY_TAB = it_binary_tab.

* Erstellen der Anlage für das Dokument
CLEAR it_contents_bin.
loop at it_BINARY_TAB into wa_BINARY_TAB.
    wa_contents_bin-line = wa_BINARY_TAB-line.
    append wa_contents_bin to it_contents_bin.
endloop.

CLEAR it_CONTENTS_HEX.
loop at it_binary_tab into wa_binary_tab.
    wa_CONTENTS_HEX-line = wa_binary_tab-lINE.
    append  wa_CONTENTS_HEX to it_CONTENTS_HEX.
endloop.


OBJHEAD = 'Zeitnachweis.pdf'. APPEND OBJHEAD.

* Erstellen des Eintrags zur komprimierten Anlage
OBJPACK-TRANSF_BIN = 'X'.
OBJPACK-HEAD_START = 1.
OBJPACK-HEAD_NUM   = 1.
OBJPACK-BODY_START = 1.
OBJPACK-BODY_NUM   = TAB_LINES.
OBJPACK-DOC_TYPE   = 'BIN'.
OBJPACK-OBJ_NAME   = 'ANLAGE'.
OBJPACK-OBJ_DESCR  = 'Zeitnachweis.pdf'.
OBJPACK-DOC_SIZE   = TAB_LINES * 255.
APPEND OBJPACK.

* Füllen der Empfängerliste
RECLIST-RECEIVER = 'youremail AT yourcompany.com'.
RECLIST-REC_TYPE = 'U'.
APPEND RECLIST.

* Versenden des Dokuments
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
     EXPORTING
          DOCUMENT_DATA = DOC_CHNG
          PUT_IN_OUTBOX = 'X'
          COMMIT_WORK   = 'X'
     TABLES
          PACKING_LIST  = OBJPACK
          OBJECT_HEADER = OBJHEAD
          CONTENTS_BIN  = it_contents_bin
          CONTENTS_HEX  = it_contents_hex
          CONTENTS_TXT  = OBJTXT
          RECEIVERS     = RECLIST

     EXCEPTIONS
          TOO_MANY_RECEIVERS = 1
          DOCUMENT_NOT_SENT  = 2
          OPERATION_NO_AUTHORIZATION = 4
          OTHERS = 99.

CASE SY-SUBRC.
  WHEN 0.
    WRITE: / 'Ergebnis des Sendevorgangs:'.
    LOOP AT RECLIST.
      WRITE: / RECLIST-RECEIVER(48), ':'.
      IF RECLIST-RETRN_CODE = 0.
        WRITE 'erfolgreich versendet'.
      ELSE.
        WRITE 'nicht versendet'.
      ENDIF.
    ENDLOOP.
  WHEN 1.
    WRITE: / 'Keine Berechtigung, an die angegebene Anzahl Empfänger',
             'zu senden !'.
  WHEN 2.
    WRITE: / 'Dokument konnte an keinen Empfänger gesendet werden !'.
  WHEN 4.
    WRITE: / 'Keine Berechtigung zum Senden !'.
  WHEN OTHERS.
    WRITE: / 'Fehler beim Senden aufgetreten !'.
ENDCASE.