首页 > 代码库 > ABAP发邮件函数

ABAP发邮件函数

步骤:

  • 一、检查输入参数,

(1)未指定文件類別代碼,
(2)未指定郵件主題,

(3)未指定郵件內容,

(4)未指定發送人郵件地址,

(5)未指定接收人郵件地址,

 

  • 二、调用发送功能,

(1)创建发送请求

(2)创建整理发送内容,

增加附件內容
(3)添加邮件内容到发送请求

(4)設置發送人出件地址

(5)接收者邮件地址转换

(6)第六步,設置狀態屬性

(7)第七步: 正式发送并提交作业

?如何确认是否发送成功,

1.是否可以请求发送接收回执?

2.是否可以返回成功代码?

 

ABAP发邮件函数:

技术分享

技术分享

技术分享

 表:

T_RECIPIENT LIKE UPS_YS_HR_CPERSON 核心人物的明细
RETURN          LIKE BAPIRET2 返回参数

 

FUNCTION ZINT_SEND_EMAIL.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     REFERENCE(KTYPE) TYPE  SO_OBJ_TP DEFAULT ‘RAW‘
*"     REFERENCE(SUBJECT) TYPE  SO_OBJ_DES
*"     REFERENCE(TEXT) TYPE  SOLI_TAB
*"     REFERENCE(SENDER_ADDRESS) TYPE  AD_SMTPADR
*"     VALUE(SENDER_NAME) TYPE  AD_SMTPADR OPTIONAL
*"     VALUE(ATTACHMENTS) TYPE  RMPS_T_POST_CONTENT OPTIONAL
*"     VALUE(IMMEDIATELY) TYPE  BOOLEAN OPTIONAL
*"  EXPORTING
*"     REFERENCE(E_RESULT) TYPE  BOOLEAN
*"  TABLES
*"      T_RECIPIENT STRUCTURE  UPS_YS_HR_CPERSON
*"      RETURN STRUCTURE  BAPIRET2
*"----------------------------------------------------------------------


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

  DATA:W_RETURN            LIKE BAPIRET2,
       W_RENAME            TYPE AD_SMTPADR,
       W_ATTACHMENT        LIKE LINE OF ATTACHMENTS,
       W_ATTACHMENT_SUBJECT TYPE SOOD-OBJDES.

*BREAK CP900.
  REFRESH RETURN.
  CLEAR E_RESULT.
  E_RESULT = ‘X‘.
  IF KTYPE IS INITIAL.
    W_RETURN-TYPE = ‘E‘.
    W_RETURN-MESSAGE = ‘未指定文件類別代碼‘.
    W_RETURN-PARAMETER = ‘KTYPE‘.
    APPEND W_RETURN TO RETURN.
    CLEAR E_RESULT.
  ENDIF.

  IF SUBJECT IS INITIAL.
    W_RETURN-TYPE = ‘E‘.
    W_RETURN-MESSAGE = ‘未指定郵件主題‘.
    W_RETURN-PARAMETER = ‘SUBJECT‘.
    APPEND W_RETURN TO RETURN.
    CLEAR E_RESULT.
  ENDIF.

  IF TEXT[] IS INITIAL.
    W_RETURN-TYPE = ‘E‘.
    W_RETURN-MESSAGE = ‘未指定郵件內容‘.
    W_RETURN-PARAMETER = ‘TEXT‘.
    APPEND W_RETURN TO RETURN.
    CLEAR E_RESULT.
  ENDIF.

  IF SENDER_ADDRESS IS INITIAL.
    W_RETURN-TYPE = ‘E‘.
    W_RETURN-MESSAGE = ‘未指定發送人郵件地址‘.
    W_RETURN-PARAMETER = ‘SENDER_ADDRESS‘.
    APPEND W_RETURN TO RETURN.
    CLEAR E_RESULT.
  ENDIF.
  IF T_RECIPIENT[] IS INITIAL.
    W_RETURN-TYPE = ‘E‘.
    W_RETURN-MESSAGE = ‘未指定接收人郵件地址‘.
    W_RETURN-PARAMETER = ‘RECIPIENT‘.
    APPEND W_RETURN TO RETURN.
    CLEAR E_RESULT.
  ENDIF.

  CLEAR W_RETURN.

  CHECK E_RESULT IS NOT INITIAL.

  TRY.
*&调用
*try.
*   第一步: 创建发送请求
      SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).

*   第二步: 创建整理发送内容
      DOCUMENT     = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
                       I_TYPE          = KTYPE
                       I_TEXT          = TEXT
                       I_SUBJECT       = SUBJECT ).
*    增加附件內容
      LOOP AT ATTACHMENTS INTO W_ATTACHMENT.
        W_ATTACHMENT_SUBJECT = W_ATTACHMENT-SUBJECT.
        DOCUMENT->ADD_ATTACHMENT(
          EXPORTING
            I_ATTACHMENT_TYPE     = W_ATTACHMENT-OBJTP
            I_ATTACHMENT_SUBJECT  = W_ATTACHMENT_SUBJECT
            I_ATT_CONTENT_HEX     = W_ATTACHMENT-CONT_HEX ).
      ENDLOOP.
*   第三步: 添加邮件内容到发送请求
      SEND_REQUEST->SET_DOCUMENT( DOCUMENT ).

*   第四步:設置發送人出件地址
      SENDER_ID    = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
                      I_ADDRESS_STRING = SENDER_ADDRESS
                      I_ADDRESS_NAME   = SENDER_NAME ).
      SEND_REQUEST->SET_SENDER( SENDER_ID ).

*   第五步: 接收者邮件地址转换
      LOOP AT T_RECIPIENT WHERE CPERSON NE ‘99999999‘.
        W_RENAME = T_RECIPIENT-EMNAM.
        RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
                     I_ADDRESS_STRING  = T_RECIPIENT-E_MAIL
                     I_ADDRESS_NAME    = W_RENAME ).
        SEND_REQUEST->ADD_RECIPIENT( RECIPIENT ).
      ENDLOOP.

      LOOP AT T_RECIPIENT WHERE CPERSON EQ ‘99999999‘.
        W_RENAME = T_RECIPIENT-EMNAM.
        RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
                     I_ADDRESS_STRING  = T_RECIPIENT-E_MAIL
                     I_ADDRESS_NAME    = W_RENAME ).
*      SEND_REQUEST->ADD_RECIPIENT( RECIPIENT ).
        CALL METHOD SEND_REQUEST->ADD_RECIPIENT
          EXPORTING
            I_RECIPIENT = RECIPIENT
            I_COPY      = ‘X‘.
      ENDLOOP.

*    第六步,設置狀態屬性
      SEND_REQUEST->SET_STATUS_ATTRIBUTES(
                      I_REQUESTED_STATUS = ‘E‘
                      I_STATUS_MAIL      = ‘E‘ ).
      SEND_REQUEST->SET_SEND_IMMEDIATELY( IMMEDIATELY ).

*   第七步: 正式发送并提交作业
      SEND_REQUEST->SEND( I_WITH_ERROR_SCREEN = ‘X‘ ).
      COMMIT WORK AND WAIT.
    CATCH CX_BCS INTO FAIL.
      E_RESULT = ‘‘.
      W_RETURN-TYPE    = ‘E‘.
      W_RETURN-MESSAGE = FAIL->GET_TEXT( ).
      APPEND W_RETURN TO RETURN.
      CLEAR W_RETURN.
  ENDTRY.


ENDFUNCTION.

  

 

ABAP发邮件+Excel 附件

*&---------------------------------------------------------------------*
*& Report  ZNMMAVW_STOCK_REPORT_CFG                                    *
*& Author: Accenture Developer                                         *
*& Date:   20/10/2016                                                  *
*& Change log:                                                         *
*&---------------------------------------------------------------------*

REPORT  znmmratj_stock_report .

*&------------------------------------------------------------------*
*&              SELF TABLES/STRUCTURES Declaration                  *
*&------------------------------------------------------------------*
TYPE-POOLS: slis.
TABLES: bkpf,bseg,mara,mard,ztatj_stk_loc,ztatj_stk_email.
*&------------------------------------------------------------------*
*&           Internal Table  Definition                             *
*&------------------------------------------------------------------*
TYPES: BEGIN OF ty_out,
        werks LIKE mard-werks,     "Plant
        bismt LIKE mara-bismt,     "Old material number
        matnr LIKE mara-matnr,     "Material Number
        lgort LIKE mard-lgort,     "Storage Location
        lgobe LIKE t001l-lgobe,    "Description of Storage Location
        zscsl LIKE mard-labst,     "生产报工数量
        zjssl LIKE mard-labst,     "仓库接收数量
        zkcsl LIKE mard-labst,     "总库存
        meins LIKE mara-meins,     "Base Unit of Measure
        sel(1),
       END OF ty_out.

TYPES: BEGIN OF ty_attac,
        bismt TYPE char18,         "Old material number
        matnr TYPE char18,         "Material Number
        werks TYPE char4,          "Plant
        lgort TYPE char8,          "Storage Location
        lgobe TYPE char16,         "Description of Storage Location
        zscsl TYPE char20,         "生产报工数量
        zjssl TYPE char20,         "仓库接收数量
        zkcsl TYPE char20,         "总库存
        meins TYPE char4,         "Base Unit of Measure
       END OF ty_attac.


TYPES: BEGIN OF ty_mseg,
        mblnr LIKE mseg-mblnr,     "Number of Material Document
        matnr LIKE mseg-matnr,     "Material Number
        werks LIKE mseg-werks,     "Plant
        bwart LIKE mseg-bwart,     "Movement Type
        shkzg LIKE mseg-shkzg,     "Debit/Credit Indicator
        erfmg LIKE mseg-erfmg,     "Quantity in Unit of Entry
        lgort LIKE mseg-lgort,     "Storage Location
        END OF ty_mseg.

TYPES: BEGIN OF ty_mara,
        matnr LIKE mara-matnr,     "Material Number
        bismt LIKE mara-bismt,     "Old material number
        meins LIKE mara-meins,     "Base Unit of Measure
       END OF ty_mara.

TYPES: BEGIN OF ty_mard,
        matnr LIKE mard-matnr,     "Material Number
        werks LIKE mard-werks,     "Plant
        lgort LIKE mard-lgort,     "Storage Location
        labst LIKE mard-labst,     "Valuated Unrestricted-Use Stock
       END OF ty_mard.
*&------------------------------------------------------------------*
*&           Global Variants Definition                             *
*&------------------------------------------------------------------*
DATA: gt_out   TYPE TABLE OF ty_out,
      gt_mara  TYPE TABLE OF ty_mara,
      gt_mseg  TYPE TABLE OF ty_mseg,
      gt_mard  TYPE TABLE OF ty_mard,
      gt_stkl  TYPE TABLE OF ztatj_stk_loc,
      gt_email TYPE TABLE OF ztatj_stk_email,
      gt_t001l TYPE TABLE OF t001l,
      gt_attac TYPE TABLE OF ty_attac WITH HEADER LINE,
      gw_out   TYPE ty_out,
      gw_mara  TYPE ty_mara,
      gw_mseg  TYPE ty_mseg,
      gw_mard  TYPE ty_mard,
      gw_stkl  TYPE ztatj_stk_loc,
      gw_email TYPE ztatj_stk_email,
      gw_t001l TYPE t001l.

RANGES: r_bsl  FOR  mard-lgort, "Backflush Storage location
        r_wsl  FOR  mard-lgort, "Warehouse Storage location
        r_sl   FOR  mard-lgort. "Storage location

DATA: gv_err TYPE c.  "错误标识
DATA: gv_message_text_output(255) TYPE c,
      gt_ztmmatj_prmillog TYPE TABLE OF ztmmatj_prmillog
       WITH HEADER LINE,
       gv_new_object_id  TYPE sofolenti1-object_id .
"alv相关定义
DATA: gt_fieldcat TYPE TABLE OF slis_fieldcat_alv,
      gw_layout   TYPE slis_layout_alv,
      gt_event    TYPE slis_t_event,
      gw_fieldcat LIKE LINE OF gt_fieldcat.
*&------------------------------------------------------------------*
*&           Select Screen                                          *
*&------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-t01.
SELECT-OPTIONS: s_matnr FOR mara-matnr,
                s_werks FOR mard-werks.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN POSITION 1.
PARAMETERS: p_rb_01 as CHECKBOX DEFAULT ‘‘ USER-COMMAND
rb01.
SELECTION-SCREEN COMMENT 5(15)  text-t03.

*SELECTION-SCREEN POSITION 25.
*PARAMETERS:     p_rb_02 RADIOBUTTON GROUP gr1.
*SELECTION-SCREEN COMMENT 30(15)   text-t03.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK b1.

*&------------------------------------------------------------------*
*&              START-OF-SELECTION                                  *
*&------------------------------------------------------------------*
START-OF-SELECTION.

*****&step1.获取数据到内表
  PERFORM frm_get_data.

*****&step2.数据处理
  CHECK gv_err NE ‘X‘.
  PERFORM frm_edit_data.

*****&step3.显示
  PERFORM frm_alv_diaplay.

*****&step4.发邮件
  IF p_rb_01 = ‘X‘.
    PERFORM frm_sent_emial.
  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  FRM_GET_DATA
*&---------------------------------------------------------------------*
*       获取数据到内表
*----------------------------------------------------------------------*
FORM frm_get_data .
  "数据定义
  DATA: lv_date TYPE sy-datum,
        lt_mseg TYPE TABLE OF ty_mseg.
  "数据初始化
  REFRESH:gt_stkl,r_bsl,r_sl,gt_out,gt_mara,
          gt_mseg,gt_mard,gt_email,gt_t001l.

*****&step1.从自建表获得库存地点信息并放到对应的range table
  SELECT * FROM ztatj_stk_loc INTO TABLE gt_stkl
                              WHERE werks IN s_werks.
*
  LOOP AT  gt_stkl INTO gw_stkl.
    "Backflush Storage location
    r_bsl-sign   = ‘I‘.
    r_bsl-option = ‘EQ‘.
    r_bsl-low    = gw_stkl-zbsl.
    APPEND r_bsl.

    "Backflush Storage location
    r_wsl-sign   = ‘I‘.
    r_wsl-option = ‘EQ‘.
    r_wsl-low    = gw_stkl-zwsl.
    APPEND r_wsl.

    "Storage location
    r_sl-sign   = ‘I‘.
    r_sl-option = ‘EQ‘.
    r_sl-low    = gw_stkl-zbsl.
    APPEND r_sl.
    r_sl-low    = gw_stkl-zwsl.
    APPEND r_sl.
    CLEAR:gw_stkl,r_bsl,r_sl,r_wsl.
  ENDLOOP.

*****&step2.从mseg和mkpf获取数据
  lv_date = sy-datum - 1.

  SELECT a~mblnr b~matnr b~werks b~bwart b~shkzg b~erfmg b~lgort
            INTO CORRESPONDING FIELDS OF TABLE gt_mseg
                FROM mkpf AS a INNER JOIN mseg AS b
                         ON a~mblnr = b~mblnr
                        AND a~mjahr = b~mjahr
                      WHERE ( ( a~cpudt = lv_date  "取昨天6点到今天6点的数据
                        AND a~cputm GE ‘060000‘  "昨天6点-24点的数据
                        AND a~cputm LE ‘240000‘ )"加今天0点到6点的数据
                         OR ( a~cpudt = sy-datum
                        AND a~cputm LE ‘060000‘ ) )
                        AND b~bwart IN (‘131‘,‘311‘)
                        AND b~matnr IN s_matnr
                        AND b~lgort IN r_sl
                        AND b~werks IN s_werks.

  IF sy-subrc <> 0.
    MESSAGE s208(00) WITH ‘No data find!‘ DISPLAY LIKE ‘E‘.
    gv_err = ‘X‘.
    EXIT.
  ENDIF.

*****&step3.从mara获取物料主数据
  "for all entries in
*  REFRESH lt_mseg.
*  lt_mseg = gt_mseg.
*  SORT lt_mseg BY matnr.
*  DELETE ADJACENT DUPLICATES FROM lt_mseg COMPARING matnr.

  SELECT matnr bismt meins INTO TABLE gt_mara FROM mara.
*                  FOR ALL ENTRIES IN  lt_mseg
*                        WHERE matnr = lt_mseg-matnr.

*****&step4.从mard获取库存数据
  SELECT matnr werks lgort labst INTO TABLE gt_mard FROM mard
                         WHERE werks IN s_werks
                          AND  lgort IN r_sl.

*****&step5.从自己表中获取ztatj_stk_email获取邮件收件人信息
  SELECT * INTO TABLE gt_email FROM ztatj_stk_email.

*****&step6.获取库存地点描述
  SELECT * INTO TABLE gt_t001l FROM t001l
                          WHERE werks IN s_werks
                            AND lgort IN r_sl.

ENDFORM.                    " FRM_GET_DATA
*&---------------------------------------------------------------------*
*&      Form  FRM_EDIT_DATA
*&---------------------------------------------------------------------*
*       数据处理
*----------------------------------------------------------------------*
FORM frm_edit_data .
  DATA:lt_mseg  TYPE TABLE OF ty_mseg,
       lw_mseg  TYPE ty_mseg.
  CLEAR:gw_out,gw_mara,gw_mseg,gw_mard,
        gw_stkl,gw_email,gw_t001l,lw_mseg.
  FREE:lt_mseg.
  lt_mseg = gt_mseg.


*****&step1.获取料号、计算报工数量、仓库接收数量
  LOOP AT gt_mseg INTO gw_mseg.
    gw_out-matnr = gw_mseg-matnr.
    gw_out-werks = gw_mseg-werks.

    "计算报工数量
    IF  gw_mseg-bwart = ‘131‘
    AND  gw_mseg-lgort IN r_bsl.
      gw_out-lgort = gw_mseg-lgort.
      gw_out-zscsl = gw_mseg-erfmg.
      COLLECT gw_out INTO gt_out.
    ENDIF.

    "仓库接收数量
    IF  gw_mseg-bwart = ‘311‘.
      "同一凭证贷方H 从线边库Backflush;同时借方S到Warehouse
      IF   gw_mseg-shkzg = ‘H‘.
        READ TABLE lt_mseg INTO lw_mseg
              WITH KEY  mblnr = gw_mseg-mblnr
                        shkzg = ‘S‘.
        IF sy-subrc = 0.

          READ TABLE gt_stkl INTO gw_stkl
            WITH KEY werks = gw_mseg-werks
                     zbsl  = gw_mseg-lgort
                     zwsl  = lw_mseg-lgort.
          IF sy-subrc = 0.
            gw_out-lgort = lw_mseg-lgort.
            gw_out-zjssl = gw_mseg-erfmg.
            COLLECT gw_out INTO gt_out.
            CLEAR:gw_stkl.
          ENDIF.

        ENDIF.
      ENDIF.

      "同一凭证贷方H 从线边库Warehouse;同时借方S到Backflush
      IF   gw_mseg-shkzg = ‘S‘.
        READ TABLE lt_mseg INTO lw_mseg
              WITH KEY  mblnr = gw_mseg-mblnr
                        shkzg = ‘H‘.
        IF sy-subrc = 0.
          READ TABLE gt_stkl INTO gw_stkl
            WITH KEY werks = gw_mseg-werks
                     zbsl  = gw_mseg-lgort
                     zwsl  = lw_mseg-lgort.
          IF sy-subrc = 0.
            gw_out-lgort = lw_mseg-lgort.
            gw_out-zjssl = 0 - gw_mseg-erfmg.
            COLLECT gw_out INTO gt_out.
            CLEAR:gw_stkl.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDIF.

    CLEAR:gw_out,gw_mseg,lw_mseg.
  ENDLOOP.
*****&step2.获取在warehouse中的且没有做过131、311物料移动的数据
  LOOP AT gt_mard INTO gw_mard WHERE lgort IN r_wsl.
    READ TABLE gt_out INTO gw_out
      WITH KEY  matnr = gw_mard-matnr
                werks = gw_mard-werks
                lgort = gw_mard-lgort.
    IF sy-subrc <> 0.
      IF gw_mard-labst NE 0.
        gw_out-matnr = gw_mard-matnr.
        gw_out-werks = gw_mard-werks.
        gw_out-lgort = gw_mard-lgort.
        APPEND gw_out TO gt_out.
      ENDIF.
    ENDIF.
    CLEAR:gw_out,gw_mard.
  ENDLOOP.

*****&step3.获取其它输出数据
  SORT gt_mara  BY matnr.
  SORT gt_t001l BY werks lgort.
  SORT gt_mard  BY matnr werks lgort.

  LOOP AT gt_out INTO gw_out.
    "获取物料类型和物料单位
    READ TABLE gt_mara INTO gw_mara
      WITH KEY matnr = gw_out-matnr BINARY SEARCH.
    IF sy-subrc = 0.
      gw_out-bismt = gw_mara-bismt.
      gw_out-meins = gw_mara-meins.
    ENDIF.

    "获取库存地点描述
    READ TABLE gt_t001l INTO gw_t001l
      WITH KEY werks = gw_out-werks
               lgort = gw_out-lgort BINARY SEARCH.
    IF sy-subrc = 0.
      gw_out-lgobe = gw_t001l-lgobe.
    ENDIF.

    "获取总库存
    READ TABLE gt_mard INTO gw_mard
       WITH KEY matnr = gw_out-matnr
                werks = gw_out-werks
                lgort = gw_out-lgort BINARY SEARCH.
    IF sy-subrc = 0.
      gw_out-zkcsl = gw_mard-labst.
    ENDIF.

    MODIFY gt_out FROM gw_out.
    CLEAR:gw_out,gw_mara,gw_t001l,gw_mard.
  ENDLOOP.

  SORT gt_out BY werks.
ENDFORM.                    " FRM_EDIT_DATA
*&---------------------------------------------------------------------*
*&      Form  FRM_ALV_DIAPLAY
*&---------------------------------------------------------------------*
*       显示
*----------------------------------------------------------------------*
FORM frm_alv_diaplay .
******设置字段输出属性
  PERFORM frm_set_fieldcat.
******设置事件
  PERFORM frm_set_events.
******设置输出LAYOUT
  PERFORM frm_set_layout.

  CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
   EXPORTING
     i_callback_program       = sy-repid
*      i_callback_pf_status_set = e_status
*      i_callback_user_command  = e_user_command
     i_save                   = ‘A‘
     is_layout                = gw_layout
     it_fieldcat              = gt_fieldcat
     it_events                = gt_event
   TABLES
     t_outtab                 = gt_out
   EXCEPTIONS
     program_error            = 1
     OTHERS                   = 2.

ENDFORM.                    " FRM_ALV_DIAPLAY
*&---------------------------------------------------------------------*
*&      Form  FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
*       设置字段输出属性
*----------------------------------------------------------------------*
FORM frm_set_fieldcat .
  DATA:l_nn TYPE i.
  DEFINE add_fieldcat.
    l_nn = l_nn + 1.
    clear gw_fieldcat.
    gw_fieldcat-fieldname    =  &1.
    gw_fieldcat-seltext_l    =  &2.
    gw_fieldcat-tabname      = ‘GT_OUT‘.
    gw_fieldcat-col_pos      =  l_nn.
    gw_fieldcat-key          = &3.
    gw_fieldcat-no_zero      = &4.
    append gw_fieldcat to gt_fieldcat.
  END-OF-DEFINITION.
  CLEAR: gt_fieldcat[],gt_fieldcat.
  add_fieldcat   ‘BISMT‘   ‘Type‘(001)       ‘X‘  ‘‘.
  add_fieldcat   ‘MATNR‘   ‘Material‘(002)   ‘X‘  ‘‘.
  add_fieldcat   ‘WERKS‘   ‘Plant‘(003)      ‘X‘  ‘‘.
  add_fieldcat   ‘LGORT‘   ‘Stor‘(004)       ‘X‘  ‘‘.
  add_fieldcat   ‘LGOBE‘   ‘Stor Des.‘(005)  ‘‘   ‘‘.
  add_fieldcat   ‘ZSCSL‘   ‘TP Produced Qty Daily‘(006)  ‘‘  ‘X‘ .
  add_fieldcat   ‘ZJSSL‘   ‘WH Received Qty Daily‘(007)  ‘‘  ‘X‘.
  add_fieldcat   ‘ZKCSL‘   ‘Total Stock‘(008)            ‘‘  ‘X‘.
  add_fieldcat   ‘MEINS‘   ‘Unit‘(009)                   ‘‘  ‘‘.

ENDFORM.                    " FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  FRM_SET_EVENTS
*&---------------------------------------------------------------------*
*       设置事件
*----------------------------------------------------------------------*
FORM frm_set_events .
  DATA: ls_event TYPE slis_alv_event.

  CALL FUNCTION ‘REUSE_ALV_EVENTS_GET‘
    EXPORTING
      i_list_type = 0
    IMPORTING
      et_events   = gt_event.
ENDFORM.                    " FRM_SET_EVENTS
*&---------------------------------------------------------------------*
*&      Form  FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*       设置输出LAYOUT
*----------------------------------------------------------------------*
FORM frm_set_layout .
  CLEAR gw_layout.
  MOVE:  ‘X‘    TO  gw_layout-zebra,
         ‘X‘    TO  gw_layout-colwidth_optimize,
         ‘SEL‘  TO  gw_layout-box_fieldname.
*         ‘X‘  TO  gw_layout-detail_popup.
ENDFORM.                    " FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*&      Form  FRM_SENT_EMIAL
*&---------------------------------------------------------------------*
*       发邮件
*----------------------------------------------------------------------*
FORM frm_sent_emial .
  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,
          lv_title        TYPE so_obj_des,
          v_docchgi       TYPE sodocchgi1,
          v_lines_bin_all TYPE i,
          v_filelen       TYPE i.
  DATA:   lv_date         TYPE string,
          lv_data         TYPE string,
          lv_time         TYPE string,
          lv_mess         TYPE string.

*****&step1.邮件收件人邮箱
  LOOP AT gt_email INTO gw_email.
    i_reclist-receiver = gw_email-smtp_addr.
    i_reclist-express = ‘X‘. "收件人
    i_reclist-rec_type = ‘U‘.
    APPEND i_reclist.
    CLEAR: i_reclist,gw_email.
  ENDLOOP.

*****&step2.邮件主题
  lv_title = ‘VWATJ Transmission stock daily report 变速器每日库存报告‘.
  v_docchgi-obj_name = ‘Text‘.
  v_docchgi-expiry_dat = sy-datum + 10 . "过期日期
  v_docchgi-sensitivty = ‘F‘. "Functional object
  v_docchgi-doc_size = v_lines_txt * 255.
  v_docchgi-obj_descr = lv_title.


*****&step3.邮件正文
  CLEAR:i_objtxt.
  i_objtxt-line = ‘Dear All,‘.
  INSERT i_objtxt INDEX 1.
  INSERT INITIAL LINE INTO i_objtxt INDEX 2  .

  CONCATENATE sy-datum(4) ‘/‘ sy-datum+4(2) ‘/‘ sy-datum+6(2)
  INTO lv_date.
  CONCATENATE  ‘  VWATJ Transmission stock daily report on‘ lv_date
  ‘in the attachment.‘ INTO lv_mess SEPARATED BY space.
  CLEAR:i_objtxt.
  i_objtxt-line = lv_mess.
  INSERT i_objtxt INDEX 3.
  CLEAR:lv_mess.
  INSERT INITIAL LINE INTO i_objtxt INDEX 4  .


*****&step4.邮件签名

  APPEND INITIAL LINE TO i_objtxt.

  "Initial line
  APPEND INITIAL LINE TO i_objtxt.

  lv_mess = ‘Ext.# VWATD:19222, VWATJ:9222‘.
  MOVE lv_mess TO i_objtxt-line.
  APPEND i_objtxt.
  CLEAR:lv_mess,i_objtxt.

  lv_mess = ‘+86 22 5880 9222‘.
  MOVE lv_mess TO i_objtxt-line.
  APPEND i_objtxt.
  CLEAR:lv_mess,i_objtxt.

  "Initial line
  APPEND INITIAL LINE TO i_objtxt.

  lv_mess = ‘http://list.vwatdportal.ap.vwg‘.
  MOVE lv_mess TO i_objtxt-line.
  APPEND i_objtxt.
  CLEAR:lv_mess,i_objtxt.

  lv_mess = ‘http://list.vwatjportal.ap.vwg‘.
  MOVE lv_mess TO i_objtxt-line.
  APPEND i_objtxt.
  CLEAR:lv_mess,i_objtxt.

  "Initial line
  APPEND INITIAL LINE TO i_objtxt.

  lv_mess = ‘itservice@atd.volkswagen.com.cn‘.
  MOVE lv_mess TO i_objtxt-line.
  APPEND i_objtxt.
  CLEAR:lv_mess,i_objtxt.

  lv_mess = ‘itservice@atj.volkswagen.com.cn‘.
  MOVE lv_mess TO i_objtxt-line.
  APPEND i_objtxt.
  CLEAR:lv_mess,i_objtxt.

  "Initial line
  APPEND INITIAL LINE TO i_objtxt.

  lv_mess = ‘IT Service, Your reliable IT assistant‘.
  MOVE lv_mess TO i_objtxt-line.
  APPEND i_objtxt.
  CLEAR:lv_mess,i_objtxt.

  "Initial line
  APPEND INITIAL LINE TO i_objtxt.


  CONCATENATE sy-datum(4) ‘.‘ sy-datum+4(2) ‘.‘ sy-datum+6(2)
  INTO lv_data .
  CONCATENATE sy-uzeit(2) ‘:‘ sy-uzeit+2(2) INTO lv_time.
  CONCATENATE lv_data lv_time INTO lv_mess SEPARATED BY space.
  MOVE lv_mess TO i_objtxt-line.
  APPEND i_objtxt.
  CLEAR:lv_mess,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.
*****&step5.邮件附件
  "把输出表数据转化为附件格式数据
  PERFORM frm_attac_get.
  "输出内表转化为字符串
  PERFORM frm_table_change TABLES gt_attac USING lv_mess.
  "将字符串转为记录
  PERFORM frm_str_record TABLES i_record USING lv_mess v_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 ‘VWATJ Transmission stock daily report .xls‘ ‘‘
  INTO i_objpack-obj_descr."附件名
  APPEND i_objpack.


*****&step6.发送邮件
  CALL FUNCTION ‘SO_NEW_DOCUMENT_ATT_SEND_API1‘
    EXPORTING
      document_data              = http://www.mamicode.com/v_docchgi" FRM_SENT_EMIAL
*&---------------------------------------------------------------------*
*&      Form  FRM_TABLE_CHANGE
*&---------------------------------------------------------------------*
*       "输出内表转化为字符串
*----------------------------------------------------------------------*
FORM frm_table_change  TABLES  intab
                       USING    outstr TYPE string.
  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: <comp_wa> TYPE abap_compdescr ,
                 <f_field> ,
                 <f_intab> TYPE ANY .
  DATA:str TYPE string,
       str2 TYPE string ,
       text1 TYPE c.
  descr_ref ?= cl_abap_typedescr=>describe_by_data( intab ).
  LOOP AT descr_ref->components ASSIGNING <comp_wa>.
    MOVE-CORRESPONDING <comp_wa> TO headtab.
    APPEND headtab.
  ENDLOOP.
  DESCRIBE TABLE headtab LINES n.
  LOOP AT intab ASSIGNING <f_intab>.
    DO n TIMES.
      ASSIGN COMPONENT sy-index OF STRUCTURE <f_intab> TO <f_field>.
      str = <f_field>.
      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 outstr str2 enter INTO outstr.
    CLEAR str2.
  ENDLOOP.


ENDFORM.                    " FRM_TABLE_CHANGE
*&---------------------------------------------------------------------*
*&      Form  FRM_STR_RECORD
*&---------------------------------------------------------------------*
*       将字符串转为记录
*----------------------------------------------------------------------*
FORM frm_str_record  TABLES record USING str len.
  DATA:tmpbuffer TYPE xstring.
  CALL FUNCTION ‘SCMS_STRING_TO_XSTRING‘
    EXPORTING
      text     = str
*      mimetype = ‘"text/html; charset=gb2312"‘
       mimetype = ‘"APPLICATION/MSEXCEL;charset=utf-16le"‘
*      encoding = ‘8400‘
    IMPORTING
      buffer   = tmpbuffer
    EXCEPTIONS
      failed   = 1
      OTHERS   = 2.

  IF sy-subrc = 0.
    CONCATENATE cl_abap_char_utilities=>byte_order_mark_little
    tmpbuffer INTO tmpbuffer IN BYTE MODE.
  ENDIF.

  CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY‘
    EXPORTING
      buffer          = tmpbuffer
      append_to_table = ‘‘
    IMPORTING
      output_length   = len
    TABLES
      binary_tab      = record.
ENDFORM.                    " FRM_STR_RECORD
*&---------------------------------------------------------------------*
*&      Form  frm_modify_gt_ZTMMATJ_PRMILLOG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_SUBRC    text
*----------------------------------------------------------------------*
FORM frm_modify_gt_ztmmatj_prmillog USING p_subrc TYPE sysubrc.

  gt_ztmmatj_prmillog-state = p_subrc.
  gt_ztmmatj_prmillog-descp = gv_message_text_output.
  IF gt_ztmmatj_prmillog-object_id IS INITIAL.
    gt_ztmmatj_prmillog-object_id = gv_new_object_id.
  ENDIF.
  gt_ztmmatj_prmillog-uname = sy-uname.
  gt_ztmmatj_prmillog-datum = sy-datum.
  gt_ztmmatj_prmillog-uzeit = sy-uzeit.

  APPEND gt_ztmmatj_prmillog.
  CLEAR:gt_ztmmatj_prmillog.


ENDFORM.                    "frm_modify_gt_ZTMMATJ_PRMILLOG
*&---------------------------------------------------------------------*
*&      Form  frm_MESSAGE_TEXT_BUILD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM frm_message_text_build.
  CALL FUNCTION ‘MESSAGE_TEXT_BUILD‘
    EXPORTING
      msgid               = sy-msgid
      msgnr               = sy-msgno
      msgv1               = sy-msgv1
      msgv2               = sy-msgv2
      msgv3               = sy-msgv3
      msgv4               = sy-msgv4
    IMPORTING
      message_text_output = gv_message_text_output.
ENDFORM.                    "frm_MESSAGE_TEXT_BUILD
*&---------------------------------------------------------------------*
*&      Form  FRM_ATTAC_GET
*&---------------------------------------------------------------------*
*     把输出表数据转化为附件格式数据
*----------------------------------------------------------------------*
FORM frm_attac_get .
  DATA:lw_out TYPE ty_out,
       lv_sum LIKE mard-labst.
  REFRESH: gt_attac.
  CLEAR: gt_attac,lw_out,lv_sum.


  SORT gt_out BY werks.
  LOOP AT gt_out INTO gw_out.
*******& AT END OF 和 ENDAT之间的loop工作区是*号,故要先赋给另一个工作区
    MOVE  gw_out TO lw_out.
    "表头数据
    AT NEW werks.
      gt_attac-bismt = ‘Type‘.
      gt_attac-matnr = ‘Material‘.
      gt_attac-werks = ‘Plnt‘.
      gt_attac-lgort = ‘Sloc‘.
      gt_attac-lgobe = ‘Sloc Des.‘.
      gt_attac-zscsl = ‘TP Produced Qty Daily‘.
      gt_attac-zjssl = ‘WH Recieved Qty Daily‘.
      gt_attac-zkcsl = ‘Total Stock‘.
      gt_attac-meins = ‘Unit‘.
      APPEND gt_attac.
      CLEAR:gt_attac.
      gt_attac-bismt = ‘类型‘.
      gt_attac-matnr = ‘物料‘.
      gt_attac-werks = ‘工厂‘.
      gt_attac-lgort = ‘库存地点‘.
      gt_attac-lgobe = ‘库存地点描述‘.
      gt_attac-zscsl = ‘生产报工数量 ‘.
      gt_attac-zjssl = ‘仓库接收数量‘.
      gt_attac-zkcsl = ‘总库存‘.
      gt_attac-meins = ‘单位‘.
      APPEND gt_attac.
      CLEAR:gt_attac.
    ENDAT.
    "行项目数据
    MOVE-CORRESPONDING lw_out TO gt_attac.
    APPEND gt_attac.
    CLEAR:gt_attac.
    lv_sum = lv_sum + lw_out-zkcsl.
    "表尾数据
    AT END OF werks.
      gt_attac-bismt = ‘Total‘.
      gt_attac-zkcsl =  lv_sum.
      gt_attac-meins = lw_out-meins.
      APPEND gt_attac.
      CLEAR:gt_attac,lv_sum.
      APPEND INITIAL LINE TO gt_attac.
    ENDAT.
  ENDLOOP.

ENDFORM.                    " FRM_ATTAC_GET

  

ABAP发邮件函数