SAP Example of sending external email via SAPCONNECT using

Example of sending external email via SAPCONNECT using







  


*& Report ZSENDEMAIL
 *&
 *&-----------------------------------------------------------
 *& Example of sending external email via SAPCONNECT using
 *& ABAP class interface CL_BCS
 *&-----------------------------------------------------------
 REPORT zsendemail.
 *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 'XLS',
       ld_att_sub    TYPE so_obj_des VALUE 'Attachment'. "Attachment name
 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 'testdev.com'. CONSTANTS: gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab, gc_crlf TYPE c VALUE cl_abap_char_utilities=>cr_lf. ************************************************************************ *START-OF-SELECTION START-OF-SELECTION. ld_subject = p_subjct. CONCATENATE '

' p_bodtxt '

' 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 ). CONCATENATE p_atttxt gc_crlf p_atttxt INTO p_atttxt. * 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.