IT干货网

ABAP-邮件发送

leader 2022年03月09日 SAP 150 0
*&---------------------------------------------------------------------* 
*& Report  ZRICO_TEST4 
*& 
*&---------------------------------------------------------------------* 
*& 
*& 
*&---------------------------------------------------------------------* 
report zrico_test4. 
 
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 default 'chenruike@geely.com'. " 收件人 
 
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 '测试!' 
              gc_crlf gc_crlf 
              into lv_string. 
 
  do 10 times. 
    concatenate lv_string 
                '1111111111111111111111111111111111111111111111111111111111111' gc_tab 
                '2222222222222222222222222222222222222222222222222222222222222' gc_tab 
                '3333333333333333333333333333333333333333333333333333333333333' 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 = '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 
 
*&---------------------------------------------------------------------* 
*&      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 ricochen' ).      " 邮件主题名 
 
*     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 ). 
 
*      recipient = cl_cam_address_bcs=>create_internet_address( 'congh@geely.com' ). 
*      send_request->add_recipient( recipient ). 
 
*     ---------- send document --------------------------------------- 
      sent_to_all = send_request->send( i_with_error_screen = 'X' ). 
 
      commit work. 
      wait up to 2 seconds. 
      submit rsconn01 with mode = 'INT' 
                    with output = '' 
                    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

评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

ABAP-JSON数据格式互转