Mail verschicken mit Klasse CL BCS (Business Communication Service)
Aus SAP-Wiki
Zur Navigation springenZur Suche springen
Siehe Klasse CL BCS (Business Communication Service).
Mit der Klasse BCS_CL können recht komfortabel Mails aus SAP heraus verschickt werden. Hilfreich ist dasbei eine Vorlage, die man auch ggf. in einer Klasse kapseln sollte.
Das BCS kann leicht verwechselt werden mit dem BDS (Business Document Servies.
Coding 1 (Projektbeispiel in Klasse)
class YCL_EMAIL_KSCHL_ZTRD definition public final create public . public section. methods CONSTRUCTOR importing !IV_REFBETREFF type CHAR20 !IV_REFNR type CHAR10 !IV_KUNAG type KUNNR !IV_MSGV_CON type SYMSGV !IV_MSGV_FORM type SYMSGV . protected section. private section. data MV_MAIL type AD_SMTPADR value 'eberstein@visable.com' ##NO_TEXT. data MV_REFBETREFF type CHAR20 . data MV_REFNR type CHAR10 . data MV_KUNAG type KUNNR . data MV_MSGV_CON type SYMSGV . data MV_MSGV_FORM type SYMSGV . methods ADD_RECIPIENT changing !LCO_BCS type ref to CL_BCS returning value(RV_ERROR) type XFELD . methods SEND_MAIL returning value(RV_ERROR) type XFELD . ENDCLASS. CLASS YCL_EMAIL_KSCHL_ZTRD IMPLEMENTATION. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Private Method YCL_EMAIL_KSCHL_ZTRD->ADD_RECIPIENT * +-------------------------------------------------------------------------------------------------+ * | [<-->] LCO_BCS TYPE REF TO CL_BCS * | [<-()] RV_ERROR TYPE XFELD * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD add_recipient. DATA: lo_recipient TYPE REF TO if_recipient_bcs. TYPES: BEGIN OF ty_mail, mail TYPE ad_smtpadr, END OF ty_mail. DATA: lt_mail TYPE STANDARD TABLE OF ty_mail, lv_bname TYPE xubname, lt_return TYPE STANDARD TABLE OF bapiret2, lt_ADDSMTP TYPE STANDARD TABLE OF bapiadsmtp. * Check if enhancement is active (and get mail-adresse from table YENH. DATA: ls_yenh TYPE yenh. SELECT SINGLE * FROM yenh INTO ls_yenh WHERE ynumr = 'SD0015' * AND para1 = * AND para2 = * AND para3 = * AND para4 = . * Enhancement exists and is active and SAP-User (-> Included E-Mails) receiver are maintained IF sy-subrc = 0 AND ls_yenh-yaktiv = abap_true AND ls_yenh-paval IS NOT INITIAL. TRY. SPLIT ls_yenh-paval AT ',' INTO TABLE lt_mail. * Read mail to SAP-ser, fill all mail receiver LOOP AT lt_mail ASSIGNING FIELD-SYMBOL(<fs_mail>). lv_bname = <fs_mail>-mail. CALL FUNCTION 'BAPI_USER_GET_DETAIL' EXPORTING username = lv_bname TABLES return = lt_return addsmtp = lt_ADDSMTP. IF lt_addsmtp IS INITIAL. "MESSAGE i323(crm_email) WITH lv_bname. "no messages allowed .. 'Keine E-Mail-Adresse für Benutzer &1 definiert' CONTINUE. else. <fs_mail>-mail = lt_ADDSMTP[ 1 ]-e_mail. ENDIF. DATA(lv_success) = abap_true. " Move the receiver address. IF <fs_mail>-mail CS '@'. lo_recipient = cl_cam_address_bcs=>create_internet_address( <fs_mail>-mail ). ELSE. lo_recipient = cl_distributionlist_bcs=>getu_persistent( i_dliname = CONV #( <fs_mail>-mail ) i_private = space ). ENDIF. CALL METHOD lco_bcs->add_recipient EXPORTING i_recipient = lo_recipient i_express = 'X'. ENDLOOP. CATCH cx_send_req_bcs INTO DATA(lo_exception1). "MESSAGE lo_exception1->get_text( ) TYPE 'S' DISPLAY LIKE 'E'. CATCH cx_ADDRESS_BCS INTO DATA(lo_exception2). "MESSAGE lo_exception2->get_text( ) TYPE 'S' DISPLAY LIKE 'E'. ENDTRY. ELSE. rv_error = abap_true. RETURN. ENDIF. * No mail could be found to send IF lv_success = abap_false. rv_error = abap_true. RETURN. ENDIF. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method YCL_EMAIL_KSCHL_ZTRD->CONSTRUCTOR * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_REFBETREFF TYPE CHAR20 * | [--->] IV_REFNR TYPE CHAR10 * | [--->] IV_KUNAG TYPE KUNNR * | [--->] IV_MSGV_CON TYPE SYMSGV * | [--->] IV_MSGV_FORM TYPE SYMSGV * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD constructor. * Save importvariables to class atributes mv_refbetreff = iv_refbetreff. mv_refnr = iv_refnr. mv_kunag = iv_kunag. mv_msgv_con = iV_MSGV_CON. mv_msgv_form = iv_msgv_form. * Send Mail me->send_mail( ). "see Report YSD_OUTPUT_COLLECTION (PERFORM send_error_log) ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Private Method YCL_EMAIL_KSCHL_ZTRD->SEND_MAIL * +-------------------------------------------------------------------------------------------------+ * | [<-()] RV_ERROR TYPE XFELD * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD send_mail. * Copy BCS-Coding from Report YSD_OUTPUT_COLLECTION (there is attachment, not BODY) DATA: lo_bcs TYPE REF TO cl_bcs, lo_document TYPE REF TO cl_document_bcs, lo_sender TYPE REF TO cl_sapuser_bcs, lt_body TYPE soli_tab, lv_subject TYPE string, lv_str1 TYPE char10, lv_str2 TYPE char10. "IF lt_result IS NOT INITIAL. TRY. FREE: lo_bcs, lo_document, lo_sender, lv_subject. " Create persistent send request lo_bcs = cl_bcs=>create_persistent( ). * Fill receiver E-mail and check YENH (SD0015) rv_error = me->add_recipient( CHANGING lco_bcs = lo_bcs ). IF rv_error = abap_true. RETURN. ENDIF. " Email FROM... lo_sender = cl_sapuser_bcs=>create( sy-uname ). " Add sender to send request CALL METHOD lo_bcs->set_sender EXPORTING i_sender = lo_sender. MESSAGE s005(y_sd) INTO lv_subject. "'Eine Bedingung verhindert ZTRD-Nachr. in Rechnung' FREE: lt_body, lv_str1, lv_str2. WRITE sy-datlo TO lv_str1. WRITE sy-uzeit TO lv_str2. * Fill Body-Text lt_body = VALUE #( ( line = |Datum
| ) ( line = |Datum: { lv_str1 }
| ) "Date ( line = |Uhrzeit: { lv_str2 }
| ) "Time ( line = |{ space }
| ) ( line = |Referenz
| ) ( line = |{ mv_refbetreff }: { mv_refnr }
| ) "Reference (Invoice or partner RE) ( line = |Auftraggeber: { mv_kunag }
| ) "Reference (partner AG) ( line = |{ space }
| )
Coding 2
FORM send_mail . DATA: lo_send_request TYPE REF TO cl_bcs. DATA: lt_text TYPE bcsy_text. DATA: lv_text TYPE soli. DATA: lt_binary_content TYPE solix_tab. DATA: lo_document TYPE REF TO cl_document_bcs. DATA: lo_sender TYPE REF TO cl_sapuser_bcs. DATA: lo_recipient TYPE REF TO if_recipient_bcs. DATA: lx_bcs_exception TYPE REF TO cx_bcs. DATA: lv_sent_to_all TYPE os_boolean. DATA: lv_var TYPE string, lv_stext TYPE string, lv_percentage TYPE string. TRY. lo_send_request = cl_bcs=>create_persistent( ). SORT gt_outdat BY status. LOOP AT gt_outdat INTO DATA(ls_outdat) WHERE status <> icon_led_green. CASE ls_outdat-status. WHEN icon_led_red. lv_stext = | Der Schwellenwert für das Land { ls_outdat-land1 } wurde überschritten (siehe Transaktion ZSD_SW) | . WHEN icon_led_yellow. lv_percentage = ls_outdat-in_proz. REPLACE FIRST OCCURRENCE OF '&1' IN lv_var WITH lv_percentage. lv_stext = | Der Schwellenwert für das Land { ls_outdat-land1 } ist zu { lv_percentage } Prozent erreicht | . WHEN icon_message_critical. lv_stext = | Customizing fehlt für Land { ls_outdat-land1 } | . ENDCASE. lv_text = lv_stext. INSERT lv_text INTO TABLE lt_text. ENDLOOP. IF lt_text IS INITIAL. MESSAGE 'Keine Fehler-Mails zu versenden'(007) TYPE 'I'. ELSE. lo_document = cl_document_bcs=>create_document( i_type = 'RAW' i_text = lt_text i_length = '12' i_subject = CONV #( TEXT-008 ) ). "'Fehler bei Schwellwert-Prüfung' ). lo_send_request->set_document( lo_document ). lo_sender = cl_sapuser_bcs=>create( sy-uname ). lo_send_request->set_sender( EXPORTING i_sender = lo_sender ). lo_recipient = cl_cam_address_bcs=>create_internet_address( i_address_string = CONV #( p_madr ) ). lo_send_request->add_recipient( EXPORTING i_recipient|1= lo_recipient i_express = abap_true ). lo_send_request->send( EXPORTING i_with_error_screen|1= abap_true RECEIVING result = lv_sent_to_all ). COMMIT WORK. IF sy-subrc = 0. MESSAGE 'Fehler-Mails wurden versandt'(002) TYPE 'I'. ENDIF. ENDIF. CATCH cx_bcs INTO lx_bcs_exception. ENDTRY. ENDFORM.