sapdev logo background
sapdev logo sapdev logo

cl_bcs ABAP class interface to send emails from SAP

The following SAP ABAP code demonstrates how to send a simple email using the new CL_BCS class interface instead of older function modules such as SO_DOCUMENT_SEND_API1 and SO_NEW_DOCUMENT_ATT_SEND_API1 in ever older versions of SAP. Both methods achieve the same result, which is to send an email to an external email address via SOST but the CL_BCS method is the newer objects based method which is...Well I'll let you judge is it is easier. One think i will say is if you have not used either way before use this method.

What this example does
The example ABAP program below uses the report parameters detailed below to send an email with a subject, body text and a basic TXT based attachment.

p_subjct - Subject text
p_bodtxt - Text contained within email body
p_atttxt - Text contained within attachment
p_recip - Email Recipient

*& Example of sending external email via SAPCONNECT using
*& ABAP class interface CL_BCS
*& Produced by

*Data Declaration
DATA: it_contents   	TYPE STANDARD TABLE OF soli,
      wa_contents  	TYPE soli,
      it_attachment 	TYPE solix_tab,
      wa_receivers    	TYPE uiys_iusr,
      ld_subject    	TYPE so_obj_des,
      ld_att_size   	TYPE so_obj_len,
      ld_att_text   	TYPE xstring,
      ld_att_type   	TYPE so_obj_tp VALUE 'TXT',
      ld_att_sub    	TYPE so_obj_des VALUE 'Attachment'.

DATA: send_email    	TYPE REF TO cl_bcs,
      send_request   	TYPE REF TO cl_send_request_bcs,
      document      	TYPE REF TO cl_document_bcs,
      recipient      	TYPE REF TO if_recipient_bcs.

PARAMETERS:	p_subjct type string DEFAULT 'Subject',
		p_bodtxt type string DEFAULT 'main email body text',
		p_atttxt type string DEFAULT 'attachment text body',
		p_recip  type string DEFAULT ''.

  ld_subject = p_subjct.

  CONCATENATE '<html><body><p>' p_bodtxt '</p></body></html>'
                   into wa_contents-line.
  APPEND wa_contents to it_contents.

* Create instance of the email class
  send_email = cl_bcs=>create_persistent( ).

* Create email document inc type, subject and boby text
  document = cl_document_bcs=>create_document(
                i_type    = 'HTM'
                i_subject = ld_subject
                i_text    = it_contents ).

* Convert attachment text to xstring
      TEXT           = p_atttxt
*      MIMETYPE       = ' '
*      ENCODING       =
      BUFFER         = ld_att_text
      FAILED         = 1
      OTHERS         = 2.

* Add converted attachment text to the email attachment table
  it_attachment = cl_document_bcs=>xstring_to_solix( ld_att_text ).

* Calculate size of attachment
  ld_att_size   = xstrlen( ld_att_text ).

* Add the attachment table to the document
  CALL METHOD document->add_attachment(
          i_attachment_type     = ld_att_type
          i_attachment_subject  = ld_att_sub
          i_attachment_size     = ld_att_size
          i_att_content_hex     = it_attachment ).

* Assign document and all its details to the email
  CALL METHOD send_email->set_document( document ).

* Setup email recipient
  wa_receivers-email = p_recip.
  recipient = cl_cam_address_bcs=>create_internet_address( wa_receivers-email ).

*Assign recipient to email
  CALL METHOD send_email->add_recipient
            i_recipient = recipient
            i_express   = 'X'.

*Send email
  CALL METHOD send_email->send( i_with_error_screen = 'X' ).

* Commit work!!! This is important email will not get sent or appear in SOST without this
Once you have got this working and your email is appearing in transaction SOST there is much more functionality available within the CL_BCS class interface. Instead of me re-inventing the wheel there are a number of reports/programs already available in your SAP system that demonstrate how to implement most of it and if you use this as a starting point you can build on your knowledge as and when you need it:

BCS_EXAMPLE_1 - BCS Use, Example 1
BCS_EXAMPLE_2 - BCS Use, Example 2
BCS_EXAMPLE_3 - BCS Use, Example 3
BCS_EXAMPLE_4 - BCS Use, Example 1
BCS_EXAMPLE_5 - BCS Use, Example 5 (Winword Attachment)
BCS_EXAMPLE_6 - Send PDF Form by E-Mail
BCS_EXAMPLE_7 - BCS: Send E-Mail with Self-Created Excel Attachment (Example
BCS_EXAMPLE_8 - BCS: Send a Spool Request as PDF by E-Mail (Example)
BCS_TEST01 - BCS Test: Various Send Settings
BCS_TEST04 - BCS Test: Send Screen
BCS_TEST05 - BCS Test: Binary Attachment
BCS_TEST06 - BCS Use, Example 1
BCS_TEST09 - BCS Test: Add_Document_As_Attachment
BCS_TEST10 - BCS Test: Various Send Settings and Status Return
BCS_TEST11 - Test: Copy Methods
BCS_TEST12 - BCS Test: Send Several Documents in an LUW
BCS_TEST13 - BCS Test: Direct Sending
BCS_TEST14 - Test: BAdI on Send Screen
BCS_TEST15 - Test: Link with Application Object
BCS_TEST16 - Test for Reorganization: CL_BCS->DELETE
BCS_TEST17 - Simplest Example for Line Break Test
BCS_TEST18 - Reorganization Test: Link to Application Object
BCS_TEST19 - BCS Test: SMS Without Service
BCS_TEST22 - Simplest Example for Line Break Test

<--SAP Email processing Home

comments powered by Disqus