*&---------------------------------------------------------------------**& Report ZLM_EMAIL01*&---------------------------------------------------------------------**&*&---------------------------------------------------------------------*REPORTZLM_EMAIL01.PARAMETERSp_fileLIKErlgrap-filename OBLIGATORY.*&---------------------------------------------------------------------**& Event AT SELECTION-SCREEN ON*&---------------------------------------------------------------------*AT SELECTION-SCREEN ON VALUE-REQUEST FORp_file.PERFORMfrm_file_open_f4CHANGINGp_file.*&---------------------------------------------------------------------**& Event AT START-OF-SELECTION*&---------------------------------------------------------------------*START-OF-SELECTION.PERFORMfrm_send_mail.*&---------------------------------------------------------------------**& Form FRM_FILE_OPEN_F4*&---------------------------------------------------------------------** text*----------------------------------------------------------------------** LIKE LINE OFlit_file.l_file_filter='Excel Template (*.xlt)|*.xlt|Excel Workbook (*.xls)|*.xls|All files (*.*)|*.*'.CALL METHODcl_gui_frontend_services=>file_open_dialogEXPORTINGwindow_title='Choose template file ...'default_extension='*.*'"file_filter = l_file_filterCHANGINGfile_table=lit_file
rc=l_rcEXCEPTIONSfile_open_dialog_failed=1cntl_error=2error_no_gui=3not_supported_by_gui=4OTHERS=5.IFsy-subrc=0.READ TABLElit_fileASSIGNINGINDEX1.IFsy-subrc=0.file=-filename.ENDIF.ENDIF.ENDFORM.*&---------------------------------------------------------------------**& Form FRM_SEND_MAIL*&---------------------------------------------------------------------** text*----------------------------------------------------------------------** --> p1 text* gui_uploadEXPORTINGfilename=l_filen
filetype='BIN'"* has_field_separator = SPACE* header_length = 0* read_by_line = ''* dat_mode = SPACE* codepage = SPACE* ignore_cerr = ABAP_TRUE* replacement = '#'* virus_scan_profile =IMPORTINGfilelength=l_file_size* header =CHANGINGdata_tab=lit_mailhex* isscanperformed = SPACEEXCEPTIONSfile_open_error=1file_read_error=2no_batch=3gui_refuse_filetransfer=4invalid_type=5no_authority=6unknown_error=7bad_data_format=8header_not_allowed=9separator_not_allowed=10header_too_long=11unknown_dp_error=12access_denied=13dp_out_of_memory=14disk_full=15dp_timeout=16not_supported_by_gui=17error_no_gui=18OTHERS=19.IFsy-subrc <>0.* Implement suitable error handling hereRETURN.ENDIF.*附件长度,这个很重要,一定要有l_file_size_char=l_file_size.*邮件正文l_string='哈喽,地球人,点开附件有惊喜'.APPENDl_stringTOlit_contents.TRY.* CREATE THE DOCUMENT WITH CONTENTSCREATEOBJECT lo_document.lo_document=cl_document_bcs=>create_document(i_type='HTM'i_subject='一封来自火星的测试邮件'"邮件标题i_length=l_file_size_char
i_language=sy-langu
i_importance='1'i_text=lit_contents).* hard code附件为excell_subject='附件.xls'.*添加附件,可以添加多个附件CALL METHODlo_document->add_attachmentEXPORTINGi_attachment_type='BIN'i_attachment_subject=l_subject
i_attachment_size=l_file_size_char
i_att_content_hex=lit_mailhex.* CREATING PERSISTENT OBJECT WILL ALLOW YOU TO SET THE DOCUMENT IN THE MAILw_document=cl_bcs=>create_persistent( ).*发件人*前提是这个邮箱地址能发邮件,并且不需要密码lo_sender=cl_sapuser_bcs=>create(sy-uname).w_document->set_sender(lo_sender).*收件人l_to='mars@mars.com'.l_bcs_to=cl_cam_address_bcs=>create_internet_address(l_to).* Add recipient to send requestCALL METHODw_document->add_recipientEXPORTINGi_recipient=l_bcs_to.*抄送人l_cc='test@test.com'.l_bcs_cc=cl_cam_address_bcs=>create_internet_address(l_cc).CALL METHODw_document->add_recipientEXPORTINGi_recipient=l_bcs_cc
i_copy='X'." i_express = 'X'.* SEND THE MAILw_document->set_send_immediately('X')."设置立即发送w_document->send_request->set_link_to_outbox('X')."与outbox关联CALL METHODw_document->set_document(lo_document).CALL METHODw_document->send(EXPORTINGi_with_error_screen='X'RECEIVING
result=l_result).CATCHcx_bcsINTOlo_fail.ENDTRY.* YOU CAN VERIFY THE STATUS IN THE LIST, YOU CAN ALSO SUBMIT THIS AS A BACKGROUND JOB.IFl_result='X'.COMMIT WORK AND WAIT.MESSAGE'发送成功'TYPE'S'.ELSE.ROLLBACK WORK.MESSAGE'发送失败'TYPE'S'DISPLAYLIKE'E'.STOP.ENDIF.ENDFORM.