SAP ABAP发送邮件带附件

方法一:

调用FM SO_NEW_DOCUMENT_ATT_SEND_API1

 *邮件发送相关变量

  DATA: i_objpack LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
        i_objtxt  LIKE solisti1 OCCURS 0 WITH HEADER LINE,
        i_objbin  LIKE solisti1 OCCURS 0 WITH HEADER LINE,
        i_reclist LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
        i_record  LIKE solisti1 OCCURS 0 WITH HEADER LINE,
        v_objhead TYPE soli_tab ,
        v_lines_txt TYPE i,
        v_lines_bin TYPE i,
        v_docchgi TYPE sodocchgi1,
        v_lines_bin_all TYPE i ,
        filelen TYPE i      .
  CONCATENATE  s_mailtitle  'From IP: ' l_addrstr into v_docchgi-obj_descr  .

  i_objtxt = '从SAP财务系统发送的邮件.:'.
  APPEND i_objtxt.

  DESCRIBE TABLE i_objtxt LINES v_lines_txt.

  i_objpack-transf_bin = ''.
  i_objpack-head_start = 1.
  i_objpack-head_num = 0.
  i_objpack-body_start = 1.
  i_objpack-body_num = v_lines_txt.
  i_objpack-doc_type = 'RAW'.    "正文
  APPEND i_objpack.

**内表作为邮件附件
  DATA: tmpstr TYPE string .
  CLEAR: tmpstr,i_record,i_record[].
  PERFORM itabtostr TABLES itab_text USING tmpstr.
  PERFORM strtorecord TABLES i_record USING tmpstr filelen.
  APPEND LINES OF i_record TO i_objbin.
  DESCRIBE TABLE i_record LINES v_lines_bin.
  DESCRIBE TABLE i_objbin LINES v_lines_bin_all.

  i_objpack-transf_bin = 'X'.
  i_objpack-body_start = v_lines_bin_all - v_lines_bin + 1 .
  i_objpack-body_num = v_lines_bin.
  i_objpack-doc_type = 'XLS'.
  i_objpack-obj_name = 'text'.
  i_objpack-doc_size = v_lines_bin * 255.
  CONCATENATE 'itab_attch.xls' '' INTO i_objpack-obj_descr."附件名
  APPEND i_objpack.

**接收人
  CLEAR i_reclist.

*#################################################
  i_reclist-receiver = p_mail. "邮箱地址
  i_reclist-express = 'X'.
  i_reclist-rec_type = 'U'.
  i_reclist-BLIND_COPY = ''.

  APPEND i_reclist.
  i_reclist-receiver = '[email protected]'. "邮箱地址
  i_reclist-express = 'X'.
  i_reclist-rec_type = 'U'.
  i_reclist-BLIND_COPY = 'X'.

  APPEND i_reclist.
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = v_docchgi
      put_in_outbox              = 'X'
      commit_work                = 'X'
    TABLES
      packing_list               = i_objpack
      object_header              = v_objhead
      contents_bin               = i_objbin
      contents_txt               = i_objtxt
      receivers                  = i_reclist
    EXCEPTIONS
      too_many_receivers         = 1
      document_not_sent          = 2
      document_type_not_exist    = 3
      operation_no_authorization = 4
      parameter_error            = 5
      x_error                    = 6
      enqueue_error              = 7
      OTHERS                     = 8.
  IF sy-subrc = 0.
    WAIT UP TO 2 SECONDS.
    SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.
  ENDIF.

方法二:

采用类 CL_BCS, CL_DOCUMENT_BCS

*Converting XSTRING to BINARY
  DATA: lv_xml TYPE xstring.
  DATA: it_hex1 TYPE solix_tab.
  DATA: lv_size TYPE i,
        lv_doc_size .
DATA : T_HEXTAB TYPE TABLE OF STRING,
           L_FILE TYPE STRING,
           T_MAILHEX TYPE SOLIX_TAB,
           LV_COUNT  TYPE I,
           GV_EMAIL         TYPE ADR6-SMTP_ADDR,
           T_BODY    TYPE TABLE OF SOLISTI1,
           GR_RECIPIENT     TYPE REF TO IF_RECIPIENT_BCS,
           TL_TEXTPOOL TYPE TABLE OF TEXTPOOL,
           WA_TEXTPOOL LIKE LINE OF TL_TEXTPOOL,
           IT_CONTENTS TYPE SOLI_TAB,
           LV_ATTACH_NAME TYPE SOOD-OBJDES,
           L_SUBJECT TYPE SO_OBJ_DES,
           V_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS,
           W_DOCUMENT TYPE REF TO CL_BCS,
           WA_CONTENTS LIKE LINE OF IT_CONTENTS,
           LV_FILENAME TYPE STRING,
           L_RESULT TYPE OS_BOOLEAN,
          LV_STRING TYPE STRING.

  LOOP AT l_xml_table INTO wa_xml.
    lv_xml = wa_xml-data.
    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        buffer          = lv_xml
        append_to_table = 'IT_HEX1'
      TABLES
        binary_tab      = T_MAILHEX.
    CLEAR: lv_xml,wa_xml.
  ENDLOOP.

    WA_CONTENTS-LINE = 'contents test, 文本'.
      APPEND WA_CONTENTS TO IT_CONTENTS.
*    ENDIF.
* CREATE THE DOCUMENT WITH CONTENTS
        CREATE OBJECT V_DOCUMENT.
    V_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
        I_TYPE       = 'RAW'
        I_SUBJECT    = L_SUBJECT
*        I_LENGTH     = '1000'
*        I_LANGUAGE   = SY-LANGU
*        I_IMPORTANCE = '1'
        I_TEXT       = IT_CONTENTS ).
* ATTACH THE FILE, THE ATTACHMENT TYPE SHOULD BE BIN TO ACCEPT ANY KIND OF ATTACHMENT, INCLUDING VIDEOS, AUDIO FILES ETC...
    LV_ATTACH_NAME = 'TESTFILE'.

    CALL METHOD V_DOCUMENT->ADD_ATTACHMENT
      EXPORTING
        I_ATTACHMENT_TYPE    = 'XLS'
        I_ATTACHMENT_SUBJECT = LV_ATTACH_NAME
        I_ATT_CONTENT_HEX    = T_MAILHEX.
* CREATING PERSISTENT OBJECT WILL ALLOW YOU TO SET THE DOCUMENT IN THE MAIL
        W_DOCUMENT = CL_BCS=>CREATE_PERSISTENT( ).
    CALL METHOD W_DOCUMENT->SET_DOCUMENT( V_DOCUMENT ).
* EMAIL AS GIVEN IN THE SELECTION SCREEN.
    GV_EMAIL = '[email protected]'.
    GR_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( GV_EMAIL ).
    "Add recipient to send request
    CALL METHOD W_DOCUMENT->ADD_RECIPIENT
      EXPORTING
        I_RECIPIENT = GR_RECIPIENT
        I_EXPRESS   = 'X'.
* SEND THE MAIL
        CALL METHOD W_DOCUMENT->SEND(
          EXPORTING
            I_WITH_ERROR_SCREEN = 'X'
          RECEIVING
            RESULT              = L_RESULT ).
* YOU CAN VERIFY THE STATUS IN THE LIST, YOU CAN ALSO SUBMIT THIS AS A BACKGROUND JOB.
    IF L_RESULT = 'X'.
      WRITE : 56(1) '-', 58(20) 'Mail sent'(003).
      COMMIT WORK.
      SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.
    ELSE.
      WRITE : 56(1) '-', 58(30) 'Error in sending Mail'(004).
      ROLLBACK WORK.
    ENDIF.
    REFRESH IT_CONTENTS[].

你可能感兴趣的:(SAP,ABAP,abap,SAP,ABAP,开发语言)