Skip to content

CL_BCS

Send emails with attachments from ABAP using the Business Communication Services API.

Purpose

The standard OO API for sending emails from ABAP. Supports plain text and HTML bodies, multiple recipients (TO/CC/BCC), and file attachments. Works in dialog and background. Replaces the older SO_NEW_DOCUMENT_ATT_SEND_API1 function module.

Key classes:

Class Role
CL_BCS Main send request — the envelope
CL_DOCUMENT_BCS Email body / document
CL_CAM_ADDRESS_BCS Internet address recipient (external email)
CL_SAPUSER_BCS SAP user recipient (internal)

Key methods

Method Description
CL_BCS=>CREATE_PERSISTENT( ) Static factory — creates the send request
lo_send_request->set_sender( ) Set the From address
lo_send_request->add_recipient( ) Add TO / CC / BCC recipient
lo_send_request->set_send_immediately( ) abap_true dispatches at COMMIT; default queues in SOST
lo_send_request->send( ) Finalise and enqueue the email
CL_DOCUMENT_BCS=>CREATE_DOCUMENT( ) Static factory — creates body document
lo_document->add_attachment( ) Attach a file to the document

Example

REPORT z_bcs_send_email.

DATA: lo_send_request TYPE REF TO cl_bcs,
      lo_document     TYPE REF TO cl_document_bcs,
      lo_recipient    TYPE REF TO if_recipient_bcs,
      lt_body         TYPE bcsy_text,
      lv_sent         TYPE os_boolean.

START-OF-SELECTION.

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

      " 2. Build the email body (plain text)
      APPEND 'Hello,' TO lt_body.
      APPEND 'This is an automated message from ABAP.' TO lt_body.

      lo_document = cl_document_bcs=>create_document(
        i_type    = 'RAW'           " 'HTM' for HTML body
        i_text    = lt_body
        i_subject = 'Test Email from ABAP' ).

      lo_send_request->set_document( lo_document ).

      " 3. Add a TO recipient by internet address
      lo_recipient = cl_cam_address_bcs=>create_internet_address(
        i_address_string = 'recipient@example.com' ).

      lo_send_request->add_recipient(
        i_recipient = lo_recipient
        i_express   = abap_true ).

      " 4. Send immediately (not via SOST queue)
      lo_send_request->set_send_immediately( abap_true ).

      " 5. Dispatch — COMMIT WORK is mandatory
      lo_send_request->send( IMPORTING sent_to_all = lv_sent ).
      COMMIT WORK.

      IF lv_sent = abap_true.
        MESSAGE 'Email sent successfully.' TYPE 'S'.
      ENDIF.

    CATCH cx_bcs INTO DATA(lx_bcs).
      MESSAGE lx_bcs->get_text( ) TYPE 'E'.
  ENDTRY.

Adding an attachment

" Convert a string to SOLIX lines (255-char chunks)
DATA: lt_attachment  TYPE solix_tab,
      lv_size        TYPE so_obj_len,
      lv_content     TYPE string.

lv_content = 'col1,col2' && cl_abap_char_utilities=>newline && '1,2'.

CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
  EXPORTING  text   = lv_content
  TABLES     ftext  = lt_attachment.

lv_size = xstrlen( cl_abap_codepage=>convert_to( lv_content ) ).

lo_document->add_attachment(
  i_attachment_type    = 'CSV'
  i_attachment_subject = 'export.csv'
  i_attachment_size    = lv_size
  i_att_content_text   = lt_attachment ).

Common pitfalls

COMMIT WORK is mandatory

send( ) only enqueues the email. Without a subsequent COMMIT WORK the message is never dispatched — not even to the SOST queue.

set_send_immediately( ) controls timing

Without it (the default) the email sits in the SOST outbox and is dispatched by the RSCONN01 background job. Pass abap_true to send at COMMIT time.

HTML body

Pass i_type = 'HTM' to CREATE_DOCUMENT. The i_text table lines are then raw HTML. Inline images are not supported — use <img src="https://..."> links instead.

  • Attachment content must be a table of SOLIX lines (each 255 bytes). Use SCMS_STRING_TO_FTEXT for text files or SCMS_XSTRING_TO_SOLIX for binary content.
  • Sender address must match an authorised From address or a SAP system address, otherwise SAPConnect may reject it.

See also

  • Send email tutorial
  • Transaction SOST — outbox / failed message monitor
  • Transaction SCOT — SAPConnect configuration (SMTP node)

Comments