新浪科技

SAP中的邮件发送功能

IT168.com

关注

文本Tag:SAP

【IT168 技术文章】

在SAP中我们可以配置STMP邮件功能,可以通过SOST来查看邮件状态,在编码中可用FM SO_DOCUMENT_SEND_API1 、SO_NEW_DOCUMENT_ATT_SEND_API1通过编码的方式来发送邮件以及邮件附件。在定义附件表时用solisti1,其长度为Char255,实际中我们导出的每一行记录都会超出这个长度。对XLS格试的附件我们可以用Excel的分隔标签来解决这个问题,对于文字乱码我们可以在附件加charset=utf-16le解决此类问题。同样,也适用于其他类似的乱码问题。

constants: c_tab  type c value cl_abap_char_utilities=>horizontal_tab,
            c_cret type c value cl_abap_char_utilities
=>cr_lf,
            c_mimetype type char64
                       value
'APPLICATION/MSEXCEL;charset=utf-16le'.

data:   v_xattach type xstring,

         it_binary_attach type solix_tab.

*---------------------------------------------------------------------------------

* Convert the internal data to XString

*----------------------------------------------------------------------------------

data: lc_descr_ref type ref to cl_abap_structdescr,
         lv_value     type char128,
         lv_temp      type string,
         lv_mid       type string,
         lv_tabix     type sy
-tabix.

   field
-symbols: <fs_intable>  type any.
   field
-symbols: <intable_wa>  type abap_compdescr.

   loop at it_table.

     lv_tabix
= sy-tabix.

     clear lv_temp.
     lc_descr_ref
?= cl_abap_typedescr=>describe_by_data( it_table ).
     loop at lc_descr_ref
->components assigning <intable_wa>.
       assign component sy
-tabix of structure  in_table to <fs_table>.
       lv_value
= <fs_table>.
       condense lv_value.
      
if sy-tabix = 1.
         lv_temp
= lv_value.
        
continue.
       endif.
       concatenate lv_temp lv_value
              into lv_temp separated by c_tab.
     endloop.

     concatenate lv_mid lv_temp c_cret into lv_mid.

   endloop.

*  Convert string to xstring type
*  'APPLICATION/MSEXCEL;charset=utf-16le'
   call function
'SCMS_STRING_TO_XSTRING'
     exporting
       text                
= lv_mid
       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  
= it_binary_attach.

*---------------------------------------------------------------------------------

*Send Email

*----------------------------------------------------------------------------------

   data: lv_title        type so_obj_des,            
" Email Title
            lv_email      type adsmtp-smtp_addr,  " Receiver
            lv_attitle     type char50.                     " Attachment Title

   data: send_request  type ref to     cl_bcs,
             document        type ref to     cl_document_bcs,
             conlengths      type               so_obj_len,
             html                 type table of w3html,  
             sender_id        type ref to     if_sender_bcs,
             recipient          type ref to     if_recipient_bcs,
             cc_recipient     type ref to     if_recipient_bcs,
             sent_to_all      type              os_boolean,
             bcs_exception type ref to     cx_bcs,
            bcs_message   type              string.

   clear:   lv_email,
               lv_title,
               html.      
" Email Body Content with html format

  
try.
*     request email function
       clear send_request.
       send_request
= cl_bcs=>create_persistent( ).

*     Create the content of email
       clear document .
       document  
=  cl_document_bcs=>create_document(
       i_type    
=  'HTM'
       i_text    
=  html
       i_length  
=  conlengths
       i_subject
=  lv_title ).

*     Add Attachment
       call method document
->add_attachment
         exporting
           i_attachment_type      
= 'XLS'
           i_attachment_subject
= lv_attitle
           i_att_content_hex      
= it_binary_attach.

*     Add document to send request
       call method send_request
->set_document( document ).
       clear recipient.

         recipient
=
           cl_cam_address_bcs
=>create_internet_address( lv_email ).
*        Add recipient with its respective attributes to send request
         call method send_request
->add_recipient
           exporting
             i_recipient
= recipient
             i_express  
= 'X'.

*     E-mail
       call method send_request
->set_status_attributes
         exporting
           i_requested_status
= 'E'
           i_status_mail          
= 'E'.
       call method send_request
->set_send_immediately( 'X' ).

*      Send document
       call method send_request
->send(
        exporting
        i_with_error_screen
= 'X'
        receiving
        result
= sent_to_all ).
      
if sent_to_all = 'X' and sy-batch <> 'X'.
         message
'Send Successfully' type 'I'.
       endif.

       commit work.

    
catch cx_bcs into bcs_exception.
       bcs_message
= bcs_exception->get_text( ).
      
if sy-batch <> 'X'.
         message bcs_exception type
'E'.
      
else.
         write: bcs_message.
       endif.
       exit.
   endtry.

加载中...