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
*&----------------------------------------------------------- *& Report ZSENDEMAIL *& *&----------------------------------------------------------- *& Example of sending external email via SAPCONNECT using *& ABAP class interface CL_BCS *& Produced by www.SAPDev.co.uk *&----------------------------------------------------------- REPORT ZSEND_BCSMAIL. *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 '[email protected]'. ************************************************************************ *START-OF-SELECTION START-OF-SELECTION. 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 CALL FUNCTION 'SCMS_STRING_TO_XSTRING' EXPORTING TEXT = p_atttxt * MIMETYPE = ' ' * ENCODING = IMPORTING BUFFER = ld_att_text EXCEPTIONS 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 EXPORTING 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 COMMIT WORK.