【ABAP】发送外部邮件代码1

通过类的方式实现邮件发送

*&---------------------------------------------------------------------*
*& Report YTEST7
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ytest7.

PARAMETERS:p_file TYPE string.

*----------------------------------------------------------------------*
*AT SELECTION-SCREEN.
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM frm_get_file_name.

*----------------------------------------------------------------------*
*START-OF-SELECTION.
*----------------------------------------------------------------------*
START-OF-SELECTION.
  DATA:lr_send_request   TYPE REF TO cl_bcs,
       lr_document       TYPE REF TO cl_document_bcs,
       lr_sender         TYPE REF TO if_sender_bcs,
       lr_recipient      TYPE REF TO if_recipient_bcs,
       lr_bcs_exception  TYPE REF TO cx_root,
       lr_send_exception TYPE REF TO cx_root,
       lr_addr_exception TYPE REF TO cx_root.
  DATA:lt_contents TYPE TABLE OF soli,
       lt_hex      TYPE TABLE OF solix,
       lv_subject  TYPE so_obj_des,
       lv_length   TYPE so_obj_len,
       lv_email    TYPE adr6-smtp_addr.
  DATA:lv_error TYPE string.

  DATA:lv_filelength TYPE i.

  "读取附件
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                = p_file
      filetype                = 'BIN'
    IMPORTING
      filelength              = lv_filelength
    CHANGING
      data_tab                = lt_hex
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      unknown_dp_error        = 12
      access_denied           = 13
      OTHERS                  = 17.


  CLEAR:lv_length.
  lv_length = lv_filelength.

  REFRESH:lt_contents.
  lt_contents = VALUE #( ( line = '您好:' )
                         ( line = '' )
                         ( line = '请及时查看通知单' ) ).

  TRY.
      "创建发送请求
      lr_send_request = cl_bcs=>create_persistent( ).

      CLEAR:lv_subject.
      lv_subject = '通知单编号'.

      "设定发送内容
      lr_document = cl_document_bcs=>create_document( i_type       = 'RAW'
                                                      i_subject    = lv_subject
                                                      i_length     = lv_length
                                                      i_importance = '1'
                                                      i_text       = lt_contents[] ).

      CLEAR:lv_subject.
      lv_subject = '附件1.xls'.

      "添加附件(可添加多个)
      CALL METHOD lr_document->add_attachment
        EXPORTING
          i_attachment_type    = 'BIN'
          i_attachment_subject = lv_subject
          i_attachment_size    = lv_length
          i_att_content_hex    = lt_hex.

      "增加发送内容到发送请求
      CALL METHOD lr_send_request->set_document( lr_document ).

      "取得发件人(前提是这个邮箱地址能发邮件,并且不需要密码)
      lr_sender = cl_sapuser_bcs=>create( 'MAILUSER' ).

      CALL METHOD lr_send_request->set_sender
        EXPORTING
          i_sender = lr_sender.

      "设置收件人
      CLEAR:lv_email.
      lv_email = '123@163.com'.
      TRANSLATE lv_email TO LOWER CASE.

      CLEAR:lr_recipient.
      lr_recipient = cl_cam_address_bcs=>create_internet_address( lv_email ).

      CALL METHOD lr_send_request->add_recipient
        EXPORTING
          i_recipient  = lr_recipient
          i_express    = 'X'
          i_copy       = ' '
          i_blind_copy = ' '
          i_no_forward = ' '.

      "设置抄送人
      CLEAR:lv_email.
      lv_email = '123@163.com'.
      TRANSLATE lv_email TO LOWER CASE.

      CLEAR:lr_recipient.
      lr_recipient = cl_cam_address_bcs=>create_internet_address( lv_email ).

      CALL METHOD lr_send_request->add_recipient
        EXPORTING
          i_recipient  = lr_recipient
          i_express    = 'X'
          i_copy       = 'X'
          i_blind_copy = ' '
          i_no_forward = ' '.

      "立即发送
      lr_send_request->set_send_immediately( 'X' ).

      "与outbox关联
      lr_send_request->send_request->set_link_to_outbox( 'X' ).

      "发送
      CALL METHOD lr_send_request->send( 'X' ).

      COMMIT WORK AND WAIT.

      CLEAR:lv_error.
    CATCH cx_document_bcs INTO lr_bcs_exception.
      lv_error = lr_bcs_exception->get_text( ).

    CATCH cx_send_req_bcs INTO lr_send_exception.
      lv_error = lr_send_exception->get_text( ).

    CATCH cx_address_bcs  INTO lr_addr_exception.
      lv_error = lr_addr_exception->get_text( ).
  ENDTRY.
*&---------------------------------------------------------------------*
*& Form frm_get_file_name
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_file_name .
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      mode             = 'O'
      title            = 'OPEN FILE'
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.
ENDFORM.

 

posted @ 2021-09-06 15:42  littlefoam  阅读(273)  评论(0)    收藏  举报