Hi Experts,
I am using below code for attaching a EXCEL in mail
1.Using this code able to attach a EXCEL file and able to send the mail too.
2.Issue is unable to open the attachment without error - i.e when try to open the attachment it a popup raises with 'The file you are trying to open 'FileNAME.560_X.XLS', is different format than specified by the file extension.Verfiy that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
Please suggest if anything is missing.
CONSTANTS:
* gc_tab TYPE c VALUE ';',
gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab,
gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf.
DATA send_request TYPE REF TO cl_bcs.
DATA document TYPE REF TO cl_document_bcs.
DATA recipient TYPE REF TO if_recipient_bcs.
DATA bcs_exception TYPE REF TO cx_bcs.
DATA main_text TYPE bcsy_text.
DATA binary_content TYPE solix_tab.
DATA size TYPE so_obj_len.
DATA sent_to_all TYPE os_boolean.
PERFORM create_content.
PERFORM send.
*&---------------------------------------------------------------------*
*& Form create_content
*&---------------------------------------------------------------------*
* Create Content
* 1) Write text into a string
* 2) convert this string to solix_tab
*----------------------------------------------------------------------*
FORM create_content.
DATA: lv_string TYPE string,
lv_date(10) TYPE c.
* --------------------------------------------------------------
* columns are separated by TAB and each line ends with CRLF
* header line
CONCATENATE lv_string
'Vendor Number' gc_tab
'Pay Site' gc_tab
'Invoice Number' gc_tab
'Invoice Type' gc_tab
'Invoice Date' gc_tab
'Invoice Description' gc_tab
'Line Type' gc_tab
'Line Description' gc_tab
'PO Number' gc_tab
'Item #' gc_tab
'Supplier UOM' gc_tab
'Quantity' gc_tab
'Price' gc_tab
'Line Total' gc_tab
'Invoice Total' gc_crlf
INTO lv_string.
* data lines
LOOP AT it_final INTO wa_final.
v_quan = wa_final-quantity.
v_price = wa_final-price.
v_line_tot = wa_final-line_total.
v_inv_tot = wa_final-inv_total.
WRITE: wa_final-inv_date TO lv_date.
CONCATENATE lv_string
wa_final-vendor_num gc_tab
wa_final-pay_site gc_tab
wa_final-inv_number gc_tab
wa_final-inv_type gc_tab
lv_date gc_tab
* wa_final-inv_date
wa_final-inv_descr gc_tab
wa_final-line_type gc_tab
wa_final-line_descr gc_tab
wa_final-po_number gc_tab
wa_final-item gc_tab
wa_final-suppli_uom gc_tab
* wa_final-quantity
* wa_final-price
* wa_final-line_total
* wa_final-inv_total
v_quan gc_tab
v_price gc_tab
v_line_tot gc_tab
v_inv_tot gc_crlf
INTO lv_string.
CLEAR: v_quan,v_price,v_line_tot,v_inv_tot,lv_date.
ENDLOOP.
* --------------------------------------------------------------
* convert the text string into UTF-16LE binary data including
* byte-order-mark. Mircosoft Excel prefers these settings
* all this is done by new class cl_bcs_convert (see note 1151257)
TRY.
cl_bcs_convert=>string_to_solix(
EXPORTING
iv_string = lv_string
iv_codepage = '4103' "suitable for MS Excel, leave empty
iv_add_bom = 'X' "for other doc types
IMPORTING
et_solix = binary_content
ev_size = size ).
CATCH cx_bcs.
MESSAGE e445(so).
ENDTRY.
ENDFORM. "create_content
*&---------------------------------------------------------------------*
*& Form send
*&---------------------------------------------------------------------*
FORM send.
DATA: l_date(10) TYPE c,
l_subject(100) TYPE c.
DATA: mailto TYPE ad_smtpadr.
TRY.
* -------- create persistent send request ------------------------
send_request = cl_bcs=>create_persistent( ).
* -------- create and set document with attachment ---------------
* create document object from internal table with text
WRITE: sy-datum TO l_date MM/DD/YYYY.
CONCATENATE 'Invoices' l_date INTO l_subject SEPARATED BY space.
APPEND l_subject TO main_text.
document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = main_text
i_subject = 'Interface' ). "#EC NOTEXT
* add the spread sheet as attachment to document object
document->add_attachment(
i_attachment_type = 'XLS' "#EC NOTEXT
i_attachment_subject = ' Invoices' "#EC NOTEXT
i_attachment_size = size
i_att_content_hex = binary_content ).
* add document object to send request
send_request->set_document( document ).
* --------- add recipient (e-mail address) -----------------------
* create recipient object
recipient = cl_cam_address_bcs=>create_internet_address( rafim.xxx@gmail.com )
* add recipient object to send request
send_request->add_recipient( recipient ).
* ---------- send document ---------------------------------------
sent_to_all = send_request->send( i_with_error_screen = 'X' ).
COMMIT WORK.
IF sent_to_all IS INITIAL.
MESSAGE i500(sbcoms) WITH mailto.
ELSE.
MESSAGE s022(so).
ENDIF.
* ------------ exception handling ----------------------------------
CATCH cx_bcs INTO bcs_exception.
MESSAGE i865(so) WITH bcs_exception->error_type.
ENDTRY.
ENDFORM. "send
Regards,
Rafi