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
关键代码,请参看红色字体部分:
*&---------------------------------------------------------------------*
*& 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
相关文章推荐
- 关于javaMail的发送邮件之简便方法(邮件带有附件及html内容中携带图标)
- mail发送的内容显示为附件的解决办法
- commons-mail发送带附件,显示图片的邮件(图片保存在邮件服务器)
- linux bash下通过mailx发送中文内容显示为附件的解决
- Flask-email 发送邮件的配置,发送附件的方法,以及os.environ.get('MAIL_USERNAME')为None的解决办法
- SpringBoot mail中文附件乱码的解决方法
- 【JavaMail开发】使javamail-1.4.2 发送邮件时中文附件乱码或显示"??"的解决办法
- VB.NET如何实现发送mail 给多个收件者 并带附件
- 发送的附件收取后为何变为winmail.dat 如何打开?
- Winxp系统下部分E-mail附件无法显示文件名的解决方法
- mail邮件发送添加附件的方法
- MFMailComposeViewController显示中文的方法
- C# MailMessage Attachment 中文名附件发邮件-Firefox中文显示正常,网页打开邮件附件中文名乱码
- 在文字模式下用mail如何将文件以附件方式发送
- 如何阻止通过Outlook用户发送WORD或EXCEL变成带Winmail.dat文件附件的邮件
- 如何解决HTML网页中文显示乱码的方法
- 如何将MFMailComposeViewController的发送邮件界面设为中文
- 如何阻止通过Outlook用户发送带Winmail.dat文件附件的邮件
- C# MailMessage Attachment 中文名附件发邮件-Firefox中文显示正常,网页打开邮件附件中文名乱码
- Flask-Mail邮件的配置以及发送附件的方法