Send email using CL_BCS class in ABAP

1
5491

Dear SAPLearners, in this code snippet blog post we will learn how to send email using CL_BCS class in ABAP.

Lets get started.

Below is the code to send emails using CL_BCS class in abap.

*&---------------------------------------------------------------------*
*& Report  ZSL_EMAIL_CL_BCS                                            *
*&---------------------------------------------------------------------*
*&                  www.saplearners.com                                *
*&---------------------------------------------------------------------*

REPORT  zsl_email_cl_bcs.


CONSTANTS:
  lc_subject TYPE so_obj_des VALUE 'Email using CL_BCS Class',
  lc_raw     TYPE char03     VALUE 'RAW'.

DATA:
  lv_mlrec         TYPE so_obj_nam,
  lv_sent_to_all   TYPE os_boolean,
  lv_email         TYPE adr6-smtp_addr,
  lv_subject       TYPE so_obj_des,
  lv_text          TYPE bcsy_text,
  lr_send_request  TYPE REF TO cl_bcs,
  lr_bcs_exception TYPE REF TO cx_bcs,
  lr_recipient     TYPE REF TO if_recipient_bcs,
  lr_sender        TYPE REF TO cl_sapuser_bcs,
  lr_document      TYPE REF TO cl_document_bcs.



TRY.
    "Create send request
    lr_send_request = cl_bcs=>create_persistent( ).


    "Email From
    lr_sender = cl_sapuser_bcs=>create( sy-uname ).

    "Add sender to send request
    CALL METHOD lr_send_request->set_sender
      EXPORTING
        i_sender = lr_sender.


    "Email To...
    lv_email = 'admin@saplearners.com'.
    lr_recipient = cl_cam_address_bcs=>create_internet_address( lv_email ).

    "Add recipient to send request
    CALL METHOD lr_send_request->add_recipient
      EXPORTING
        i_recipient = lr_recipient
        i_express   = 'X'.


    "Email BODY
    APPEND 'Hello world! My first ABAP email using Class CL_BCS!' TO lv_text.
    lr_document = cl_document_bcs=>create_document(
                    i_type    = lc_raw
                    i_text    = lv_text
                    i_subject = lc_subject ).

    "Add document to send request
    CALL METHOD lr_send_request->set_document( lr_document ).


    "Send email
    CALL METHOD lr_send_request->send(
      EXPORTING
        i_with_error_screen = 'X'
      RECEIVING
        result              = lv_sent_to_all ).
    IF lv_sent_to_all = 'X'.
      WRITE 'Email sent successully!'.
    ENDIF.

    "Commit to send email
    COMMIT WORK.          "MANDATORY*


    "Exception handling
  CATCH cx_bcs INTO lr_bcs_exception.
    WRITE:
      'Error!',
      'Error type:',
      lr_bcs_exception->error_type.
ENDTRY.

Please feel free to comment and let us know your feedback. Subscribe for more updates

If you liked it, please share it! Thanks!

mail

SAPLearners.com is now on Telegram. Click here to join our channel and stay updated with the latest tutorials and updates.

Like us on Facebook and follow us on Twitter.

Comments are closed.