CL_BCS Class : 5 Easy Steps To Send Email with ABAP

SAP send email cl bcs

CL_BCS Class is the standard new SAP to handle and send email.

CL_BCS allows to
– Attach files to email
– Build the HTML/raw of body.
– Set email’s Senders & Receiver
– Send email …

This post is a step by step SAP ABAP Tutorial how to sends email from SAP with the CL_BCS in ABAP with sample ABAP code.

SAP Send Email CL BCS Class

The different steps are:

1- Create BCS Document

cl_document_bcs=>create_document(
 i_type ='HTM'
i_hex = lt_body_hex
i_subject = lv_mail_subject )

2- Set the e-mail address of the sender

IF NOT lv_sender_email IS INITIAL.
          lo_sender = cl_cam_address_bcs=>create_internet_address( 
                    i_address_string = lv_sender_email ).
ENDIF.

Or you can set the connected user as a sender with

lo_sender = cl_sapuser_bcs=>create( sy-uname ).

Check if an email is value in ABAP Two ways to Check if email is valid in ABAP : Function Vs Regex

3- Set the receiver

 lo_recipient = cl_cam_address_bcs=>create_internet_address(
                          i_address_string = lv_email ).
lo_send_request->add_recipient( lo_recipient ) .

If you want to retrieve the email address of an user,
use the function module “HR_FBN_GET_USER_EMAIL_ADDRESS”

lo_sender = cl_sapuser_bcs=>create( sy-uname  ).

4- Assign document to the send request

Add the sender to the send request

 lo_send_request->set_sender( lo_sender )..

5- Send mail CL BCS Class’s Send Method

 lv_sent_to_all  = lo_send_request->send(
                              lv_with_error_screen ).

Learn more about the new SAP ABAP with the following book ABAP Development for SAP HANA

ABAP Send Mail with CL_BCS Class

CL_BCS Class 5 Easy Steps To Send Email with ABAP

Here the complete code for Send Email CL_BCS ready to go.

  CLASS cl_bcs DEFINITION LOAD.

  "******************************************************"
  " SAP send mail with CL_BCS
  "******************************************************"
  DATA: lo_document         TYPE REF TO cl_document_bcs.
  DATA: lx_document_bcs     TYPE REF TO cx_document_bcs.
  DATA: lo_send_request     TYPE REF TO cl_bcs.
  DATA: lo_sender           TYPE REF TO if_sender_bcs.
  DATA: lv_sent_to_all      TYPE os_boolean  .
  DATA: lt_att_content_hex  TYPE solix_tab .
  DATA: lt_message_body     TYPE bcsy_text.
  DATA: lv_sender_email     TYPE adr6-smtp_addr.
  DATA: lo_recipient        TYPE REF TO if_recipient_bcs  .
  DATA: lv_with_error_screen  TYPE os_boolean .
  DATA: lv_length_mime      TYPE num12.
  DATA: lv_mime_type        TYPE w3conttype.
  DATA: lt_attachment       TYPE solix_tab.
  DATA: lv_attachment_type  TYPE soodk-objtp.
  DATA: lv_attachment_size  TYPE sood-objlen.
  DATA: lv_attachment_subject TYPE sood-objdes.
  DATA: lt_body_hex         TYPE solix_tab.
  DATA: lv_mail_subject     TYPE  so_obj_des.
  DATA: lv_type             TYPE string.
  DATA: lv_extension        TYPE string.
  DATA: lv_docid_str(12)    .



  "-------------------------------------------"
  " Send Email
  "-------------------------------------------"
  TRY.

      lo_send_request = cl_bcs=>create_persistent( ).

      " Set the subjest of email
      "lv_mail_subject up to 50 c.

      " Send in HTML format
      lo_document = cl_document_bcs=>create_document(
                           i_type    = 'HTM'
                           i_hex     = lt_body_hex
                           i_subject = lv_mail_subject ) .

      " add the document as an attachment
      IF lt_attachment[] IS NOT INITIAL .

        lv_attachment_size    = lv_length_mime.
        lt_att_content_hex[]  = lt_attachment[].

        lv_attachment_subject = "Your Attachment Name" . 

        lo_document->add_attachment(
                           i_attachment_type    = lv_attachment_type
                           i_attachment_size    = lv_attachment_size
                           i_attachment_subject = lv_attachment_subject
                           i_att_content_hex    = lt_att_content_hex ).
      ENDIF.

      "****************************"
      " EMAIL
      "*****************************"
      " set the e-mail address of the sender:
      lo_sender = cl_sapuser_bcs=>create( sy-uname ).

      " set the e-mail address of the recipient
      LOOP AT lt_receiver ASSIGNING .
        lo_recipient = cl_sapuser_bcs=>create( sy-uname )
        lo_send_request->add_recipient( lo_recipient ) .
      ENDLOOP.

      " assign document to the send request:
      lo_send_request->set_document( lo_document ).

      " add the sender:
      lo_send_request->set_sender( lo_sender ).


      MOVE space TO lv_with_error_screen.
      " SAP Send Email CL_BCS
      lv_sent_to_all  = lo_send_request->send( lv_with_error_screen ).

    CATCH cx_document_bcs INTO lx_document_bcs.


  ENDTRY.