Keyword: sost
In SAP, We can configure the stmp mail function to view the mail status through sost. In encoding, FM so_document_send_api1 and so_new_document_att_send_api1 can be used to send emails and email attachments by encoding. When defining an attachment table, use solisti1. Its length is char255. In reality, each row of records exported exceeds this length. For XLS lattice try attachment we can use Excel separator label to solve this problem, for text garbled we can add charset = utf-16le in the attachment to solve this problem. Similarly, it applies to other similar garbled issues.
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 the 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, "Cycler
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 '.
* Email
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'
Processing ing
Result = sent_to_all ).
If sent_to_all = 'X' and sy-batch <> 'x '.
Message 'send successfully 'type '.
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.