发送内容 的数据获取:
-
正文部分使用cl_document_bcs=>create_document静态方法实现
-
传入参数为html内表结构
CLEAR lo_document . lo_document = cl_document_bcs=>create_document( i_type = 'HTM' i_text = lt_html i_length = conlengths i_subject = lv_subject ).
-
添加附件使用lo_document->add_attachment
-
传入参数为二进制数据(Excel的制作使用ZXLSX_WORKBENCH方式,百度搜ZXLSX_WORKBENCH就可以了)
CALL METHOD lo_document->add_attachment EXPORTING i_attachment_type = 'BIN' i_attachment_subject = lv_att_subject i_attachment_size = lv_att_size i_att_content_hex = lt_doc_table.
示例代码:
DATA: lv_subject TYPE so_obj_des,
lo_send_request TYPE REF TO cl_bcs,
lo_document TYPE REF TO cl_document_bcs,
conlengths TYPE so_obj_len,
bcs_exception TYPE REF TO cx_bcs,
bcs_message TYPE string,
lt_html TYPE STANDARD TABLE OF w3html, "存放HTML5代码的内表
lw_html TYPE w3html, "工作区
lo_sender TYPE REF TO cl_sapuser_bcs, "发送人
lv_to TYPE adr6-smtp_addr, "接收人
lo_bcs_to TYPE REF TO if_recipient_bcs,
lv_cc TYPE adr6-smtp_addr, "抄送人
lo_bcs_cc TYPE REF TO if_recipient_bcs,
lv_rawdata_01 TYPE mime_data,
lv_doc_size TYPE i, "文件大小
lt_doc_table TYPE solix_tab, "文件内容
lv_att_size TYPE so_obj_len, "附件长度
lv_att_subject TYPE so_obj_des, "附件名称
lv_result TYPE os_boolean, "发送结果
lo_fail TYPE REF TO cx_bcs. "异常信息
DEFINE %%add_html.
lw_html-line = &1.
APPEND lw_html TO lt_html.
CLEAR lw_html.
END-OF-DEFINITION.
SELECT * INTO TABLE @DATA(lt_emp) FROM ztlbmm_emp_email.
** 邮件主题
lv_subject = '大仓多库位库存预警表'.
** 邮件正文
%%add_html:'<html>',
'<head>',
'<meta charset="utf-8" />',
'<title></title>',
'<style>table {text-align: center;}</style>',
'</head>',
'<body>',
'<h3> 为避免单个SKU存放多库位交货时,产品不能按批次先进先出原则,导致存在呆滞风险,',
'烦请仓库重点关注对产品库位进行管控;单个SKU多库位存放的成品预警明细见附件:</h3>',
'<table border="1" width="550px" height="150px" cellspacing="0" align="center">',
'<thead><tr>',
'<th>装运点</th>',
'<th>数据获取时间</th>',
'<th>SKU总数</th>',
'<th>多库位SKU总数</th>',
'<th>多库位SKU占比</th>',
'<th>增长率</th>',
'</tr></thead>'.
LOOP AT lt_email INTO lw_email. "存放表格数据
%%add_html: '<tbody><tr>',
'<td>', lw_email-name1, '</td>',
'<td>', lw_email-zdate, '</td>',
'<td>', lw_email-skun1, '</td>',
'<td>', lw_email-skun2, '</td>',
'<td>', lw_email-zcent, '</td>',
'<td>', lw_email-zupsn, '</td>',
'</tr></tbody>'.
CLEAR:lw_email.
ENDLOOP.
%%add_html: '</table>',
'</body>',
'</html>'.
"开源项目ZXLSX_WORKBENCH使用方式
CALL FUNCTION 'ZXLWB_CALLFORM'
EXPORTING
iv_formname = 'ZMMR05B_TEMP' "Excel 模板
iv_context_ref = lt_excel[] "Excel 映射内表 会自动填充到模板
iv_viewer_suppress = 'X'
IMPORTING
ev_document_rawdata = lv_rawdata_01 "把EXCEL 转换为String类型的字符串
EXCEPTIONS
OTHERS = 2.
"附件部分
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_rawdata_01 "传入ZXLSX_WORKBENCH处理的Excel字符串
IMPORTING
output_length = lv_doc_size
TABLES
binary_tab = lt_doc_table. "传出二进制内容
"附件长度
lv_att_size = lv_doc_size.
"附件名称
lv_att_subject = |{ sy-datum }_多库位预警报表.xlsx|.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
RETURN.
ENDIF.
" 开始发送流程
TRY.
"正文部分
CLEAR lo_document .
lo_document = cl_document_bcs=>create_document(
i_type = 'HTM' "正文选择HTML格式显示
i_text = lt_html "显示的内容
i_length = conlengths
i_subject = lv_subject ). "邮件主题
"添加附件
CALL METHOD lo_document->add_attachment
EXPORTING
i_attachment_type = 'BIN'
i_attachment_subject = lv_att_subject "附件主题
i_attachment_size = lv_att_size
i_att_content_hex = lt_doc_table. "附件内容
"创建传输请求对象
CLEAR lo_send_request.
lo_send_request = cl_bcs=>create_persistent( ).
"将文件对象放进传输请求对象
CALL METHOD lo_send_request->set_document( lo_document ).
" 发件人:前提是这个邮箱地址能发邮件,并且不需要密码
lo_sender = cl_sapuser_bcs=>create( sy-uname ).
lo_send_request->set_sender( lo_sender ).
" 收件人
lv_to = '这里填收件人邮箱'
lo_bcs_to = cl_cam_address_bcs=>create_internet_address( lv_to ).
CALL METHOD lo_send_request->add_recipient
EXPORTING
i_recipient = lo_bcs_to.
" 抄送人
lv_cc = '这里填抄送人邮箱'.
lo_bcs_cc = cl_cam_address_bcs=>create_internet_address( lv_cc ).
CALL METHOD lo_send_request->add_recipient
EXPORTING
i_recipient = lo_bcs_cc
i_copy = 'X'.
"设置立即发送
lo_send_request->set_send_immediately( 'X' ).
"与outbox关联
lo_send_request->send_request->set_link_to_outbox( 'X' ).
"发送邮件
CALL METHOD lo_send_request->send(
EXPORTING
i_with_error_screen = 'X'
RECEIVING
result = lv_result ).
CATCH cx_bcs INTO bcs_exception.
bcs_message = bcs_exception->get_text( ).
MESSAGE bcs_exception TYPE 'E'.
EXIT.
ENDTRY.
IF lv_result EQ 'X'.
COMMIT WORK AND WAIT. "提交事务
ELSE.
ROLLBACK WORK. "事务回滚
ENDIF.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
RETURN.
ENDIF.