SAP_BC教程-发送邮件

 
"SAP-SE38-BCS_EXAMPLE_7

"前提是要设置了个人账号里的发送邮件地址,此邮件要在smtp服务里配置过




REPORT
ywt_bcs_example_7. * This report provides an example for sending an Excel * attachment in Unicode Systems CONSTANTS: gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab, gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf. PARAMETERS: mailto TYPE ad_smtpadr DEFAULT 'tao.wang17@ctg.cn'. "#EC * DATA send_request TYPE REF TO cl_bcs. DATA document TYPE REF TO cl_document_bcs. DATA recipient TYPE REF TO if_recipient_bcs. DATA bcs_exception TYPE REF TO cx_bcs. DATA main_text TYPE bcsy_text. DATA binary_content TYPE solix_tab. DATA size TYPE so_obj_len. DATA sent_to_all TYPE os_boolean. START-OF-SELECTION. PERFORM create_content. PERFORM send. *&---------------------------------------------------------------------* *& Form send *&---------------------------------------------------------------------* FORM send. TRY. * -------- create persistent send request ------------------------ send_request = cl_bcs=>create_persistent( ). * -------- create and set document with attachment --------------- * create document object from internal table with text APPEND 'Hello world!' TO main_text. "#EC NOTEXT document = cl_document_bcs=>create_document( i_type = 'RAW' i_text = main_text i_subject = 'Test Created By BCS_EXAMPLE_7' ). "#EC NOTEXT * add the spread sheet as attachment to document object document->add_attachment( i_attachment_type = 'xls' "#EC NOTEXT i_attachment_subject = 'ExampleSpreadSheet' "#EC NOTEXT i_attachment_size = size i_att_content_hex = binary_content ). * add document object to send request send_request->set_document( document ). * --------- add recipient (e-mail address) ----------------------- * create recipient object recipient = cl_cam_address_bcs=>create_internet_address( mailto ). * add recipient object to send request send_request->add_recipient( recipient ). send_request->set_send_immediately( 'X' )."设置为立即发送 * ---------- send document --------------------------------------- sent_to_all = send_request->send( i_with_error_screen = 'X' ). COMMIT WORK. IF sent_to_all IS INITIAL. MESSAGE i500(sbcoms) WITH mailto. ELSE. MESSAGE s022(so). ENDIF. * ------------ exception handling ---------------------------------- * replace this rudimentary exception handling with your own one !!! CATCH cx_bcs INTO bcs_exception. MESSAGE i865(so) WITH bcs_exception->error_type. ENDTRY. ENDFORM. "send *&---------------------------------------------------------------------* *& Form create_content *&---------------------------------------------------------------------* * Create Example Content * 1) Write example text into a string * 2) convert this string to solix_tab *----------------------------------------------------------------------* FORM create_content. DATA lv_string TYPE string. DATA ls_t100 TYPE t100. * -------------------------------------------------------------- * as example content we use some system messages out of t100 * get them for all installed languages from db * and write one line for each language into the spread sheet * columns are separated by TAB and each line ends with CRLF CONCATENATE 'This Is Just Example Text!' "#EC NOTEXT gc_crlf gc_crlf INTO lv_string. * header line CONCATENATE lv_string 'MSGID' gc_tab 'MSGNO' gc_tab 'Language' gc_tab "#EC NOTEXT 'Text' gc_crlf "#EC NOTEXT INTO lv_string. * data lines SELECT * FROM t100 INTO ls_t100 WHERE arbgb = 'SO' AND msgnr = '182'. CONCATENATE lv_string ls_t100-arbgb gc_tab ls_t100-msgnr gc_tab ls_t100-sprsl gc_tab ls_t100-text gc_crlf INTO lv_string. ENDSELECT. SELECT * FROM t100 INTO ls_t100 WHERE arbgb = 'SO' AND msgnr = '316'. CONCATENATE lv_string ls_t100-arbgb gc_tab ls_t100-msgnr gc_tab ls_t100-sprsl gc_tab ls_t100-text gc_crlf INTO lv_string. ENDSELECT. * -------------------------------------------------------------- * convert the text string into UTF-16LE binary data including * byte-order-mark. Mircosoft Excel prefers these settings * all this is done by new class cl_bcs_convert (see note 1151257) TRY. cl_bcs_convert=>string_to_solix( EXPORTING iv_string = lv_string iv_codepage = '4103' "suitable for MS Excel, leave empty iv_add_bom = 'X' "for other doc types IMPORTING et_solix = binary_content ev_size = size ). CATCH cx_bcs. MESSAGE e445(so). ENDTRY. ENDFORM. "create_content *--------------------------------------------------------------- * NOTES: *--------------------------------------------------------------- * UTF-16LE including the BOM (Byte order mark) * is preferred by Microsoft Excel. If you want to create * other binary content you may choose another codepage (e.g. * '4110' (UTF-8) which is standard for e-mails). * Find SAP codepage names in the drop down list * for the codepage setting of node SMTP in transaction SCOT. * Or: leave iv_codepage and iv_add_bom empty. Then the target * codepage is set according to SAPconnect settings * * Important: * SAP neither guarantees that the attachment created * by this report can be opened by all Excel Versions nor * that it can be opened by any 3rd party software at all

 

posted @ 2020-12-31 16:36  taogee  阅读(300)  评论(0)    收藏  举报