ABAP-邮件发送


*&---------------------------------------------------------------------*
*& 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

优质内容筛选与推荐>>
1、Linux 2.6 字符设备驱动程序
2、Java大全-吐血整理
3、PHP 函数proc_open笔记
4、MEK6318接线方法
5、Java解决栈溢出


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号