SAP 发送邮件 面向对象

时间:2021-10-29 05:48:38
REPORT ZMMR0068_YYN.

CONSTANTS:
gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab, "CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf. "CL_ABAP_CHAR_UTILITIES=>CR_LF PARAMETERS: mailto TYPE ad_smtpadr . " 收件人 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 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!'
gc_crlf gc_crlf
INTO lv_string. DO TIMES.
CONCATENATE lv_string
'' gc_tab
'' gc_tab
'' gc_tab
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' gc_tab
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' gc_tab
'ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc' gc_tab
'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd' gc_tab
'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' gc_tab
'fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' gc_tab
'ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg' gc_tab
'hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh' gc_tab
'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii' gc_tab
'jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj' gc_tab
'kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk' gc_crlf
INTO lv_string.
ENDDO.
* --------------------------------------------------------------
* 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 = '' "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 *&---------------------------------------------------------------------*
*& 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. " 邮件内容
document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = main_text
i_subject = 'Test Created By BCS_EXAMPLE_7' ). " 邮件主题名 * add the spread sheet as attachment to document object
document->add_attachment(
i_attachment_type = 'xls' " 附件格式
i_attachment_subject = 'ExampleSpreadSheet' " attachment name
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 document ---------------------------------------
sent_to_all = send_request->send( i_with_error_screen = 'X' ). COMMIT WORK.
WAIT UP TO SECONDS.
SUBMIT rsconn01 WITH mode = 'INT'
WITH output = 'X'
AND RETURN.
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