带EXCEL附件邮件发送相关代码

1.查看生成的邮件

2.1 非面向对象的方式(demo直接copy即可)

​
REPORT Z12.
DATA: IT_DOCUMENT_DATA TYPE SODOCCHGI1,
      IT_CONTENT_TEXT  TYPE STANDARD TABLE OF SOLISTI1 WITH HEADER LINE,
      IT_PACKING_LIST  TYPE TABLE OF SOPCKLSTI1 WITH HEADER LINE,
      IT_RECEIVERS     TYPE STANDARD TABLE OF SOMLRECI1 WITH HEADER LINE,
      LC_MAIL_ATTACH   TYPE STRING,
      LT_CONTENT_HEX   TYPE STANDARD TABLE OF SOLIX WITH HEADER LINE,
      LT_OBJECT_HEADER TYPE STANDARD TABLE OF SOLISTI1 WITH HEADER LINE,
      LC_MAIL_XATTACH  TYPE XSTRING,
      LV_SEND_ALL      TYPE C,
      IT_MAKT          LIKE TABLE OF MAKT WITH HEADER LINE,
      FIR_DATE         TYPE SY-DATUM,
      MIMETYPE         TYPE CHAR64.
DATA: IT_ITAB TYPE TABLE OF ITAB WITH HEADER LINE,
      WA_ITAB TYPE ITAB.

CONSTANTS:LC_TAB  TYPE C VALUE CL_BCS_CONVERT=>GC_TAB,       "excel换格符
          LC_CTRL TYPE C VALUE CL_BCS_CONVERT=>GC_CRLF.      "excel换行符

  PERFORM SEND_EMAIL.


*&---------------------------------------------------------------------*
*&      Form  SEND_EMAIL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM SEND_EMAIL.
  DATA SY_VLINE TYPE I.
  DATA:MBLNR TYPE STRING,   "物料凭证
       MATNR TYPE STRING,   "物料
       MJAHR TYPE STRING,   "年度
       MAKTX TYPE STRING.   "物料描述

****邮件标题信息
  IT_DOCUMENT_DATA-OBJ_DESCR = '邮件主题1' .    "内容的简短描述
  IT_DOCUMENT_DATA-PRIORITY = '1'.             "优先次序  1~9  1:最高优先权
  IT_DOCUMENT_DATA-OBJ_NAME = 'OFFER'.
  IT_DOCUMENT_DATA-OBJ_LANGU = SY-LANGU.

*****邮件正文信息
  IT_CONTENT_TEXT = '各位好:'.
  APPEND IT_CONTENT_TEXT.
  IT_CONTENT_TEXT = '  此邮件为测试邮件!!!!!!!'.
  APPEND IT_CONTENT_TEXT.

  DESCRIBE TABLE IT_CONTENT_TEXT LINES SY_VLINE.
  IT_DOCUMENT_DATA-DOC_SIZE = 255 * ( SY_VLINE - 1 ) + STRLEN( IT_CONTENT_TEXT ).  "SAPoffice 文档的大小(用于 API1)

  IT_PACKING_LIST-TRANSF_BIN = SPACE.
  IT_PACKING_LIST-HEAD_START = 1 .
  IT_PACKING_LIST-HEAD_NUM =  0 .
  IT_PACKING_LIST-BODY_START = 1. "控制从第几行开始读
  IT_PACKING_LIST-BODY_NUM = SY_VLINE."控制读到第几行
  IT_PACKING_LIST-DOC_TYPE = 'RAW'.
  APPEND IT_PACKING_LIST.


***收件人信息
  IT_RECEIVERS-RECEIVER = 'MEIQIAN.LIANG@HAND-CHINA.COM'.  "收件人地址
  IT_RECEIVERS-REC_TYPE = 'U'.
  IT_RECEIVERS-COM_TYPE = 'INT'.
  IT_RECEIVERS-NOTIF_DEL =  'X'.
  IT_RECEIVERS-NOTIF_NDEL = 'X'.
  APPEND IT_RECEIVERS.

  CONCATENATE '物料'     LC_TAB
  '物料凭证' LC_TAB
  '物料描述' LC_TAB
  '年度'     LC_CTRL INTO LC_MAIL_ATTACH.   "lc_ctrl换行符

  CLEAR:MBLNR, MATNR, MJAHR, MAKTX.
  MBLNR  = '7777777'.
  MATNR  = '7777777'.
  MJAHR  = '7777'.
  MAKTX  = 'seven'."物料类型

  CONCATENATE LC_MAIL_ATTACH
  MATNR  LC_TAB
  MBLNR  LC_TAB
  MJAHR  LC_TAB
  MAKTX  LC_CTRL INTO LC_MAIL_ATTACH.      "lc_ctrl换行符

  CLEAR:MBLNR, MATNR, MJAHR, MAKTX.
  MBLNR  = '8888888'.
  MATNR  = '8888888'.
  MJAHR  = '8888'.
  MAKTX  = 'seven'."物料类型

  CONCATENATE LC_MAIL_ATTACH
  MATNR  LC_TAB
  MBLNR  LC_TAB
  MJAHR  LC_TAB
  MAKTX  LC_CTRL INTO LC_MAIL_ATTACH.      "lc_ctrl换行符

  MIMETYPE = 'APPLICATION/MSEXCEL;CHARSET=UTF-16LE'.
  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      TEXT     = LC_MAIL_ATTACH
      MIMETYPE = MIMETYPE
    IMPORTING
      BUFFER   = LC_MAIL_XATTACH
    EXCEPTIONS
      FAILED   = 1
      OTHERS   = 2.

  IF SY-SUBRC = 0.
    CONCATENATE CL_ABAP_CHAR_UTILITIES=>BYTE_ORDER_MARK_LITTLE LC_MAIL_XATTACH INTO LC_MAIL_XATTACH IN BYTE MODE.
  ENDIF.

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' "将Xtring转化成二进制格式并保存到内表中
    EXPORTING
      BUFFER     = LC_MAIL_XATTACH
    TABLES
      BINARY_TAB = LT_CONTENT_HEX.

  "不知道干啥用的
  LT_OBJECT_HEADER = 'Seven test'.
  APPEND LT_OBJECT_HEADER.

  DESCRIBE TABLE LT_CONTENT_HEX LINES SY_VLINE.

  IT_PACKING_LIST-TRANSF_BIN = 'X'.
  IT_PACKING_LIST-HEAD_START = 1 .
  IT_PACKING_LIST-HEAD_NUM =  0 .
  IT_PACKING_LIST-BODY_START = 1.
  IT_PACKING_LIST-BODY_NUM = SY_VLINE.
  IT_PACKING_LIST-DOC_TYPE = 'XLS'.
  IT_PACKING_LIST-DOC_SIZE = 255 * SY_VLINE.
  IT_PACKING_LIST-OBJ_NAME = '附件名'.
  IT_PACKING_LIST-OBJ_DESCR = IT_PACKING_LIST-OBJ_NAME."文档名字
  APPEND IT_PACKING_LIST.


  "方法1  无法指定发送人邮箱
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      DOCUMENT_DATA              = IT_DOCUMENT_DATA
      PUT_IN_OUTBOX              = 'X'
      COMMIT_WORK                = 'X'
    IMPORTING
      SENT_TO_ALL                = LV_SEND_ALL
*     NEW_OBJECT_ID              =
    TABLES
      PACKING_LIST               = IT_PACKING_LIST[]
      OBJECT_HEADER              = LT_OBJECT_HEADER[]
*     contents_bin               =
      CONTENTS_TXT               = IT_CONTENT_TEXT[]
      CONTENTS_HEX               = LT_CONTENT_HEX[]
*     object_para                =
*     object_parb                =
      RECEIVERS                  = IT_RECEIVERS[]
    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.

*  "方法2  可以指定发送人邮箱
*  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
*    EXPORTING
*      DOCUMENT_DATA              = IT_DOCUMENT_DATA
*      PUT_IN_OUTBOX              = 'X'
*      SENDER_ADDRESS             = 'MEIQIAN.LIANG@HAND-CHINA.COM'
*      SENDER_ADDRESS_TYPE        = 'INT'
*      COMMIT_WORK                = 'X'
**     IP_ENCRYPT                 =
**     IP_SIGN                    =
**     IV_VSI_PROFILE             =
*    IMPORTING
*      SENT_TO_ALL                = LV_SEND_ALL
**     NEW_OBJECT_ID              =
**     SENDER_ID                  =
*    TABLES
*      PACKING_LIST               = IT_PACKING_LIST[]
*      OBJECT_HEADER              = LT_OBJECT_HEADER[]
**     CONTENTS_BIN               =
*      CONTENTS_TXT               = IT_CONTENT_TEXT[]
*      CONTENTS_HEX               = LT_CONTENT_HEX[]
**     OBJECT_PARA                =
**     OBJECT_PARB                =
*      RECEIVERS                  = IT_RECEIVERS[]
**     ET_VSI_ERROR               =
*    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.
**   Implement suitable error handling here
*  ELSE.
*  BREAK-POINT.
*  ENDIF.

ENDFORM.                    "SEND_EMAIL

​

非面向对象运行效果图:

如果要抄送则增加以下属性:

2.2面向对象的方法


REPORT Z1234.

TABLES:MSEG,SSCRFIELDS,MARA,ZOWMS_DAYSTOCK_R,ADR6.
TYPE-POOLS:ICON,SLIS.


TYPES: BEGIN OF TYS_DETAIL,
         TOTAL(100),
         SEL(1),
         STATUS(4),
         ITEM_STATUS(4),
         CYMENG         TYPE MSEG-MENGE, "差异数量
         XCHAR          TYPE MARC-XCHAR, "批次管理标识
         MAKTX          TYPE MAKT-MAKTX,
         LGOBE          TYPE T001L-LGOBE,
         MEINS          TYPE MARA-MEINS,
         LABST          TYPE MARD-LABST. "SAP库存
        INCLUDE STRUCTURE ZOWMS_DAYSTOCK_R.
TYPES:     END OF TYS_DETAIL.
DATA: GT_DETAIL     TYPE STANDARD TABLE OF TYS_DETAIL,
      GT_DETAIL_TMP TYPE TABLE OF TYS_DETAIL,
      GT_DETAIL_KEY TYPE TABLE OF TYS_DETAIL.


DATA FUNCTXT TYPE SMP_DYNTXT.


DATA: I_BDCDATA   LIKE BDCDATA OCCURS 0 WITH HEADER LINE.

*ALV定义
DATA: GT_FIELDCAT TYPE LVC_T_FCAT,
      GV_CODE     LIKE SY-UCOMM,
      GW_LAYOUT   TYPE LVC_S_LAYO,
      I_CELLCLR   TYPE LVC_S_SCOL. "单元格颜色
TYPES: BEGIN OF SLIS_EXTAB,
         FCODE LIKE RSMPE-FUNC,
       END OF SLIS_EXTAB.
TYPES: SLIS_T_EXTAB TYPE SLIS_EXTAB OCCURS 1.
DATA:LS_CELLTAB TYPE LVC_S_STYL.
DATA LS_STABLE TYPE LVC_S_STBL.
DATA:GS_FIELDCAT TYPE LVC_S_FCAT.
DATA:GT_HEADER TYPE TABLE OF CHAR30,
     GV_CHAR30 TYPE CHAR30.
*子ALV调用
DATA:
  LT_FIELDCAT TYPE LVC_T_FCAT,
  LS_FCAT     TYPE LVC_S_FCAT,
  LS_SELFIELD TYPE SLIS_SELFIELD.



SELECTION-SCREEN BEGIN OF BLOCK BLK2 WITH FRAME TITLE TEXT-T02.
SELECT-OPTIONS: S_RECEP FOR ADR6-SMTP_ADDR NO INTERVALS
                                          MODIF ID MAL,"收件人
                S_SCR FOR ADR6-SMTP_ADDR  NO INTERVALS "抄送人
                                          MODIF ID MAL.
SELECTION-SCREEN END OF BLOCK BLK2 .


END-OF-SELECTION.


  PERFORM FRM_SEND_MAIL.



FORM FRM_SEND_MAIL .
  DATA: BCS_EXCEPTION        TYPE REF TO CX_BCS,
        ERRORTEXT            TYPE STRING,
        LCL_SEND_REQUEST     TYPE REF TO CL_BCS,
        LCL_DOCUMENT         TYPE REF TO CL_DOCUMENT_BCS,
        LCL_RECIPIENT        TYPE REF TO IF_RECIPIENT_BCS,
        LCL_SENDER           TYPE REF TO CL_CAM_ADDRESS_BCS,
        T_ATTACHMENT_HEADER  TYPE SOLI_TAB,
        WA_ATTACHMENT_HEADER LIKE LINE OF T_ATTACHMENT_HEADER,
        ATTACHMENT_SUBJECT   TYPE SOOD-OBJDES,
        SOOD_BYTECOUNT       TYPE SOOD-OBJLEN,
        MAIL_TITLE           TYPE SO_OBJ_DES,
        T_MAILTEXT           TYPE SOLI_TAB,
        WA_MAILTEXT          LIKE LINE OF T_MAILTEXT,
        SEND_TO              TYPE ADR6-SMTP_ADDR,
        SENT                 TYPE OS_BOOLEAN,
        LV_MAILNAME          TYPE SOOD-OBJDES,
        BINARY_CONTENT       TYPE SOLIX_TAB.
  DATA:LV_SMTP_ADDR          TYPE ADR6-SMTP_ADDR.

  " Set mail subject
  MAIL_TITLE     = '菜鸟&SAP库存比对邮件推送'.
  LV_MAILNAME = '菜鸟&SAP库存比对报表'.
  " Set mail context
  DATA(LV_STRING) = |您好,附件是菜鸟&SAP库存比对报表,请查阅,谢谢|.
  WA_MAILTEXT    = LV_STRING.

  APPEND WA_MAILTEXT TO T_MAILTEXT.
  CONCATENATE '事务码为:ZM390 程序为:ZMM390。' 'Job时间:' SY-DATUM '' SY-UZEIT INTO WA_MAILTEXT.
  APPEND WA_MAILTEXT TO T_MAILTEXT.

  TRY.
* Create send request
      LCL_SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).
* Create new document with mailtitle and mailtextg
      LCL_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT( I_TYPE    = 'RAW' "#EC NOTEXT
      I_TEXT    = T_MAILTEXT
      I_LANGUAGE = SY-LANGU
      I_SUBJECT = MAIL_TITLE ).

* add document to send request
      LCL_SEND_REQUEST->SET_DOCUMENT( LCL_DOCUMENT ).

*收件人
      LOOP AT S_RECEP.
        LV_SMTP_ADDR = S_RECEP-LOW.
        LCL_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( LV_SMTP_ADDR  ).
        LCL_SEND_REQUEST->ADD_RECIPIENT( LCL_RECIPIENT ).
      ENDLOOP.

* 抄送人
      LOOP AT S_SCR.
        LV_SMTP_ADDR = S_SCR-LOW.
        LCL_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( LV_SMTP_ADDR ).
        LCL_SEND_REQUEST->ADD_RECIPIENT(
        I_RECIPIENT  = LCL_RECIPIENT
        I_EXPRESS    = 'X'
        I_COPY       = 'X'
        I_BLIND_COPY = ' '
        I_NO_FORWARD = ' ' ).
      ENDLOOP.
*     添加抄送人

*lcl_document->
      CLEAR LV_STRING.
      PERFORM FRM_SET_DOCUMENT_BODY  CHANGING BINARY_CONTENT.

      LCL_DOCUMENT->ADD_ATTACHMENT(
      I_ATTACHMENT_TYPE    = 'XLS'                        " 附件格式
      I_ATTACHMENT_SUBJECT = LV_MAILNAME                  " 附件名称
*      i_attachment_size    = size                         "附件大小
      I_ATTACHMENT_LANGUAGE = SY-LANGU
*      I_ATTACHMENT_HEADER   =
      I_ATT_CONTENT_HEX    = BINARY_CONTENT
      ).            "附件内容


      LCL_SEND_REQUEST->SET_DOCUMENT( LCL_DOCUMENT ).
      SENT = LCL_SEND_REQUEST->SEND( I_WITH_ERROR_SCREEN = 'X' ).

    CATCH CX_BCS INTO BCS_EXCEPTION.
      ERRORTEXT = BCS_EXCEPTION->IF_MESSAGE~GET_TEXT( ).
      MESSAGE ERRORTEXT TYPE 'S' DISPLAY LIKE 'E'.
  ENDTRY.

  COMMIT WORK.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_SET_DOCUMENT_BODY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_BINARY_CONTENT  text
*----------------------------------------------------------------------*
FORM FRM_SET_DOCUMENT_BODY  CHANGING PT_BODY TYPE SOLIX_TAB.
  TYPES: BEGIN OF TY_DATA,
           WERKS  TYPE ZOWMS_DAYSTOCK_R-WERKS, "工厂
           MATNR  TYPE ZOWMS_DAYSTOCK_R-MATNR, "物料
           MAKTX  TYPE MAKT-MAKTX,             "物料描述
           LGORT  TYPE ZOWMS_DAYSTOCK_R-LGORT, "库存地点
           LGOBE  TYPE T001L-LGOBE,            "库存地点描述
           CHARG  TYPE ZOWMS_DAYSTOCK_R-CHARG, "批次
           LABST  TYPE MARD-LABST,             "SAP库存数量
           OMENGE TYPE ZOWMS_DAYSTOCK_R-OMENGE, "未结库存
           WMENGE TYPE ZOWMS_DAYSTOCK_R-WMENGE, "OFS未传明细
           TMENGE TYPE ZOWMS_DAYSTOCK_R-TMENGE, "SAP库存数量(含未结)
           SMENGE TYPE ZOWMS_DAYSTOCK_R-SMENGE, "WMS库存数量
           CYMENG TYPE MSEG-MENGE.             "差异数量
  TYPES: END OF TY_DATA.

  DATA:LV_STRING   TYPE STRING,
       LC_MIMETYPE TYPE CHAR64 VALUE 'APPLICATION/MSEXCEL;charset=utf-16le',
       LV_XATTACH  TYPE XSTRING,
       LV_VALUE    TYPE STRING,
       LV_FLAG     TYPE C.
  DATA:LT_MAIL TYPE TABLE OF TY_DATA,
       LS_MAIL TYPE TY_DATA.

  CLEAR:GT_HEADER.

  APPEND '工厂' TO GT_HEADER.
  APPEND '物料' TO GT_HEADER.
  APPEND '物料描述' TO GT_HEADER.
  APPEND '库存地点' TO GT_HEADER.
  APPEND '库存地点描述' TO GT_HEADER.
  APPEND 'SAP库存数量' TO GT_HEADER.
  APPEND '未结库存' TO GT_HEADER.
  APPEND 'OFS未传SAP数量' TO GT_HEADER.
  APPEND 'SAP库存数量(含未结未传)' TO GT_HEADER.
  APPEND 'WMS库存数量' TO GT_HEADER.
  APPEND  '差异' TO GT_HEADER.

  LOOP AT GT_HEADER INTO GV_CHAR30.
    CONDENSE GV_CHAR30.
    LV_STRING = |{ LV_STRING }{ GV_CHAR30 }{ CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB }|.
  ENDLOOP.
  LV_STRING = LV_STRING && CL_ABAP_CHAR_UTILITIES=>CR_LF.

  "发送有差异的邮件数据
  MOVE-CORRESPONDING GT_DETAIL[] TO LT_MAIL[].
  DELETE  LT_MAIL[] WHERE CYMENG EQ '0.000'.
  "判断是否发送批次字段的数据
  DATA(LV_LINES1) = LINES( GT_HEADER ).
  IF LV_LINES1 = '11'.
    LV_FLAG = 'X'.
  ENDIF.

  DATA(LV_LINES) = LINES( GT_FIELDCAT ).

  LOOP AT LT_MAIL ASSIGNING FIELD-SYMBOL(<FS_STURC>).
    DO LV_LINES TIMES.
      IF SY-INDEX = 6 AND LV_FLAG = 'X'.
        CONTINUE.
      ENDIF.
      ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_STURC> TO FIELD-SYMBOL(<FS_VALUE>).
      IF SY-SUBRC = 0.
        LV_VALUE = <FS_VALUE>.
        CONDENSE LV_VALUE.
        DATA(LV_LEN) = STRLEN( LV_VALUE ) - 1.
        IF LV_LEN > 0.
          IF LV_VALUE+LV_LEN = '-'.
            LV_VALUE = '-' && LV_VALUE(LV_LEN).
            CONDENSE LV_VALUE NO-GAPS.
            CLEAR LV_LEN.
          ENDIF.
        ENDIF.
      ELSE.
        LV_VALUE = ''.
      ENDIF.
      IF LV_LINES <> SY-INDEX.
        LV_STRING = |{ LV_STRING }{ LV_VALUE }{ CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB }|.
      ELSE.
        LV_STRING = |{ LV_STRING }{ LV_VALUE }{ CL_ABAP_CHAR_UTILITIES=>CR_LF }|.
      ENDIF.
    ENDDO.
  ENDLOOP.
  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      TEXT     = LV_STRING
      MIMETYPE = LC_MIMETYPE
    IMPORTING
      BUFFER   = LV_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
    LV_XATTACH INTO LV_XATTACH IN BYTE MODE.
  ENDIF.
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      BUFFER     = LV_XATTACH
    TABLES
      BINARY_TAB = PT_BODY.
ENDFORM.

面向对象效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值