Send mail with out using commit work with cl_bcs_message class

 In my last post Send mail using CL_BCS library We explored the class to achieve mail sending functionalities. The class is a really powerful tool but it has a shortcoming i.e. it needs to commit work.

It's not a problem most of the time unless you are working with User-Exit and BADI. In this post, I wanted to showcase the usage of CL_BCS_MESSAGE class.

The class only collects data and depending upon how you set the attributes of the class object, function module SBCS_SEND or SBCS_SEND_UPDATE is called. This means we can use this class to send emails from User-exit or BADI.

Below is a sample code.

REPORT syst-repid.
PARAMETERS p_update TYPE char1 AS CHECKBOX.
DATA:
lo_message TYPE REF TO cl_bcs_message.
TRY.
CREATE OBJECT lo_message.
lo_message->set_main_doc( iv_contents_txt = |Test Email| " Main Documet, First Body Part
iv_doctype = 'txt' " Document Category
).
lo_message->add_attachment( iv_filename = 'My Text file.txt'
iv_contents_txt = `My attached txt file content.` ).
lo_message->add_recipient( 'Test@domain.com' ). " Communication Address (for INT, FAX, SMS, and so on)
lo_message->set_sender( 'Test@domain.com' )." Communication Address (for INT, FAX, SMS, and so on)
lo_message->set_update_task( p_update ).
lo_message->set_send_immediately( boolc( p_update = abap_false ) ).
lo_message->send( ).
IF p_update = abap_true.
COMMIT WORK AND WAIT.
ENDIF.
CATCH cx_bcs_send.
MESSAGE e000(db) WITH 'Error Sending Email.'.
ENDTRY.

Hope You like the snippet and use this information in your day to day work.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP