背景:
解决:
参考outlook中带图片的邮件,有两种形式,一种为url链接,需要点击下载才展示图片,一种为直接显示;如果使用ABAP发送该类型的邮件,则需要发送HTML格式邮件,参考HTML展示图片,有如下三种实现方式
实现方式一
实现效果
实现方式
代码示例:
DATA: lt_content TYPE soli_tab, "Email Contentlv_subject TYPE so_obj_des, "Email Subjectls_return TYPE bapiret2,lv_sender TYPE ad_smtpadr,ls_content TYPE soli,lt_accept TYPE bcsy_smtpa,lt_ccaccept TYPE bcsy_smtpa,lv_accept TYPE ad_smtpadr,lt_attachment TYPE tfpattachments.DATA: lv_url TYPE string VALUE 'https://XXX/sap/public/bc/abap/mime_demo/header.jpg?sap-client=260'.lv_accept = 'tutu@xie.com'.
APPEND lv_accept TO lt_accept.lv_subject = |测试图片|.lv_sender = 'fireworsk@xie.com'.CLEAR ls_content.
ls_content-line = |<img src="&URL&" alt="image" /><br/>|.
REPLACE '&URL&' WITH lv_url INTO ls_content.
APPEND ls_content TO lt_content.CLEAR ls_content.
APPEND ls_content TO lt_content.ls_content-line = '您好,'.
APPEND ls_content TO lt_content.*--------------------------------------------------------------------*
" 邮件发送
*--------------------------------------------------------------------*
zcl_tool=>t_email(EXPORTINGpi_type = 'HTM'pi_subject = lv_subjectpi_sender = lv_senderpi_sender_name = '测试'pt_accept = lt_acceptpt_ccaccept = lt_ccacceptpt_content = lt_contentpt_attachment = lt_attachmentIMPORTINGps_return = ls_return
).
实现方式二
实现效果
该实现方式在outlook中如上图,但是在移动端可能会出现图片附件或者图片未加载图标。
代码示例
DATA: lt_content TYPE soli_tab, "Email Contentlv_subject TYPE so_obj_des, "Email Subjectls_return TYPE bapiret2,lv_sender TYPE ad_smtpadr,ls_content TYPE soli,lt_accept TYPE bcsy_smtpa,lt_ccaccept TYPE bcsy_smtpa,lv_accept TYPE ad_smtpadr,lt_attachment TYPE tfpattachments.lv_accept = 'tutu@xie.com'.
APPEND lv_accept TO lt_accept.lv_subject = |测试图片|.lv_sender = 'fireworsk@xie.com'.CLEAR ls_content.
ls_content-line = |<img src="cid:logo.jpg" alt="image" /><br/>|.
APPEND ls_content TO lt_content.CLEAR ls_content.
APPEND ls_content TO lt_content.ls_content-line = '您好,'.
APPEND ls_content TO lt_content.cl_mime_repository_api=>get_api( )->get( EXPORTING i_url = `/sap/public/ydemo/header.jpg` IMPORTING e_content = DATA(lv_imgx) ).lt_attachment = VALUE #( ( filename = 'logo' mimetype = 'jpg' data = lv_imgx ) ).*--------------------------------------------------------------------*
" 邮件发送
*--------------------------------------------------------------------*
zcl_tool=>t_email(EXPORTINGpi_type = 'HTM'pi_subject = lv_subjectpi_sender = lv_senderpi_sender_name = '测试'pt_accept = lt_acceptpt_ccaccept = lt_ccacceptpt_content = lt_contentpt_attachment = lt_attachmentIMPORTINGps_return = ls_return
).
实现方式三
实现效果
outlook中展示和实现方式二一致,该方式为移动端和PC端均合适的实现方式
代码示例
DATA: lt_content TYPE soli_tab,ls_content TYPE soli,lt_attach_header TYPE soli_tab,ls_attach_header TYPE soli,lt_att_content_hex TYPE solix_tab,lv_outlength TYPE i,lv_attch_size TYPE so_obj_len,lv_content_size TYPE so_obj_len,lv_send_return TYPE os_boolean.DATA: lo_send TYPE REF TO cl_bcs,lo_document TYPE REF TO cl_document_bcs,lo_sender TYPE REF TO cl_cam_address_bcs,lo_accept TYPE REF TO cl_cam_address_bcs,lo_ccaccept TYPE REF TO cl_cam_address_bcs,lo_sap_user TYPE REF TO cl_sapuser_bcs,lo_bcs TYPE REF TO cx_bcs.DATA: lv_subject TYPE so_obj_des,lv_sender TYPE adr6-smtp_addr,lv_address_name TYPE adr6-smtp_addr.DATA:lo_cx_send_req_bcs TYPE REF TO cx_send_req_bcs,lo_cx_address_bcs TYPE REF TO cx_address_bcs.DATA: lv_accept TYPE ad_smtpadr,lv_message TYPE bapiret2-message.DATA: ls_attachment TYPE sfpattachments,lv_doctype TYPE so_obj_tp,lv_attsubject TYPE so_obj_des.DATA: lo_mime TYPE REF TO cl_gbt_multirelated_service.lv_subject = |测试图片|.lv_sender = 'tutu@xie.com'.
lv_address_name = '测试'.lv_accept = 'fireworsk@xie.com'.CLEAR ls_content.
ls_content-line = |<img src="cid:logo.jpg" alt="image" /><br/>|.
APPEND ls_content TO lt_content.CLEAR ls_content.
APPEND ls_content TO lt_content.ls_content-line = '您好,'.
APPEND ls_content TO lt_content.CREATE OBJECT lo_mime.
cl_mime_repository_api=>get_api( )->get( EXPORTING i_url = `/sap/public/ydemo/header.jpg` IMPORTING e_content = DATA(lv_imgx) ).
DATA(lt_solix) = cl_bcs_convert=>xstring_to_solix( lv_imgx ).
DATA(lv_len) = CONV so_obj_len( xstrlen( lv_imgx ) ).
CALL METHOD lo_mime->add_binary_partEXPORTINGcontent = lt_solixfilename = 'logo.jpg'extension = 'jpg'content_type = 'image/jpg'length = lv_lencontent_id = 'logo.jpg'.CALL METHOD lo_mime->set_main_htmlEXPORTINGcontent = lt_content
* filename =
* description =.*&------Create Send Request
TRY.CALL METHOD cl_bcs=>create_persistentRECEIVINGresult = lo_send.CATCH cx_send_req_bcs INTO lo_cx_send_req_bcs.ENDTRY.
IF lo_cx_send_req_bcs IS NOT INITIAL.lv_message = lo_cx_send_req_bcs->get_text( ).RETURN.
ENDIF.*&------Get The Sender Email Adress
TRY.CALL METHOD cl_cam_address_bcs=>create_internet_addressEXPORTINGi_address_string = lv_senderi_address_name = lv_address_name
* i_incl_sapuser =RECEIVINGresult = lo_sender.*&---------Set SenderCALL METHOD lo_send->set_senderEXPORTINGi_sender = lo_sender.CATCH cx_address_bcs INTO lo_cx_address_bcs.CATCH cx_send_req_bcs INTO lo_cx_send_req_bcs.
ENDTRY." Error Deal
IF lo_cx_address_bcs IS NOT INITIAL.lv_message = lo_cx_address_bcs->get_text( ).RETURN.
ENDIF.*&------Create Send Document Content
TRY.lo_document = cl_document_bcs=>create_from_multirelated(i_subject = lv_subjecti_multirel_service = lo_mime ).CATCH cx_document_bcs INTO lo_bcs.
ENDTRY.IF lo_bcs IS NOT INITIAL.lv_message = lo_bcs->get_text( ).RETURN.
ENDIF.*&------Set Send Document For Send Request
TRY.CALL METHOD lo_send->set_documentEXPORTINGi_document = lo_document.CATCH cx_send_req_bcs INTO lo_bcs.
ENDTRY.
IF lo_bcs IS NOT INITIAL.lv_message = lo_bcs->get_text( ).RETURN.
ENDIF.*&------Add Accept For Email
TRY.CLEAR lo_accept.CALL METHOD cl_cam_address_bcs=>create_internet_addressEXPORTINGi_address_string = lv_accept
* i_address_name =
* i_incl_sapuser =RECEIVINGresult = lo_accept.CALL METHOD lo_send->add_recipientEXPORTINGi_recipient = lo_accepti_express = 'X'i_copy = ''i_blind_copy = ''i_no_forward = ''.CATCH cx_send_req_bcs INTO lo_bcs.lv_message = lo_bcs->get_text( ).CATCH cx_address_bcs INTO lo_bcs.lv_message = lo_bcs->get_text( ).
ENDTRY.TRY.CALL METHOD lo_send->set_send_immediatelyEXPORTINGi_send_immediately = 'X'.CATCH cx_send_req_bcs INTO lo_bcs.lv_message = lo_bcs->get_text( ).RETURN.
ENDTRY.*&------Send Email
TRY.CALL METHOD lo_send->sendEXPORTINGi_with_error_screen = spaceRECEIVINGresult = lv_send_return.CATCH cx_send_req_bcs INTO lo_bcs.lv_message = lo_bcs->get_text( ).RETURN.
ENDTRY.COMMIT WORK AND WAIT.