Application Development and Automation 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: 
Read only

Email sending from abap code

anuj_srivastava
Active Participant
0 Likes
1,180

Hi,

I am trying to send an attachment with the mail from my abap code. I am providing the doc type as "RAW'. In the sandbox system the attachment is containing the data but with the same abap code in development client the attachment is coming empty.

What can be the reason for this?

Regards,

Anuj

6 REPLIES 6
Read only

Former Member
0 Likes
1,142

You do have data in the Dev client right?

How are you attaching the data?

Put the code here.

Read only

0 Likes
1,142

Hi Shambu,

Thanks for ur reply. Data is not a issue at all as i am populating some dummy data to be sent within the code itself. I am pasting my code.

*&---------------------------------------------------------------------*
*& DATA DECLARATION FOR EMAIL
*&---------------------------------------------------------------------*
data: docdata    type sodocchgi1,
       objpack    type standard table of sopcklsti1 with header line,
       objhead    type standard table of solisti1   with header line,
       objtxt     type standard table of solisti1   with header line,
       objbin     type standard table of solisti1   with header line,
       reclist    type standard table of somlreci1  with header line.

data: tab_lines  type sy-tabix,
       att_type   type soodk-objtp.
TYPES: BEGIN OF ty_test,
         text TYPE char255,
           END OF ty_test.

DATA: i_test TYPE STANDARD TABLE OF ty_test,
       wa_test TYPE ty_test.

class cl_abap_char_utilities definition load.
constants: con_tab  type c value cl_abap_char_utilities=>horizontal_tab,
            con_cret type c value cl_abap_char_utilities=>NEWLINE.

*Body of mail
   clear objtxt.
   clear objtxt[].

   concatenate 'HI' 'Sir' into objtxt separated by space.
   append objtxt.

   clear objtxt.
   append objtxt.

   clear objtxt.
   objtxt = 'Please see the attached file'.

   append objtxt.
   clear objtxt.



   describe table objtxt lines tab_lines.
   read table objtxt index tab_lines.



*Packing list for main document
   clear objpack.
   clear objpack[].

   objpack-head_start = 1.
   objpack-head_num   = 0.
   objpack-body_start = 1.
   objpack-body_num   = tab_lines.
   objpack-doc_type   = 'RAW'.
   append objpack.

*Attachment data
   clear objbin.
   clear objbin[].

   wa_test-text = 'THIS is TEST'.
   APPEND wa_test to i_test.
wa_test-text = 'This is test 2'.
APPEND wa_test to i_test.
wa_test-text = 'This is test 3'.
APPEND wa_test to i_test.
wa_test-text = 'This is test 4'.
APPEND wa_test to i_test.
wa_test-text = 'This is test 5'.
APPEND wa_test to i_test.

   loop at i_test into wa_test.

*    concatenate  wa_test-text con_tab
*      into objbin." separated by con_tab.

     concatenate con_cret wa_test-text into objbin.
     append objbin.
     clear objbin.

   endloop.

   describe table objbin lines tab_lines.
   read table objbin index tab_lines.
*Mail description
   clear docdata.

   docdata-doc_size  = ( tab_lines - 1 ) * 255 + strlen( objbin ).
   docdata-obj_name  = 'Intimation'.
   docdata-obj_descr = 'INTIMATION'.
   docdata-obj_langu = sy-langu.
*Packing list for attachment document
   clear objpack.
   att_type = 'RAW'.
   objpack-doc_size = tab_lines * 255 .
   objpack-transf_bin = 'X'.
   objpack-head_start = 1.
   objpack-head_num   = 1.
   objpack-body_start = 1.
   objpack-body_num   = tab_lines.
   objpack-doc_type   = att_type.
   objpack-obj_name   = 'Attachment'.
   objpack-obj_descr  = 'Report.txt'.
   append objpack.

   clear reclist.
   clear reclist[].
   reclist-receiver = 'anujsri2@gmail.com'..
   reclist-rec_type = 'U'.
   append reclist.
BREAK-POINT.
*Function module to send email with an attachment
   call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'
     exporting
       document_data              = docdata
       put_in_outbox              = 'X'
       commit_work                = 'X'
     tables
       packing_list               = objpack
       object_header              = objhead
       contents_bin               = objbin
       contents_txt               = objtxt
       receivers                  = reclist
     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.


   if sy-subrc <> 0.
*    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
   else.

   endif.

Read only

0 Likes
1,142

The same code produces different results in 2 systems right?

Are you on different releases?

You should try to use CL_BCS for sending mails instead of using outdated FM's.

Thanks,

Shambu

Read only

0 Likes
1,142

Hi Shambu,

The release levels are same. I have even tried CL_BCS also. With this also i faced the same issue. Attached is the code.

*TYPES: BEGIN OF ty_log,
*        text TYPE char255,
*       END OF ty_log.
* DATA: im_err_log TYPE STANDARD TABLE OF ty_log,
*       lwa_err_log TYPE ty_log.
*CLASS cl_bcs DEFINITION LOAD.
*DATA:  lo_send_request TYPE REF TO cl_bcs
*      ,lo_document     TYPE REF TO cl_document_bcs
*      ,lo_sender       TYPE REF TO if_sender_bcs
*      ,lo_recipient    TYPE REF TO if_recipient_bcs      ,lt_message_body TYPE bcsy_text
*      ,lx_document_bcs TYPE REF TO cx_document_bcs
*      ,lv_send         TYPE ad_smtpadr VALUE 'anuj.srivastava@in.ibm.com'
*      ,lv_sent_to_all  TYPE os_boolean     .
*"create send request
*lo_send_request = cl_bcs=>create_persistent( ).
*
*"create message body and subject
*APPEND 'Dear Vendor,' TO lt_message_body.
*APPEND INITIAL LINE to lt_message_body.
*APPEND 'Please fill the attached .' TO lt_message_body.
*APPEND INITIAL LINE to lt_message_body.
*APPEND 'Thank You,' TO lt_message_body.
*
*"put your text into the document
*lo_document = cl_document_bcs=>create_document(
*                 i_type = 'RAW'
*                 i_text = lt_message_body
*                 i_subject = 'US Bank log' ).
*DATA: li_attach TYPE soli_tab,
*      lwa_attach TYPE soli.
*lwa_err_log-text = 'Error with record type for Total record for Account number String 20'.
**             con_cret INTO lwa_err_log-err_text.
*APPEND lwa_err_log to im_err_log.
*lwa_err_log-text = 'Error with record type for Total record for Account number String 51'.
**            con_cret INTO .
*APPEND lwa_err_log to im_err_log.
*
*LOOP AT im_err_log INTO lwa_err_log.
*lwa_attach-line = lwa_err_log-text.
*APPEND lwa_attach to li_attach.
*ENDLOOP.
*
*call function 'SO_RAW_INT_TO_RTF'
*    tables
*      objcont_old       = li_attach
*      objcont_new       = li_attach.
*
*"put your text into the document
**lo_document = cl_document_bcs=>create_document(
**                 i_type = 'TXT'
**                 i_text = li_attach
**                 i_subject = 'Bank log' ).
*DATA: l_lines TYPE i,
*      size TYPE SO_OBJ_LEN.
*DESCRIBE TABLE li_attach LINES l_lines.
*size = l_lines * 255.
*TRY.
*  lo_document->add_attachment(
*    EXPORTING
*      i_attachment_type = 'BIN'
*      i_attachment_subject = 'Error'
*      i_attachment_size = size"l_lines * 255
*      i_att_content_text = li_attach ).
*
*  CATCH cx_document_bcs INTO lx_document_bcs.
*ENDTRY.
*
** Add attachment
** Pass the document to send request
*lo_send_request->set_document( lo_document ).
*
*
*"Create sender
*lo_sender = cl_cam_address_bcs=>create_internet_address( lv_send ).
*
*"Set sender
*lo_send_request->set_sender( lo_sender ).
*
*"Create recipient
*lo_recipient = cl_sapuser_bcs=>create( sy-uname ).
*
**Set recipient
*lo_send_request->add_recipient(
*     EXPORTING
*       i_recipient = lo_recipient i_express = 'X' ).
*
*lo_send_request->add_recipient( lo_recipient ).
*
** Send email
*lo_send_request->send(
*  EXPORTING
*    i_with_error_screen = 'X'
*  RECEIVING
*    result = lv_sent_to_all ).
*
*COMMIT WORK.

Read only

0 Likes
1,142

I will check this tom..dont have access to SAP now.

Read only

0 Likes
1,142

Hi,

Both are working fine for me. In BCS i had to change the attachment type to TXT instead of BIN though.

Thanks,

Shambu