您的位置:首页 > 大数据 > 人工智能

SAP 中发送e-Mail的方法,如何发送附件?如何在附件中显示中文?

2012-09-18 15:48 274 查看
经多次研究和测试,终于可以正常发送所有附件,且可实际中文字符的正常显示。

关键代码,请参看红色字体部分:

*&---------------------------------------------------------------------*

*& Form SEND_EMAIL_WITH_ATTACHMENT

*&---------------------------------------------------------------------*

FORM SEND_EMAIL_WITH_ATTACHMENT .

data: binary_content type solix_tab.

data: xl_content type xstring .

constants:

con_tab type c value cl_abap_char_utilities=>horizontal_tab,

con_cret type c value cl_abap_char_utilities=>cr_lf.

data: xlcontent type xstring,

conlength type i,

conlengths type so_obj_len,

result_content type string,

wa_string type string, "This holds all of your data

wa_str_temp type string,

dummy type string.

data: send_request type ref to cl_bcs.

data: text type bcsy_text.

data: document type ref to cl_document_bcs.

data: sender type ref to if_sender_bcs.

data: recipient type ref to if_recipient_bcs.

data: recipients type bcsy_smtpa.

data: bcs_exception type ref to cx_bcs.

data: sent_to_all type os_boolean.

data: e_r_page type ref to cl_rsr_www_page.

data: content_length type w3param-cont_len ,

content_type type w3param-cont_type,

return_code type w3param-ret_code .

data: html type standard table of w3html .

data: server type string ,

port type string .

data: wa_rec type ad_smtpadr .

data: bcs_message type string .

data: subject type so_obj_des.

data: sender_id type ad_smtpadr.

data: email type ad_smtpadr.

data: gv_file type string,

gv_zipfilehex type xstring,

go_zipper type ref to cl_abap_zip. "The zip folder object

data: lp_size type so_obj_len,

lv_i type i.

refresh binary_content .

clear wa_string.

clear: lp_size, lv_i.

loop at it_html.

clear: xl_content .

* convert the string data to xstring

call function 'SCMS_STRING_TO_XSTRING'

exporting

text = it_html-line

MIMETYPE = 'DAT'

ENCODING = '4110' " 4103 NG, 8400 NG, 4110 is OK

importing

buffer = xl_content

EXCEPTIONS

FAILED = 1

OTHERS = 2.

call function 'SCMS_XSTRING_TO_BINARY'

exporting

buffer = xl_content

append_to_table = 'X'

importing

OUTPUT_LENGTH = lv_i

tables

binary_tab = binary_content.

add lv_i to lp_size.

endloop.

lp_size = lp_size * 6.

*"Title

subject = p_subj .

sender_id = p_sender.

clear P_att_name.

concatenate wa_header-reinr '.htm' into p_att_name.

* refresh text .

clear result_content .

* The body message

loop at zmess.

if result_content is initial.

concatenate zmess-zline '<br>' into result_content.

else.

concatenate result_content zmess-zline '<br>' into result_content.

endif.

endloop.

conlength = strlen( result_content ) .

conlengths = conlength .

call function 'SCMS_STRING_TO_FTEXT'

exporting

text = result_content

tables

ftext_tab = text.

try.

clear send_request .

send_request = cl_bcs=>create_persistent( ).

clear document .

document = cl_document_bcs=>create_document(

i_type = 'HTM'

i_text = text

i_length = conlengths

i_subject = subject ).

call method document->add_attachment

exporting

i_attachment_type = 'htm'

i_attachment_subject = p_att_name "'Trip.htm' "Attachment name

i_att_content_hex = binary_content.

* add document to send request

call method send_request->set_document( document ).

clear sender .

sender = cl_cam_address_bcs=>create_internet_address( sender_id ).

call method send_request->set_sender

exporting

i_sender = sender.

clear wa_rec .

clear recipient .

wa_rec = p_receip.

recipient = CL_CAM_ADDRESS_BCS=>create_internet_address(

wa_rec ).

call method send_request->add_recipient

exporting

i_recipient = recipient.

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'.

* APPEND 'Mail sent successfully ' TO return .

endif.

commit work.

catch cx_bcs into bcs_exception.

bcs_message = bcs_exception->get_text( ).

* APPEND bcs_message TO return .

exit.

endtry.

ENDFORM. " SEND_EMAIL_WITH_ATTACHMENT
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: