ABAP 邮件发送解决附件邮件格式已损坏问题,带有xslx 格式附件

该代码示例展示了如何在SAP环境中使用BCS(BusinessCommunicationServices)发送包含文本和MSWord文档附件的电子邮件。它创建了一个持久的发送请求,设置了发送者、收件人,并通过add_attachment方法添加了附件。
摘要由CSDN通过智能技术生成

发送邮件附件代码demo 

REPORT ytest_019.

* This example shows how to send
*   - a simple text provided in an internal table of text lines
*   - and an attached MS word document provided in internal table
*   - to some internet email address.
*
* All activities done via facade CL_BCS!

DATA: send_request       TYPE REF TO cl_bcs.
DATA: text               TYPE bcsy_text.
DATA: binary_content     TYPE solix_tab.
DATA: document           TYPE REF TO cl_document_bcs.
DATA: sender             TYPE REF TO cl_sapuser_bcs.
DATA: recipient          TYPE REF TO if_recipient_bcs.
DATA: bcs_exception      TYPE REF TO cx_bcs.
DATA: sent_to_all        TYPE os_boolean.



START-OF-SELECTION.

  PERFORM main.


*---------------------------------------------------------------------*
*       FORM main                                                     *
*---------------------------------------------------------------------*
FORM main.

  TRY.
*     -------- create persistent send request ------------------------
      send_request = cl_bcs=>create_persistent( ).

*     -------- create and set document with attachment ---------------
*     create document from internal table with text
      APPEND 'Hello world!' TO text.
      document = cl_document_bcs=>create_document(
                      i_type    = 'RAW'
                      i_text    = text
                      i_length  = '12'
                      i_subject = 'test created by BCS_EXAMPLE_2' ).

*     add attachment to document
*     BCS expects document content here e.g. from document upload
*     binary_content = ...

      DATA(lv_attachment_subject)  = 'ztest' && '.xlsx'.


      DATA ls_attachment_header  TYPE soli.
      DATA lt_attachment_header  TYPE soli_tab.

      CONCATENATE '&SO_FILENAME=' lv_attachment_subject INTO ls_attachment_header.
      APPEND ls_attachment_header TO lt_attachment_header.


      CALL METHOD document->add_attachment
        EXPORTING
          i_attachment_type    = 'DOC'
          i_attachment_subject = 'My attachment'
          i_attachment_header  = lt_attachment_header
          i_att_content_hex    = binary_content.

*     add document to send request
      CALL METHOD send_request->set_document( document ).

*     --------- set sender -------------------------------------------
*     note: this is necessary only if you want to set the sender
*           different from actual user (SY-UNAME). Otherwise sender is
*           set automatically with actual user.

      sender = cl_sapuser_bcs=>create( sy-uname ).
      CALL METHOD send_request->set_sender
        EXPORTING
          i_sender = sender.

*     --------- add recipient (e-mail address) -----------------------
*     create recipient - please replace e-mail address !!!
      recipient = cl_cam_address_bcs=>create_internet_address(
                                        'joe.doe@crazy-company.com' ).

*     add recipient with its respective attributes to send request
      CALL METHOD send_request->add_recipient
        EXPORTING
          i_recipient = recipient
          i_express   = 'X'.

*     ---------- send document ---------------------------------------
      CALL METHOD send_request->send(
        EXPORTING
          i_with_error_screen = 'X'
        RECEIVING
          result              = sent_to_all ).
      IF sent_to_all = 'X'.
        WRITE text-003.
      ENDIF.

      COMMIT WORK.


* -----------------------------------------------------------
* *                     exception handling
* -----------------------------------------------------------
* * replace this very rudimentary exception handling
* * with your own one !!!
* -----------------------------------------------------------
    CATCH cx_bcs INTO bcs_exception.
      WRITE: 'Fehler aufgetreten.'(001).
      WRITE: 'Fehlertyp:'(002), bcs_exception->error_type.
      EXIT.

  ENDTRY.

ENDFORM.
关键代码
加附件的时候 调用 add_attachment
给入参像下面这样赋值传入 即可

      DATA(lv_attachment_subject)  = 'ztest' && '.xlsx'.


      DATA ls_attachment_header  TYPE soli.
      DATA lt_attachment_header  TYPE soli_tab.

      CONCATENATE '&SO_FILENAME=' lv_attachment_subject INTO ls_attachment_header.
      APPEND ls_attachment_header TO lt_attachment_header.




      CALL METHOD document->add_attachment
        EXPORTING
          i_attachment_type    = 'DOC'
          i_attachment_subject = 'My attachment'
          i_attachment_header  = lt_attachment_header
          i_att_content_hex    = binary_content.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值