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>
示例:
"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: cl_exception TYPE REF TO CX_BCS. DATA: if_recipient TYPE REF TO IF_RECIPIENT_BCS. DATA: t_solix TYPE SOLIX_TAB. DATA: wa_solix LIKE SOLIX. DATA: attachment_type TYPE so_obj_tp VALUE 'XLS'. "内容 DATA:TEXT_L TYPE STRING. DATA:MAIN_TEXT TYPE BCSY_TEXT. "收件人 DATA:MAILTO TYPE AD_SMTPADR. "主题 DATA:TITLE TYPE SO_OBJ_DES. "发件人 DATA:MAIL_SENDER_LR TYPE REF TO IF_SENDER_BCS. DATA:SENDER LIKE ADR6-SMTP_ADDR. "返回结果 DATA:result TYPE os_boolean. "内表数据 DATA:temp_spfli LIKE TABLE OF spfli WITH HEADER LINE. "创建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_renderer TYPE REF TO IF_IXML_RENDERER. "render返回值 DATA:l_rc TYPE I. "文档大小 DATA:l_xml_size 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_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. START-OF-SELECTION. PERFORM get_data. PERFORM creat_xml_content. PERFORM create_content. PERFORM send_mail. END-OF-SELECTION. FORM get_data. SELECT * INTO CORRESPONDING FIELDS OF TABLE temp_spfli FROM spfli WHERE carrid = 'AA'. ENDFORM. FORM create_content. TEXT_L = '<p><span style="font-size:14px;">Dear All:</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="1px solid black">'. APPEND TEXT_L TO MAIN_TEXT. TEXT_L = '<tbody><tr><td>mandt</td><td>carrid</td><td>connid</td><td>countryfr</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></tr>'. APPEND TEXT_L TO MAIN_TEXT. ENDLOOP. TEXT_L = '</tbody></table>'. APPEND TEXT_L TO MAIN_TEXT. ENDFORM. FORM send_mail. TITLE = 'Mail Test'. MAILTO = '[email protected]'. SENDER = '[email protected]'. TRY. "创建发送请求 cl_send_mail = CL_BCS=>CREATE_PERSISTENT( ). "创建整理发送内容 cl_content = CL_DOCUMENT_BCS=>CREATE_DOCUMENT( I_TYPE = 'HTM' I_TEXT = MAIN_TEXT I_SUBJECT = TITLE ). "添加xml表格 cl_content->add_attachment( i_attachment_type = attachment_type i_attachment_subject = 'SpreadSheet.xls' i_att_content_hex = t_solix ). "添加邮件内容到发送请求 cl_send_mail->SET_DOCUMENT( cl_content ). "设置发件人 MAIL_SENDER_LR = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( SENDER ). cl_send_mail->SET_SENDER( I_SENDER = MAIL_SENDER_LR ). "设置收件人,可以设置多次,发送多人 if_recipient = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( MAILTO ). "添加邮件地址到发送请求 cl_send_mail->ADD_RECIPIENT( if_recipient ). "设置状态 cl_send_mail->SET_STATUS_ATTRIBUTES( I_REQUESTED_STATUS = 'E' I_STATUS_MAIL = 'E' ). "设置立即发送 cl_send_mail->SET_SEND_IMMEDIATELY( 'X' ). "正式发送并提交作业 result = cl_send_mail->SEND( I_WITH_ERROR_SCREEN = 'X' ). COMMIT WORK AND WAIT. IF result is INITIAL. message s000 with 'send mail failed!'. ENDIF. CATCH CX_BCS INTO cl_exception. MESSAGE s000 WITH cl_exception->error_type. ENDTRY. ENDFORM. FORM creat_xml_content. "创建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 temp_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 = temp_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 = temp_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 = temp_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 = temp_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. "保存文件 l_stream_fac = l_ixml->create_stream_factory( ). 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. LOOP AT l_xml_table INTO wa_xml_line. wa_solix-line = wa_xml_line-val. APPEND wa_solix TO t_solix. ENDLOOP. ENDFORM. "save_xml
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 = '[email protected]'. "郵件地址 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.