ABAP学习(15):邮件发送实例
ABAP邮件
使用ABAP程式可以发送外部邮件。
Tcode:SOST可以查看邮件发送队列以及邮件发送状态。
1、使用类对象发送邮件
类CL_BCS:发送邮件功能类,创建发送邮件请求,添加发送人,添加收件人,添加发送内容等;
类CL_DOCUMENT_BCS:邮件内容类;
类CX_BCS:发送异常类;
接口IF_RECIPIENT_BCS:邮件地址接口;
邮件发送过程:
1、使用CL_BCS=>CREATE_PERSISTENT( )创建邮件发送请求,cl_send_mail保存对象;
2、使用CL_DOCUMENT_BCS=>CREATE_DOCUMENT( ),创建发送内容对象,cl_content保存类对象;
3、添加附件,使用cl_content->add_attachment,调用内容对象方法add_attachment;
4、将发送邮件内容添加到请求对象,cl_send_mail的SET_DOCUMENT方法;
5、调用发送请求的SET_SENDER方法设置发件人;
6、调用发送请求对象的ADD_RECIPIENT方法设置收件人;
7、调用发送请求对象的SEND方法发送邮件;
附件生成:
使用XML文本,最后保存为excel,获得excel类型附件。实际上就是构建一个EXCEL的XML文档。可以将一个EXCEL保存成XML格式文件,就可以看到我们需要构建的XML文档样子。
1、使用cl_xml=>create( ),创建IF_IXML类对象,XML工厂;
2、使用XML工厂生成文档模型对象,使用create_document( )方法;
3、使用create_simple_element,创建各种子节点;
4、使用create_namespace_decl,生成namespace;
5、使用set_attribute,设置节点属性值;
6、使用set_attribute_node,为节点设置属性节点;
Excel样式:
对应XML样式:其中以下节点是必须要生成的
1、生成Workbook节点;
2、生成Worksheet节点;
3、生成Table节点;
4、生成Row节点;
5、生成Cell节点;
6、生成Data节点;
EXCEL对应的XML文档,红色部分是必须生成节点。
<?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <Created>2015-06-05T18:19:34Z</Created> <LastSaved>2019-11-04T03:35:18Z</LastSaved> <Version>16.00</Version> </DocumentProperties> <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> <AllowPNG/> <RemovePersonalInformation/> </OfficeDocumentSettings> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <WindowHeight>12645</WindowHeight> <WindowWidth>22260</WindowWidth> <WindowTopX>0</WindowTopX> <WindowTopY>0</WindowTopY> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> <Styles> <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom"/> <Borders/> <Font ss:FontName="新細明體" x:CharSet="136" x:Family="Roman" ss:Size="11" ss:Color="#000000"/> <Interior/> <NumberFormat/> <Protection/> </Style> </Styles> <Worksheet ss:Name="工作表1"> <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15.75"> <Row> <Cell><Data ss:Type="String">col1</Data></Cell> <Cell><Data ss:Type="String">col2</Data></Cell> </Row> <Row> <Cell><Data ss:Type="Number">1</Data></Cell> <Cell><Data ss:Type="Number">2</Data></Cell> </Row> </Table> <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> <PageSetup> <Header x:Margin="0.3"/> <Footer x:Margin="0.3"/> <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/> </PageSetup> <Print> <ValidPrinterInfo/> <PaperSizeIndex>9</PaperSizeIndex> <HorizontalResolution>600</HorizontalResolution> <VerticalResolution>600</VerticalResolution> </Print> <Selected/> <Panes> <Pane> <Number>3</Number> <ActiveRow>2</ActiveRow> <ActiveCol>2</ActiveCol> </Pane> </Panes> <ProtectObjects>False</ProtectObjects> <ProtectScenarios>False</ProtectScenarios> </WorksheetOptions>
</Worksheet> </Workbook>
步骤0:定义全局参数
"内表数据 DATA:lt_spfli LIKE TABLE OF spfli. DATA:ls_spfli LIKE LINE OF lt_spfli. "正文内容 DATA:lv_text TYPE string. DATA:lt_main_text TYPE soli_tab. "html轉換string DATA:lv_html_str TYPE string. "附件內容 DATA:t_solix TYPE solix_tab. DATA:wa_solix TYPE solix. "附件内容字节长度 DATA:lv_bytecount TYPE sood-objlen.
步骤1:获取要发送表格数据,保存到内表 lt_spfli
FORM get_data. SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_spfli FROM spfli WHERE carrid = 'AA'. ENDFORM.
步骤2:利用xml形式创建excel附件,这种方式有奇怪xls,xlsx兼容问题,将内容保存到t_solix内表
"創建xml附件 FORM creat_xml_content. "创建xml表格 DATA:l_ixml TYPE REF TO IF_IXML. DATA:l_document TYPE REF TO IF_IXML_DOCUMENT. "流容器工厂 DATA:l_stream_fac TYPE REF TO IF_IXML_STREAM_FACTORY. "流容器 DATA:l_ostream TYPE REF TO IF_IXML_OSTREAM. DATA:l_istream TYPE REF TO IF_IXML_ISTREAM. DATA:l_renderer TYPE REF TO IF_IXML_RENDERER. DATA:l_rc TYPE i. TYPES:BEGIN OF xml_line, val(255) TYPE x, END OF xml_line. DATA:l_xml_table TYPE TABLE OF xml_line. DATA:wa_xml_line TYPE xml_line. DATA:l_xml_size TYPE i. "根节点 DATA:l_element_root TYPE REF TO IF_IXML_ELEMENT. DATA:ns_attribute TYPE REF TO IF_IXML_ATTRIBUTE. "properties DATA:l_element_properties TYPE REF TO IF_IXML_ELEMENT. "worksheet DATA:l_element_worksheet TYPE REF TO IF_IXML_ELEMENT. "table DATA:l_element_table TYPE REF TO IF_IXML_ELEMENT. "column节点 DATA:l_element_column TYPE REF TO IF_IXML_ELEMENT. "row节点 DATA:l_element_row TYPE REF TO IF_IXML_ELEMENT. "cell节点,单元格 DATA:l_element_cell TYPE REF TO IF_IXML_ELEMENT. "data节点,数据 DATA:l_element_data TYPE REF TO IF_IXML_ELEMENT. "值 DATA:l_value TYPE String. "xstring DATA:lv_xstring TYPE xstring. "创建ixml factory l_ixml = cl_ixml=>create( ). "创建文档模型 l_document = l_ixml->create_document( ). "创建root,worksheet节点 l_element_root = l_document->create_simple_element( name = 'Workbook' parent = l_document ). l_element_root->set_attribute( name = 'xmlns' value = 'urn:schemas-microsoft-com:office:spreadsheet' ). "设置xmlns value ns_attribute = l_document->create_namespace_decl( name = 'o' prefix = 'xmlns' uri = 'urn:schemas-microsoft-com:office:office' ). l_element_root->set_attribute_node( ns_attribute ). ns_attribute = l_document->create_namespace_decl( name = 'x' prefix = 'xmlns' uri = 'urn:schemas-microsoft-com:office:excel' ). l_element_root->set_attribute_node( ns_attribute ). ns_attribute = l_document->create_namespace_decl( name = 'ss' prefix = 'xmlns' uri = 'urn:schemas-microsoft-com:office:spreadsheet' ). l_element_root->set_attribute_node( ns_attribute ). "worksheet节点 l_element_worksheet = l_document->create_simple_element( name = 'Worksheet' parent = l_element_root ). l_element_worksheet->set_attribute_ns( name = 'Name' prefix = 'ss' value = 'Sheet1' ). "table节点 l_element_table = l_document->create_simple_element( name = 'Table' parent = l_element_worksheet ). l_element_table->set_attribute_ns( name = 'FullColumns' prefix = 'x' value = '1' ). l_element_table->set_attribute_ns( name = 'FullRows' prefix = 'x' value = '1' ). "创建列节点 * DO 4 TIMES. * l_element_column = l_document->create_simple_element( name = 'Column' parent = l_element_table ). * ENDDO. "创建表头行 l_element_row = l_document->create_simple_element( name = 'Row' parent = l_element_table ). l_element_row->set_attribute_ns( name = 'AutoFitHeight' prefix = 'ss' value = '1' ). "创建单元格,设置单元格内容及格式 l_element_cell = l_document->create_simple_element( name = 'Cell' parent = l_element_row ). l_element_data = l_document->create_simple_element( name = 'Data' value = 'mandt' parent = l_element_cell ). l_element_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). l_element_cell = l_document->create_simple_element( name = 'Cell' parent = l_element_row ). l_element_data = l_document->create_simple_element( name = 'Data' value = 'carrid' parent = l_element_cell ). l_element_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). l_element_cell = l_document->create_simple_element( name = 'Cell' parent = l_element_row ). l_element_data = l_document->create_simple_element( name = 'Data' value = 'connid' parent = l_element_cell ). l_element_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). l_element_cell = l_document->create_simple_element( name = 'Cell' parent = l_element_row ). l_element_data = l_document->create_simple_element( name = 'Data' value = 'countryfr' parent = l_element_cell ). l_element_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). "填充数据 LOOP AT lt_spfli INTO ls_spfli. "创建新行 l_element_row = l_document->create_simple_element( name = 'Row' parent = l_element_table ). "创建单元格 l_element_cell = l_document->create_simple_element( name = 'Cell' parent = l_element_row ). l_value = ls_spfli-mandt. l_element_data = l_document->create_simple_element( name = 'Data' value = l_value parent = l_element_cell ). l_element_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). l_element_cell = l_document->create_simple_element( name = 'Cell' parent = l_element_row ). l_value = ls_spfli-carrid. l_element_data = l_document->create_simple_element( name = 'Data' value = l_value parent = l_element_cell ). l_element_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). l_element_cell = l_document->create_simple_element( name = 'Cell' parent = l_element_row ). l_value = ls_spfli-connid. l_element_data = l_document->create_simple_element( name = 'Data' value = l_value parent = l_element_cell ). l_element_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). l_element_cell = l_document->create_simple_element( name = 'Cell' parent = l_element_row ). l_value = ls_spfli-countryfr. l_element_data = l_document->create_simple_element( name = 'Data' value = l_value parent = l_element_cell ). l_element_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). ENDLOOP. "保存文件CREATE_ISTREAM_XSTRING l_stream_fac = l_ixml->create_stream_factory( ). l_ostream = l_stream_fac->create_ostream_xstring( string = lv_xstring ). l_ostream = l_stream_fac->create_ostream_itable( table = l_xml_table ). l_renderer = l_ixml->create_renderer( ostream = l_ostream document = l_document ). l_rc = l_renderer->render( ). "保存xml文档 l_xml_size = l_ostream->get_num_written_raw( ). CALL METHOD cl_gui_frontend_services=>gui_download EXPORTING bin_filesize = l_xml_size filename = 'D:\temp\test.xml' filetype = 'BIN' CHANGING data_tab = l_xml_table. IF sy-subrc = 0. MESSAGE s000 WITH '保存成功!'. ENDIF. t_solix = cl_document_bcs=>xstring_to_solix( ip_xstring = lv_xstring ). lv_bytecount = xstrlen( lv_xstring ). ENDFORM. "save_xml
步骤2-1:使用zcl_excel创建excel附件,推荐使用这种方式,将内容保存到t_solix内表
"excel输出字段 TYPES:BEGIN OF s_fields, field TYPE string, des TYPE string, END OF s_fields. DATA:lt_fields TYPE TABLE OF s_fields. DATA:ls_fields LIKE LINE OF lt_fields. FORM create_fields. CLEAR lt_fields[]. ls_fields-field = 'CARRID'. ls_fields-des = '航班ID'. APPEND ls_fields TO lt_fields. ls_fields-field = 'CONNID'. ls_fields-des = 'CONNID'. APPEND ls_fields TO lt_fields. ENDFORM. "excel转换成xstring FORM translate_excel USING lo_excel TYPE REF TO zcl_excel. "下载显示excel转换 DATA:cl_writer TYPE REF TO zif_excel_writer. DATA:xdata TYPE xstring. "cl_writer对象 CREATE OBJECT cl_writer TYPE zcl_excel_writer_2007. xdata = cl_writer->write_file( lo_excel ). * "方式1:convert to binary * CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' * EXPORTING * buffer = xdata * IMPORTING * output_length = bytecount * TABLES * binary_tab = t_rawdata. "方式2:This method is only available on AS ABAP > 6.40 "excel转换 t_solix = cl_bcs_convert=>xstring_to_solix( iv_xstring = xdata ). "统计字节数 lv_bytecount = xstrlen( xdata ). ENDFORM. "使用excel表構建mail附件 FORM creat_excel_content. "excel文档类对象 DATA:lo_excel TYPE REF TO zcl_excel. "excel worksheet类对象 DATA:lo_worksheet TYPE REF TO zcl_excel_worksheet. DATA:lv_row TYPE I. DATA:lv_col TYPE I. "创建字段 PERFORM create_fields. TRY . "创建excel对象 CREATE OBJECT lo_excel. "获取当前获得worksheet lo_worksheet = lo_excel->get_active_worksheet( ). LOOP AT lt_spfli INTO ls_spfli. lv_row = sy-tabix. LOOP AT lt_fields INTO ls_fields. lv_col = sy-tabix. CASE ls_fields-field. WHEN 'CARRID'. "设置单元格 lo_worksheet->set_cell( ip_column = lv_col ip_row = lv_row ip_value = ls_spfli-carrid ). WHEN 'CONNID'. "设置单元格 lo_worksheet->set_cell( ip_column = lv_col ip_row = lv_row ip_value = ls_spfli-connid ). ENDCASE. ENDLOOP. ENDLOOP. "转换为xstring table PERFORM translate_excel USING lo_excel. CATCH ZCX_EXCEL. ENDTRY. ENDFORM.
步骤3:创建正文html内容,保存到lt_main_text内表
FORM create_content. lv_text = '<p><span style="font-size:14px;">Dear All:</span></p>'. APPEND lv_text TO lt_main_text. lv_html_str = lv_html_str && lv_text. lv_text = '<p><span style="font-size:14px;"> 显示报表如下:</span></p>'. APPEND lv_text TO lt_main_text. lv_html_str = lv_html_str && lv_text. lv_text = '<table border="1px solid black">'. APPEND lv_text TO lt_main_text. lv_html_str = lv_html_str && lv_text. lv_text = '<tbody><tr><td>mandt</td><td>carrid</td><td>connid</td><td>countryfr</td></tr>'. APPEND lv_text TO lt_main_text. lv_html_str = lv_html_str && lv_text. "表格內容 LOOP AT temp_spfli. lv_text = '<tr><td>' && temp_spfli-mandt && '</td>' && '<td>' && temp_spfli-carrid && '</td>' && '<td>' && temp_spfli-connid && '</td>' && '<td>' && temp_spfli-countryfr && '</td></tr>'. APPEND lv_text TO lt_main_text. lv_html_str = lv_html_str && lv_text. ENDLOOP. lv_text = '</tbody></table>'. APPEND lv_text TO lt_main_text. lv_html_str = lv_html_str && lv_text. ENDFORM.
步骤4:发送邮件
"發送邮件 FORM send_mail. "Tcode:SOST可以查看邮件发送情况 * 类: CL_BCS 发送邮件主要用到的功能类, 包括创建发送请求, 添加发送内容,添加发送地址, 到最终的发送指令发出. * 类: CL_DOCUMENT_BCS, 用来放置发送的内容. * 类: CX_BCS, 异常类, 用于捕捉发送邮件过程中出现的异常. * 接口: IF_RECIPIENT_BCS, 用来做邮件地址的存储转换. DATA:cl_send_mail TYPE REF TO CL_BCS. DATA:cl_content TYPE REF TO CL_DOCUMENT_BCS. DATA:lo_exception TYPE REF TO CX_BCS. DATA:lv_attachment_type TYPE so_obj_tp VALUE 'xls'. DATA:lt_attach_h TYPE soli_tab. DATA:ls_attach_h LIKE LINE OF lt_attach_h. "主题 DATA:lv_subject TYPE so_obj_des. DATA:lv_subject_long TYPE string. "附件名字,默認xls,支持附件xlsx DATA:lv_attach_subject TYPE sood-objdes VALUE 'SpreadSheet.xlsx'. "发件人,收件人 DATA:lo_recipient TYPE REF TO if_recipient_bcs. DATA:lo_sender TYPE REF TO if_sender_bcs. DATA:lv_sender TYPE adr6-smtp_addr. DATA:lv_mailto TYPE ad_smtpadr. "返回结果 DATA:lv_result TYPE os_boolean. lv_subject = 'Mail Test'. lv_subject_long = 'Mail Test This is a long subject long subject'. lv_mailto = 'xxxxx@xxx'. lv_sender = 'xxxxx@xxx'. TRY. "创建发送请求 cl_send_mail = cl_bcs=>create_persistent( ). "创建正文发送内容 cl_content = cl_document_bcs=>create_document( I_TYPE = 'HTM' I_TEXT = lt_main_text I_SUBJECT = lv_subject ). "創建正文的i_subject長度有限制,使用set_message_subject可以設置長subject cl_send_mail->set_message_subject( lv_subject_long ). "解決i_attachment_type三碼只能設置xls CONCATENATE '&SO_FILENAME=' lv_attach_subject INTO ls_attach_h. APPEND ls_attach_h TO lt_attach_h. "添加xml表格 cl_content->add_attachment( i_attachment_type = lv_attachment_type i_attachment_subject = lv_attach_subject i_att_content_hex = t_solix i_attachment_size = lv_bytecount i_attachment_header = lt_attach_h ). "添加邮件内容到发送请求 cl_send_mail->set_document( cl_content ). "设置发件人 lo_sender = cl_cam_address_bcs=>create_internet_address( lv_sender ). cl_send_mail->set_sender( i_sender = lo_sender ). "设置收件人,设置多个人,设置多次不同邮件地址即可 lo_recipient = cl_cam_address_bcs=>create_internet_address( lv_mailto ). "添加邮件地址到发送请求 cl_send_mail->add_recipient( EXPORTING i_recipient = lo_recipient ). "设置收件人,设置多个人,设置多次不同邮件地址即可 "添加邮件地址到发送CC请求 "cl_send_mail->add_recipient( EXPORTING i_recipient = lo_recipient i_copy = abap_true ). "添加邮件地址到发送BCC请求 "cl_send_mail->add_recipient( EXPORTING i_recipient = lo_recipient i_blind_copy = abap_true ). "设置状态 cl_send_mail->set_status_attributes( I_REQUESTED_STATUS = 'E' I_STATUS_MAIL = 'E' ). "设置立即发送 cl_send_mail->set_send_immediately( 'X' ). "正式发送并提交作业 lv_result = cl_send_mail->send( i_with_error_screen = 'X' ). COMMIT WORK AND WAIT. IF lv_result is INITIAL. message s000 with 'send mail failed!'. ENDIF. CATCH CX_BCS INTO lo_exception. MESSAGE s000 WITH lo_exception->error_type. ENDTRY. ENDFORM.
2、调用function发送邮件
使用function,‘SO_NEW_DOCUMENT_SEND_API1’发送邮件。
示例:
DATA:temp_spfli LIKE TABLE OF spfli WITH HEADER LINE. START-OF-SELECTION. PERFORM get_data. PERFORM send_mail. END-OF-SELECTION. FORM get_data. SELECT * INTO CORRESPONDING FIELDS OF TABLE temp_spfli FROM spfli WHERE carrid = 'AA'. ENDFORM. "send mail FORM send_mail. DATA: MAILSUBJECT TYPE SODOCCHGI1. "正文 DATA:MAIN_TEXT TYPE STANDARD TABLE OF SOLI WITH HEADER LINE. DATA:TEXT_L TYPE String. "收件人 DATA:T_RECEIVERS TYPE TABLE OF SOMLRECI1 WITH HEADER LINE. "设置邮件主题,描述 MAILSUBJECT-OBJ_NAME = 'SAPRPT'. MAILSUBJECT-OBJ_LANGU = SY-LANGU. MAILSUBJECT-SKIP_SCREN = 'X'. "直接发送,不弹出窗口 MAILSUBJECT-OBJ_DESCR = '測試郵件發送'. "設置收件人 CLEAR T_RECEIVERS. T_RECEIVERS-receiver = 'xxxxx@qq.com'. "郵件地址 T_RECEIVERS-rec_type = 'U'. T_RECEIVERS-com_type = 'INT'. T_RECEIVERS-notif_del = 'X'. T_RECEIVERS-notif_ndel = 'X'. APPEND T_RECEIVERS. "設置正文 TEXT_L = '<p><span style="font-size:14px;">Dear Sirs:</span></p>'. APPEND TEXT_L TO MAIN_TEXT. TEXT_L = '<p><span style="font-size:14px;">資料如下:</span></p>'. APPEND TEXT_L TO MAIN_TEXT. TEXT_L = '<table border="1">'. APPEND TEXT_L TO MAIN_TEXT. TEXT_L = '<tbody><tr><td>Mandt</td>' && '<td>Carrid</td>' && '<td>Connid</td>' && '<td>Countryfr</td>' && '<td>Cityfrom</td></tr>'. APPEND TEXT_L TO MAIN_TEXT. LOOP AT temp_spfli. TEXT_L = '<tr><td>' && temp_spfli-mandt && '</td>' && '<td>' && temp_spfli-carrid && '</td>' && '<td>' && temp_spfli-connid && '</td>' && '<td>' && temp_spfli-countryfr && '</td>' && '<td>' && temp_spfli-cityfrom && '</td>' && '</tr>'. APPEND TEXT_L TO MAIN_TEXT. ENDLOOP. TEXT_L = '</tbody></table>'. APPEND TEXT_L TO MAIN_TEXT. "发送邮件 CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1' EXPORTING DOCUMENT_DATA = MAILSUBJECT DOCUMENT_TYPE = 'HTM' COMMIT_WORK = 'X' TABLES OBJECT_CONTENT = MAIN_TEXT RECEIVERS = T_RECEIVERS. IF sy-subrc = 0. "立即发送,会将所有SOST,邮件任务队列中邮件发送 * SUBMIT rsconn01 WITH mode = 'INT'. ENDIF. ENDFORM.
本文来自博客园,作者:渔歌晚唱,转载请注明原文链接:https://www.cnblogs.com/tangToms/p/11991414.html