SAP ABAP - Send Email
REPORT ztest_email.
*Prepare Mail Object
DATA: lo_send_request TYPE REF TO cl_bcs VALUE IS INITIAL.
CLASS cl_bcs DEFINITION LOAD.
DATA: lo_document TYPE REF TO cl_document_bcs VALUE IS INITIAL.
DATA : i_text TYPE bcsy_text.
DATA : w_text LIKE LINE OF i_text.
DATA: lo_sender TYPE REF TO if_sender_bcs VALUE IS INITIAL.
DATA: lo_recipient TYPE REF TO if_recipient_bcs VALUE IS INITIAL.
PARAMETERS : p_email TYPE adr6-smtp_addr.
PARAMETERS: p_sub TYPE char50.
PARAMETERS : p_send AS CHECKBOX.
START-OF-SELECTION.
lo_send_request = cl_bcs=>create_persistent( ).
w_text-line = 'Testing Email'.
APPEND w_text TO i_text.
CLEAR w_text.
w_text-line = 'Testing email 2'.
APPEND w_text TO i_text.
CLEAR w_text.
lo_document = cl_document_bcs=>create_document(
i_type = 'TXT'
i_text = i_text
i_subject = p_sub ).
lo_send_request->set_document( lo_document ).
TRY.
lo_sender = cl_sapuser_bcs=>create( sy-uname ). "sender is the logged in user
* Set sender to send request
lo_send_request->set_sender(
EXPORTING
i_sender = lo_sender ).
ENDTRY.
**Set recipient
lo_recipient = cl_cam_address_bcs=>create_internet_address( p_email ).
TRY.
lo_send_request->add_recipient(
EXPORTING
i_recipient = lo_recipient
i_express = 'X' ).
ENDTRY.
TRY.
CALL METHOD lo_send_request->set_send_immediately
EXPORTING
i_send_immediately = p_send.
ENDTRY.
TRY.
lo_send_request->send(
EXPORTING
i_with_error_screen = 'X' ).
COMMIT WORK.
IF sy-subrc = 0.
WRITE :/ 'Mail sent'.
ENDIF.
ENDTRY.
Comments
Post a Comment