ABAP 发送邮件

DATA: G_AUTOF TYPE CHAR4.
DATA SEND_REQUEST TYPE REF TO CL_BCS.    "CL_BCS 发送邮件主要用到的功能类, 包括创建发送请求, 添加发送内容,添加发送地址, 到最终的发送指令发出.
DATA DOCUMENT     TYPE REF TO CL_DOCUMENT_BCS." 用来放置发送的内容.
DATA G_TEXT TYPE SOLI_TAB.
DATA G_CONTENT TYPE STRING.
DATA MAILTO TYPE AD_SMTPADR.
DATA RECIPIENT TYPE REF TO CL_CAM_ADDRESS_BCS.
DATA L_BCS_EXCEPTION TYPE REF TO CX_DOCUMENT_BCS.
DATA L_SEND_EXCEPTION TYPE REF TO CX_SEND_REQ_BCS.
DATA L_ADDR_EXCEPTION TYPE REF TO CX_ADDRESS_BCS.
DATA: IT_HEX1 TYPE SOLIX_TAB  .
DATA: LV_XML TYPE XSTRING.
DATA:T_MAILHEX TYPE SOLIX_TAB.
DATA:I_RECORD TYPE SOLIX_TAB.
DATA:FILELEN TYPE I.
DATA: G_OVERDAT LIKE SY-DATUM.

DATA: BEGIN OF WA_ATTACH,
  EBELN LIKE EKPO-EBELN,
  EBELP LIKE EKPO-EBELP,
  BANFN LIKE EBAN-BANFN,
  BNFPO LIKE EBAN-BNFPO,
  TXZ01 LIKE ZMMT116-TXZ01,
  LIFNR LIKE LFA1-LIFNR,
  NAME1 LIKE LFA1-NAME1,
  OVERDAT LIKE ZMMT116-OVERDAT,
  CRNAM LIKE ZMMT116-CRNAM,
  ERNAM LIKE EKKO-ERNAM,
  END OF WA_ATTACH.

DATA: GT_ATTACH LIKE WA_ATTACH OCCURS 0 WITH HEADER LINE.

SELECT *
  INTO CORRESPONDING FIELDS OF TABLE GT_ATTACH
FROM ****
  WHERE ************.

TRY.
"  邮件正文
    G_CONTENT = ''.
    APPEND G_CONTENT TO G_TEXT.
    G_CONTENT = 'DEAR SIR:' .
    APPEND G_CONTENT TO G_TEXT.
    G_CONTENT = ''.
    APPEND G_CONTENT TO G_TEXT.
    G_CONTENT = '...........................'.
    APPEND G_CONTENT TO G_TEXT.
    G_CONTENT = ''.
    APPEND G_CONTENT TO G_TEXT.
    APPEND G_CONTENT TO G_TEXT.
    G_CONTENT = 'THANKS.'.
    APPEND G_CONTENT TO G_TEXT.

*    创建发送请求
    SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).

    DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
                                I_TYPE = 'RAW'
                                I_TEXT = G_TEXT
                                I_IMPORTANCE = '1'
                                I_SUBJECT = '************'              "郵件主題
                                ).

*---------- 創建郵件的附件
*----內表作為郵件附件發送 BEGIN
    DATA: TMPSTR TYPE STRING .
    CLEAR: TMPSTR,I_RECORD,I_RECORD[].

    DATA: TAB TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB,
          ENTER(2) TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>CR_LF,
          N TYPE I.
    DATA: BEGIN OF HEADTAB OCCURS 0 ,
            LENGTH    TYPE I ,
            DECIMALS  TYPE I,
            TYPE_KIND TYPE C,
            NAME(30)  TYPE C,
          END OF HEADTAB.
    DATA DESCR_REF TYPE REF TO CL_ABAP_STRUCTDESCR.
    FIELD-SYMBOLS:  TYPE ABAP_COMPDESCR ,
                    ,
                    TYPE ANY .
    DATA:STR TYPE STRING,
         STR2 TYPE STRING ,
         TEXT1 TYPE C.
    DESCR_REF ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( GT_ATTACH ). "導出表頭內容BANFN BNFPO MATNR TXZ01 MEINS EKGRP MENGE等等
    LOOP AT DESCR_REF->COMPONENTS ASSIGNING .
      MOVE-CORRESPONDING  TO HEADTAB.
      APPEND HEADTAB.
    ENDLOOP.
    DESCRIBE TABLE HEADTAB LINES N.
    LOOP AT GT_ATTACH ASSIGNING .
      DO N TIMES.
        ASSIGN COMPONENT SY-INDEX OF STRUCTURE  TO .
        STR = .
        READ TABLE HEADTAB INDEX SY-INDEX.
        IF HEADTAB-TYPE_KIND = 'I' OR HEADTAB-TYPE_KIND = 'P'
                                   OR HEADTAB-TYPE_KIND = 'F'.
          SEARCH STR FOR '-'.
          IF SY-SUBRC = 0 AND SY-FDPOS <> 0.
            SPLIT STR AT '-' INTO STR TEXT1.
            CONDENSE STR.
            CONCATENATE '-' STR INTO STR.
          ELSE.
            CONDENSE STR.
          ENDIF.
        ELSE.
*        SHIFT STR LEFT DELETING LEADING '0' .
        ENDIF.
        CONCATENATE STR2 TAB STR INTO STR2.
      ENDDO.
      SHIFT STR2.
      CONCATENATE TMPSTR STR2 ENTER INTO TMPSTR.
      CLEAR STR2.
    ENDLOOP.

    CONCATENATE '採購單號' TAB
                '採購單項目' TAB
                '請購單號' TAB
                '請購單項目' TAB
                '物料說明' TAB
                '供應商代碼' TAB
                '供應商名稱' TAB
                '完工日期' TAB
                '開單人' TAB
                '需求人' ENTER
                TMPSTR INTO TMPSTR.

    DATA:TMPBUFFER TYPE XSTRING.
    CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
      EXPORTING
        TEXT     = TMPSTR
        MIMETYPE = '"TEXT/HTML; CHARSET=BIG5"'
*       ENCODING = '8400'
      IMPORTING
        BUFFER   = TMPBUFFER
      EXCEPTIONS
        FAILED   = 1
        OTHERS   = 2.
    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        BUFFER          = TMPBUFFER
        APPEND_TO_TABLE = ''
      IMPORTING
        OUTPUT_LENGTH   = FILELEN
      TABLES
        BINARY_TAB      = I_RECORD.
    APPEND LINES OF I_RECORD TO T_MAILHEX.



*----內表作為郵件附件發送 END
    DOCUMENT->ADD_ATTACHMENT(                             "添加附件
      I_ATTACHMENT_TYPE    = 'XLS'                        " 附件格式
      I_ATTACHMENT_SUBJECT = '*****************'         " ATTACHMENT NAME
 "     I_ATTACHMENT_SIZE    = SIZE                         "附件大小
      I_ATT_CONTENT_HEX    =  T_MAILHEX ).

    CALL METHOD SEND_REQUEST->SET_DOCUMENT( DOCUMENT ).
*       CALL METHOD L_SEND_REQUEST->SET_SENDER
*        EXPORTING
*          I_SENDER = L_SENDER.

"邮件地址
      CLEAR :MAILTO.
      MAILTO = '[email protected]'.
      RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( MAILTO ).
    SEND_REQUEST->ADD_RECIPIENT( RECIPIENT ).
    SEND_REQUEST->SET_SEND_IMMEDIATELY( 'X' ).
    CALL METHOD SEND_REQUEST->SEND( ).
    COMMIT WORK.
  CATCH CX_DOCUMENT_BCS INTO L_BCS_EXCEPTION.
  CATCH CX_SEND_REQ_BCS INTO L_SEND_EXCEPTION.
  CATCH CX_ADDRESS_BCS  INTO L_ADDR_EXCEPTION.
ENDTRY.

你可能感兴趣的:(ABAP 发送邮件)