ABAP 发送邮件的两种方式


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 DATA: I_PACKING_LIST LIKE SOPCKLSTI1 OCCURS 0 WITH HEADER LINE, I_RECEIVERS LIKE SOMLRECI1 OCCURS 0 WITH HEADER LINE, "receivers I_CONTENTS_TXT LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE, "ASCII contents of object and attachments I_CONTENTS_HEX LIKE SOLIX OCCURS 0 WITH HEADER LINE, "Binary contents of object and attachments I_CONTENTS_BIN LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE, I_OBJECT_HEADER LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE, "attachments name W_DOC_DATA LIKE SODOCCHGI1. DATA: C_MIMETYPE TYPE CHAR64 VALUE 'APPLICATION/MSEXCEL;charset=utf-16le'. DATA: V_XATTACH TYPE XSTRING. DATA: LV_STRING TYPE STRING. DATA: TAB_LINES LIKE SY-TABIX, W_SENT_ALL(1) TYPE C, W_ERROR TYPE SY-SUBRC. DATA: I_SPFLI LIKE STANDARD TABLE OF SPFLI WITH HEADER LINE. *********************************************************************** START-OF-SELECTION. PERFORM GET_SPFLI_DATA. ***define mail main context**** PERFORM DEFINE_MAIL_HEADER. ***dfine attachment******************* PERFORM DEFINE_MAIL_ATTACHMENT. ***get mail receiver***************** PERFORM GET_RECEIVERS. *Send email message, although is not sent from SAP until mail send *program has been executed(rsconn01) PERFORM SEND_EMAIL_MESSAGE. *&---------------------------------------------------------------------* *& Form GET_SPFLI_DATA *&---------------------------------------------------------------------* FORM GET_SPFLI_DATA . SELECT * FROM SPFLI INTO CORRESPONDING FIELDS OF TABLE I_SPFLI. ENDFORM. " GET_SPFLI_DATA *&---------------------------------------------------------------------* *& Form DEFINE_MAIL_HEADER *&---------------------------------------------------------------------* FORM DEFINE_MAIL_HEADER . DATA: TMP_DATE LIKE SY-DATUM. DATA: TMP_CHAR(30). TMP_DATE = SY-DATUM. CONCATENATE 'Overdue Batch ' SY-DATUM INTO TMP_CHAR. ********define subject and priority************ W_DOC_DATA-OBJ_DESCR = 'Hello'. "Email Subject. W_DOC_DATA-PRIORITY = 1. "mail PRIORITY¡A 1 is highest W_DOC_DATA-OBJ_LANGU = SY-LANGU. W_DOC_DATA-OBJ_NAME = 'OFFER'. "Name of document.. W_DOC_DATA-SENSITIVTY = 'O'. "Document sensitivity. ********mail main context*********************** I_CONTENTS_TXT = 'Dear Mr and Miss:'. APPEND I_CONTENTS_TXT. I_CONTENTS_TXT = ' '. APPEND I_CONTENTS_TXT. I_CONTENTS_TXT = ' Please check attachment in time!'. APPEND I_CONTENTS_TXT. I_CONTENTS_TXT = ' This Email is sent by systme, Do not Return the Email.'. APPEND I_CONTENTS_TXT. I_CONTENTS_TXT =' If you have any problem, please contact with ERP.'. APPEND I_CONTENTS_TXT. I_CONTENTS_TXT = ' '. APPEND I_CONTENTS_TXT. I_CONTENTS_TXT = TMP_DATE. APPEND I_CONTENTS_TXT. DESCRIBE TABLE I_CONTENTS_TXT LINES TAB_LINES. READ TABLE I_CONTENTS_TXT INDEX TAB_LINES. W_DOC_DATA-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( I_CONTENTS_TXT ). ********count lines by man context************* CLEAR I_PACKING_LIST-TRANSF_BIN. I_PACKING_LIST-TRANSF_BIN = SPACE. "ASCII format/binary format I_PACKING_LIST-HEAD_START = 1. I_PACKING_LIST-HEAD_NUM = 0. I_PACKING_LIST-BODY_START = 1. I_PACKING_LIST-BODY_NUM = TAB_LINES. I_PACKING_LIST-DOC_TYPE = 'RAW'. APPEND I_PACKING_LIST. ENDFORM. " DEFINE_MAIL_HEADER *&---------------------------------------------------------------------* *& Form DEFINE_MAIL_ATTACHMENT *&---------------------------------------------------------------------* FORM DEFINE_MAIL_ATTACHMENT . LOOP AT I_SPFLI. CONCATENATE LV_STRING I_SPFLI-CARRID GC_TAB I_SPFLI-CONNID GC_TAB I_SPFLI-COUNTRYFR GC_TAB I_SPFLI-CITYFROM GC_TAB I_SPFLI-CITYFROM GC_TAB I_SPFLI-COUNTRYTO GC_TAB I_SPFLI-CITYTO GC_TAB I_SPFLI-AIRPTO GC_CRLF INTO LV_STRING. ENDLOOP. * Convert string to xstring type * 'APPLICATION/MSEXCEL;charset=utf-16le' CALL FUNCTION 'SCMS_STRING_TO_XSTRING' EXPORTING TEXT = LV_STRING MIMETYPE = C_MIMETYPE IMPORTING BUFFER = V_XATTACH EXCEPTIONS FAILED = 1 OTHERS = 2. * Add the file header for utf-16le. . IF SY-SUBRC = 0. CONCATENATE CL_ABAP_CHAR_UTILITIES=>BYTE_ORDER_MARK_LITTLE V_XATTACH INTO V_XATTACH IN BYTE MODE. ENDIF. CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' EXPORTING BUFFER = V_XATTACH TABLES BINARY_TAB = I_CONTENTS_HEX. ***set attachment name, count size******************* DESCRIBE TABLE I_SPFLI LINES TAB_LINES. TAB_LINES = TAB_LINES . I_PACKING_LIST-TRANSF_BIN = 'X'. I_PACKING_LIST-HEAD_START = 1. I_PACKING_LIST-HEAD_NUM = 0. I_PACKING_LIST-BODY_START = 1. I_PACKING_LIST-BODY_NUM = TAB_LINES. I_PACKING_LIST-DOC_TYPE = 'XLS'. I_PACKING_LIST-OBJ_NAME = 'ATTACHMENT'. I_PACKING_LIST-OBJ_DESCR = 'spfli'. I_PACKING_LIST-DOC_SIZE = TAB_LINES * 255. APPEND I_PACKING_LIST. ENDFORM. " DEFINE_MAIL_ATTACHMENT *&---------------------------------------------------------------------* *& Form GET_RECEIVERS *&---------------------------------------------------------------------* FORM GET_RECEIVERS . * Add the recipients email address CLEAR I_RECEIVERS. REFRESH I_RECEIVERS. * SELECT * FROM zszmail. I_RECEIVERS-RECEIVER = '461356592@qq.com'."Email Address I_RECEIVERS-REC_TYPE = 'U'. "Internet address I_RECEIVERS-NOTIF_READ = 'X'. I_RECEIVERS-COM_TYPE = 'INT'. I_RECEIVERS-NOTIF_DEL = 'X'. I_RECEIVERS-NOTIF_NDEL = 'X'. APPEND I_RECEIVERS. * ENDSELECT. ENDFORM. " GET_RECEIVERS *&---------------------------------------------------------------------* *& Form SEND_EMAIL_MESSAGE *&---------------------------------------------------------------------* FORM SEND_EMAIL_MESSAGE. DATA: W_SUBRC LIKE SY-SUBRC. * Call the FM to post the message to SAPMAIL CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1' EXPORTING DOCUMENT_DATA = W_DOC_DATA "Attributes of new document PUT_IN_OUTBOX = 'X' "Flag: Move document to outbox after send COMMIT_WORK = 'X' "An explicit COMMIT WORK is to be set IMPORTING SENT_TO_ALL = W_SENT_ALL TABLES PACKING_LIST = I_PACKING_LIST "Information about structure of data tables OBJECT_HEADER = I_OBJECT_HEADER CONTENTS_HEX = I_CONTENTS_HEX CONTENTS_TXT = I_CONTENTS_TXT "ASCII contents of object and attachments RECEIVERS = I_RECEIVERS "Document recipients with send attributes 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. W_SUBRC = SY-SUBRC. WAIT UP TO 2 SECONDS. IF W_SUBRC EQ 0. SUBMIT RSCONN01 WITH MODE = 'INT' WITH OUTPUT = 'X' AND RETURN. ENDIF. ENDFORM. " SEND_EMAIL_MESSAGE

2.00

 DATA: WA_ZBCFILE TYPE ZBCFILE.
 DATA: IT_ZBCFILE LIKE TABLE OF WA_ZBCFILE.

 DATA: BEGIN OF IT_MAILCONTENT OCCURS 0,
   PLANT LIKE ZBCFILE-PLANT,
   LOCATION LIKE ZBCFILE-LOCATION,
   END OF IT_MAILCONTENT.

 DATA: WA_MAILCONTENT LIKE IT_MAILCONTENT.

 DATA: SEND_REQUEST TYPE REF TO CL_BCS,
       DOCUMENT TYPE REF TO CL_DOCUMENT_BCS,
       FAIL TYPE REF TO CX_BCS,
       RECIPIENT TYPE REF TO IF_RECIPIENT_BCS.

 DATA: LS TYPE STRING,
        L_INDEX TYPE I,
        L_CONTENT TYPE STRING,
        MAILTO TYPE AD_SMTPADR,
        MAIN_TEXT TYPE BCSY_TEXT,
        TITLE TYPE SO_OBJ_DES,
        MAIL_SENDER_LR TYPE REF TO IF_SENDER_BCS,
        P_SENDER LIKE ADR6-SMTP_ADDR.


 SELECT * FROM ZBCFILE
   INTO CORRESPONDING FIELDS OF TABLE IT_ZBCFILE.

 LOOP AT IT_ZBCFILE INTO WA_ZBCFILE.
   WA_MAILCONTENT-PLANT = WA_ZBCFILE-PLANT.
   WA_MAILCONTENT-LOCATION = WA_ZBCFILE-LOCATION.
   APPEND WA_MAILCONTENT TO IT_MAILCONTENT.
 ENDLOOP.

 IF NOT IT_MAILCONTENT[] IS INITIAL.
   SORT IT_MAILCONTENT BY PLANT LOCATION.
   DELETE ADJACENT DUPLICATES FROM IT_MAILCONTENT COMPARING PLANT LOCATION.

   CLEAR: WA_MAILCONTENT.
   L_CONTENT = 'please maintain area mapping at TCODE¡GZPPE91¡Asome B\C FILE failed.'.
   APPEND L_CONTENT TO MAIN_TEXT.
   L_CONTENT = 'Plant  Location'.
   APPEND L_CONTENT TO MAIN_TEXT.
   LOOP AT IT_MAILCONTENT INTO WA_MAILCONTENT.
     CLEAR L_CONTENT.
     CONCATENATE WA_MAILCONTENT-PLANT WA_MAILCONTENT-LOCATION INTO L_CONTENT SEPARATED BY '  '.
     APPEND L_CONTENT TO MAIN_TEXT.
   ENDLOOP.
   TITLE = 'B\C FILE failed.'.
   CONCATENATE SY-SYSID SY-TCODE TITLE INTO TITLE SEPARATED BY ''.
   MAILTO = '461356592@qq.com'.
   P_SENDER = 'sapadmin@xxxx.com'.

   TRY.


*   step1: create request


      SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).



*   step2: create mail content


      DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
       I_TYPE = 'RAW'
       I_TEXT = MAIN_TEXT
       I_SUBJECT = TITLE ).



*   step3: add mail content


      SEND_REQUEST->SET_DOCUMENT( DOCUMENT ).



*  step4: mail send receive address


      MAIL_SENDER_LR = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( P_SENDER ).
       SEND_REQUEST->SET_SENDER( I_SENDER = MAIL_SENDER_LR ).
       RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( MAILTO ).



*  step5: add address to request


      SEND_REQUEST->ADD_RECIPIENT( RECIPIENT ).



*   step6: commit request


      SEND_REQUEST->SEND( I_WITH_ERROR_SCREEN = 'X' ).

      COMMIT WORK AND WAIT.
     CATCH CX_BCS INTO FAIL.
   ENDTRY.
 ENDIF.

 

posted @ 2019-10-16 13:12  真的是很难  阅读(3992)  评论(0编辑  收藏  举报