How to send mail using CL_BCS via adding XLS table attachment in ABAP
The class CL_BCS is the actual interface to the application. The application can create and send a send request using this interface. The interface classes are used to create senders, recipients, and objects that can be sent by BCS. These classes are provided by BCS for the document types and address types frequently used in the SAP System. If they do not suffice, the application must implement classes of its own.
Prerequisites
All object types that support the interface IF_DOCUMENT_BCS can be sent. The recipients and senders of documents must support the interface IF_RECIPIENT_BCS or IF_SENDER_BCS. BCS has implemented these interfaces for frequently used object types, recipient types and sender types, so you do not have to do anything else here. The objects sent can be related to the application. The objects must be implemented as business objects in the Business Object Repository for this.
The CL_BCS class is a standard tool in SAP ABAP used for sending emails. It allows you to perform the following actions when sending an email:
- Adding Attachments: You can attach files to the email.
- Creating HTML or Plain Text Content: You can create the email body in either HTML or plain text format.
- Setting Recipients and Senders: You can specify the recipients and sender of the email.
- Sending the Email: You can send the composed email.
…
The key difference between this class and other email sending methods lies in its more object-oriented approach and greater stability. SAP recommends using the CL_BCS class for email communication. For example, if you want the reply to go to a general mailbox (e.g., customer-support@mycompany.com) instead of your personal email address when sending an email to a customer, you can change the sender using this class.
Here’s a example of ABAP code using the CL_BCS class to send an email:
Method send_mail.
CONSTANTS : gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf,
gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab.
DATA : lo_recipient TYPE REF TO if_recipient_bcs VALUE IS INITIAL,
lo_sender TYPE REF TO if_sender_bcs VALUE IS INITIAL,
lo_bcs TYPE REF TO cl_bcs,
lt_soli TYPE soli_tab.
DATA : lv_status TYPE bcs_rqst,
lv_content TYPE string,
lv_send_all TYPE os_boolean.
DATA : lv_sender TYPE adr6-smtp_addr VALUE 'tufan.sasmaz@bs.nttdata.com',
ls_address TYPE bapiaddr3,
lt_return TYPE TABLE OF bapiret2.
DATA : lv_att_size TYPE sood-objlen,
lt_att_content_hex TYPE solix_tab,
lv_att_line TYPE string.
DATA : lv_sndto TYPE adr6-smtp_addr,
lv_sndcc TYPE adr6-smtp_addr,
lv_menge TYPE string,
lv_subj TYPE so_obj_des VALUE 'Sending Mail using BCS'.
DATA(lo_gbt) = NEW cl_gbt_multirelated_service( ).
DATA(lo_doc_bcs) = NEW cl_document_bcs( ).
*you can get the recipients and cc from table (optional)
SELECT * FROM zztest_mail
INTO TABLE @DATA(lt_email).
LOOP AT lt_email INTO DATA(ls_email).
CLEAR: lv_content, lt_soli, lv_sndto, lv_sndcc, lv_menge,
lv_att_line, lt_att_content_hex, lv_att_size.
"Create persistent send request
lo_bcs = cl_bcs=>create_persistent( ).
"Build Mail Content
lv_content = '<!DOCTYPE html> '
&& '<html> '
&& ' <head> '
&& ' <meta charset"utf=8">'
&& '</head> '
&& '<body> '
&& '<h3>Send Mail via XLS Table Attachement</h3>'
&& '</body> '
&& '</html> '.
lt_soli = cl_document_bcs=>string_to_soli( lv_content ).
lo_gbt->set_main_html(
EXPORTING
content = lt_soli ).
lo_doc_bcs = cl_document_bcs=>create_from_multirelated(
i_subject = lv_subj
i_multirel_service = lo_gbt ).
"Set Mail Sender
TRY.
lo_sender = cl_cam_address_bcs=>create_internet_address( lv_sender ).
lo_bcs->set_sender( i_sender = lo_sender ).
CATCH cx_address_bcs.
RETURN.
ENDTRY.
"Set Mail Recipient
TRY.
lv_sndto = CONV adr6-smtp_addr( ls_email-sndto ).
lo_recipient = cl_cam_address_bcs=>create_internet_address( lv_sndto ).
lo_bcs->add_recipient( i_recipient = lo_recipient
i_express = abap_true ).
CATCH cx_send_req_bcs.
ENDTRY.
"Set Mail CC
TRY.
lv_sndcc = CONV adr6-smtp_addr( ls_email-sndcc ).
IF lv_sndcc IS NOT INITIAL.
lo_recipient = cl_cam_address_bcs=>create_internet_address( lv_sndcc ).
lo_bcs->add_recipient( i_recipient = lo_recipient
i_copy = abap_true ).
ENDIF.
CATCH cx_send_req_bcs .
CATCH cx_root .
ENDTRY.
"Create TABLE contents for XLS
CONCATENATE 'XLS Table'
gc_crlf gc_crlf INTO lv_att_line.
CONCATENATE lv_att_line
'MBLNR' gc_tab
'MJAHR' gc_tab
'BWART' gc_tab
'WERKS' gc_tab
'LGORT' gc_tab
'MATNR' gc_tab
'MAKTX' gc_tab
'EAN11' gc_tab
'CHARG' gc_tab
'HSDAT' gc_tab
'VFDAT' gc_tab
'SHKZG' gc_tab
'BUKRS' gc_tab
'UMWRK' gc_tab
'UMLGO' gc_tab
'BLDAT' gc_tab
'BUDAT' gc_tab
'MEINS' gc_tab
'MENGE' gc_tab
'KUNNR' gc_tab
'NAME1' gc_tab
'KVERM' gc_tab
'STCD2' gc_tab
'WEMPF' gc_tab
'VBELN' gc_crlf INTO lv_att_line.
LOOP AT me->data INTO DATA(ls_itab).
lv_menge = CONV string( ls_itab-menge ).
CONCATENATE lv_att_line
ls_itab-mblnr gc_tab
ls_itab-mjahr gc_tab
ls_itab-bwart gc_tab
ls_itab-werks gc_tab
ls_itab-lgort gc_tab
ls_itab-matnr gc_tab
ls_itab-maktx gc_tab
ls_itab-ean11 gc_tab
ls_itab-charg gc_tab
ls_itab-hsdat gc_tab
ls_itab-vfdat gc_tab
ls_itab-shkzg gc_tab
ls_itab-bukrs gc_tab
ls_itab-umwrk gc_tab
ls_itab-umlgo gc_tab
ls_itab-bldat gc_tab
ls_itab-budat gc_tab
ls_itab-meins gc_tab
lv_menge gc_tab
ls_itab-kunnr gc_tab
ls_itab-name1 gc_tab
ls_itab-kverm gc_tab
ls_itab-stcd2 gc_tab
ls_itab-wempf gc_tab
ls_itab-vbeln gc_crlf INTO lv_att_line.
ENDLOOP.
cl_bcs_convert=>string_to_solix(
EXPORTING
iv_string = lv_att_line
iv_codepage = '4103'
iv_add_bom = 'X'
IMPORTING
et_solix = lt_att_content_hex
ev_size = lv_att_size ).
lo_doc_bcs->add_attachment(
EXPORTING
i_attachment_type = 'XLS'
i_attachment_subject = 'ATTAHMENET_NAME'
i_attachment_size = lv_att_size
i_att_content_hex = lt_att_content_hex ).
"Set document
lo_bcs->set_document( i_document = lo_doc_bcs ).
lv_status = 'N'.
lo_bcs->set_status_attributes( i_requested_status = lv_status ).
"Send Mail
TRY.
lo_bcs->set_send_immediately( i_send_immediately = abap_true ).
lv_send_all = lo_bcs->send( i_with_error_screen = abap_true ).
"Exception Hangdling
CATCH cx_send_req_bcs INTO DATA(o_root).
statu-statu = c_error.
statu-info = o_root->get_text( ).
ENDTRY.
ENDLOOP.
COMMIT WORK.
statu-statu = c_success.
MESSAGE s001 INTO statu-info.
ENDMETHOD.
The CL_BCS class is a useful and reliable tool for sending emails in SAP ABAP. It offers a more flexible and object-oriented approach compared to other email sending methods.
REFERENCES
CL_BCS Class : 5 Easy Steps To Send Email with ABAP — SAP4TECH
SAP S/4HANA Business Partner — Field Enhancement — SAP Community
Solved: Why use class CL_BCS for sending emails? — SAP Community