ABAP 发送邮件详解编程语言

1、发送邮件(带附件xls)

ABAP 发送邮件详解编程语言

ABAP 发送邮件详解编程语言

*&---------------------------------------------------------------------* 
*& Report  YWX_TEST11 
*& 
*&---------------------------------------------------------------------* 
*& 
*& 
*&---------------------------------------------------------------------* 
REPORT ywx_test11. 
START-OF-SELECTION. 
PERFORM frm_send_email. 
FORM frm_send_email . 
CONSTANTS: lc_objtp TYPE so_obj_tp VALUE 'xls'. 
DATA: lt_return    TYPE bapiret2_t, 
lv_string    TYPE string, 
lv_string1   TYPE string, 
lv_sender    TYPE ad_smtpadr, 
lt_mail_body TYPE bcsy_text, 
ls_mail_body TYPE soli, 
lv_psubject  TYPE so_obj_des, 
lt_receivers TYPE STANDARD TABLE OF uiys_iusr, 
ls_receivers LIKE LINE OF lt_receivers, 
lv_name      TYPE char20, 
lv_age       TYPE char20, 
lv_sex       TYPE char20, 
lv_weight    TYPE char20. 
DATA: lt_attachment TYPE rmps_t_post_content, 
ls_attachment TYPE rmps_post_content. 
DATA: lt_solix TYPE solix_tab, 
lv_size  TYPE so_obj_len. 
DATA: lv_date TYPE c LENGTH 20, 
lv_time TYPE c LENGTH 20. 
****ADD  BEGIN  BY 29.03.2021 08:39:41 
"发送MAIL的定义 
DATA: lo_send_request TYPE REF TO cl_bcs VALUE IS INITIAL, 
lo_recipient    TYPE REF TO if_recipient_bcs VALUE IS INITIAL, 
lo_document     TYPE REF TO cl_document_bcs VALUE IS INITIAL, 
lx_document_bcs TYPE REF TO cx_document_bcs VALUE IS INITIAL, 
lo_sender       TYPE REF TO if_sender_bcs VALUE IS INITIAL. 
DATA: l_send             TYPE adr6-smtp_addr, 
l_rec              TYPE  adr6-smtp_addr, 
lv_recipient_uid   TYPE uname, 
lv_recipient_mail  TYPE adr6-smtp_addr, 
attachment_subject TYPE so_obj_des, 
recipient_mail     TYPE adr6-smtp_addr, 
lv_result          TYPE boolean. 
****ADD  END    BY 29.03.2021 08:39:41 
"获取发信地址 
lv_sender = [email protected]'. 
"获取收件人 
ls_receivers-email = [email protected]'. 
APPEND ls_receivers TO lt_receivers. 
ls_receivers-email = [email protected]'. 
APPEND ls_receivers TO lt_receivers. 
"将数据转成string 
CONCATENATE 
'姓名' 
'年龄' 
'性别' 
'体重' 
INTO lv_string SEPARATED BY cl_bcs_convert=>gc_tab. 
DO 5 TIMES. 
lv_name = '张三' && sy-index. 
lv_age  = 18 * sy-index. 
IF sy-index MOD 2 EQ 0. 
lv_sex = '男'. 
ELSE. 
lv_sex = '女'. 
ENDIF. 
lv_weight = '70KG'. 
CONCATENATE 
lv_name 
lv_age 
lv_sex 
lv_weight 
INTO lv_string1 SEPARATED BY cl_bcs_convert=>gc_tab. 
CONCATENATE lv_string cl_bcs_convert=>gc_crlf lv_string1 INTO lv_string. 
ENDDO. 
"将string转换成xls放入附件 
REFRESH lt_attachment. 
TRY. 
cl_bcs_convert=>string_to_solix( 
EXPORTING 
iv_string   = lv_string 
iv_codepage = '4103'  "suitable for MS Excel, leave empty 
iv_add_bom  = 'X'     "for other doc types 
IMPORTING 
et_solix  = lt_solix 
ev_size   = lv_size ). 
CATCH cx_bcs. 
MESSAGE e445(so). 
ENDTRY. 
ls_attachment-subject = '附件主题名'. 
ls_attachment-objtp = lc_objtp. 
ls_attachment-cont_hex = lt_solix. 
APPEND ls_attachment TO lt_attachment. 
"邮件正文 
CLEAR ls_mail_body. 
ls_mail_body-line = 'TEST SEND EMAIL(详细请查看附件)'. 
APPEND ls_mail_body TO lt_mail_body. 
ls_mail_body-line = 'SAP自动发送,请勿回复'. 
APPEND ls_mail_body TO lt_mail_body. 
WRITE: sy-datum TO lv_date USING EDIT MASK '____-__-__'. 
WRITE: sy-uzeit TO lv_time USING EDIT MASK '__:__:__'. 
CONCATENATE '发送时间:' lv_date lv_time INTO ls_mail_body-line RESPECTING BLANKS. 
APPEND ls_mail_body TO lt_mail_body. 
"邮件主题 
lv_psubject = 'EMAIL SUBJECT' && '_' && sy-datum. 
* 初始化邮件发送请求 
CLASS cl_bcs DEFINITION LOAD. 
TRY. 
lo_send_request = cl_bcs=>create_persistent( ). 
CATCH   cx_send_req_bcs . 
ENDTRY. 
* 设置邮件主内容 
TRY. 
lo_document = cl_document_bcs=>create_document( 
i_type = 'RAW' 
i_text =  lt_mail_body 
i_subject = lv_psubject ). 
CATCH   cx_document_bcs . 
ENDTRY. 
* 设置附件 
LOOP AT lt_attachment INTO ls_attachment. 
"获取附件主题 
attachment_subject = ls_attachment-subject. 
TRY. 
lo_document->add_attachment( 
EXPORTING 
i_attachment_type = ls_attachment-objtp 
i_attachment_subject = attachment_subject 
i_att_content_hex = ls_attachment-cont_hex ). 
CATCH cx_document_bcs INTO lx_document_bcs. 
ENDTRY. 
ENDLOOP. 
* 设置附件文档到发送请求 
TRY. 
lo_send_request->set_document( lo_document ). 
CATCH cx_send_req_bcs. 
ENDTRY. 
" 设置发件人 
TRY. 
"根据参数设置发件人的邮箱 需要在后台basis配置 
IF lv_sender IS NOT INITIAL. 
lo_sender = cl_cam_address_bcs=>create_internet_address( lv_sender ). 
ELSE. 
"将当前GUI 登录的用户邮箱作为发送邮箱 需要在SU01里配置 
lo_sender = cl_sapuser_bcs=>create( sy-uname ). 
ENDIF. 
TRY. 
lo_send_request->set_sender( 
EXPORTING 
i_sender = lo_sender ). 
CATCH cx_send_req_bcs. 
ENDTRY. 
CATCH cx_address_bcs. 
RETURN. 
ENDTRY. 
* 设置收件人 
IF lt_receivers[] IS INITIAL. 
"设置指定的单个人 或者 根据用户名发送 
IF recipient_mail IS NOT INITIAL. 
TRY. 
lo_recipient = cl_cam_address_bcs=>create_internet_address( recipient_mail ). "#EC .. 
CATCH cx_address_bcs. 
ENDTRY. 
ELSE. 
TRY. 
lo_recipient = cl_sapuser_bcs=>create( sy-uname ). "#EC .. 
CATCH cx_address_bcs. 
ENDTRY. 
ENDIF. 
TRY. 
lo_send_request->add_recipient( 
EXPORTING 
i_recipient = lo_recipient 
i_express = 'X' ).                                  "#EC .. 
CATCH cx_send_req_bcs. 
ENDTRY. 
ELSE. 
LOOP AT lt_receivers INTO ls_receivers. 
IF ls_receivers-iusrid IS NOT INITIAL. 
lv_recipient_uid = ls_receivers-iusrid. 
TRY. 
lo_recipient = cl_sapuser_bcs=>create( lv_recipient_uid ). "#EC .. 
CATCH cx_address_bcs. 
ENDTRY. 
ELSEIF ls_receivers-email IS NOT INITIAL. 
lv_recipient_mail = ls_receivers-email . 
TRY. 
lo_recipient = cl_cam_address_bcs=>create_internet_address( lv_recipient_mail ). 
CATCH cx_address_bcs. 
ENDTRY. 
ENDIF. 
TRY. 
"如果copy是X 则是抄送 
lo_send_request->add_recipient( 
EXPORTING 
i_recipient   = lo_recipient 
i_express     = space 
i_copy        = space 
i_blind_copy  = abap_true ). 
CATCH cx_send_req_bcs. 
ENDTRY. 
ENDLOOP. 
ENDIF. 
** Send email 
TRY. 
"立即发送 
"lo_send_request->set_send_immediately( 'X' ). 
lo_send_request->send( 
EXPORTING 
i_with_error_screen = 'X' 
RECEIVING 
result = lv_result ). 
COMMIT WORK. 
WAIT UP TO 1 SECONDS. 
CATCH cx_send_req_bcs. 
lv_result = ''. 
ENDTRY. 
IF lv_result = 'X'. 
MESSAGE '发送成功' TYPE 'S'. 
*    SUBMIT rsconn01 WITH mode = 'INT' 
*                  WITH output = '' 
*                  AND RETURN. 
ENDIF. 
ENDFORM.

2、发送邮件(带附件pdf)

2.1 第一种(弹出打印框后进行发送)

ABAP 发送邮件详解编程语言

ABAP 发送邮件详解编程语言

REPORT ywx_test12. 
DATA: gs_name  TYPE rs38l_fnam. 
DATA: gs_job_output_info    TYPE ssfcrescl, 
gs_output_options     TYPE ssfcompop, 
gs_control_parameters TYPE ssfctrlop, 
gt_ekpo               TYPE STANDARD TABLE OF zmmf01it, 
gs_ekko               TYPE zmmf01wa. 
DATA: gt_spoolids TYPE tsfspoolid, 
gs_spoolids TYPE rspoid. 
DATA: rq       TYPE tsp01, 
bin_size TYPE i, 
dummy    TYPE TABLE OF rspoattr. 
DATA: send_request  TYPE REF TO cl_bcs, 
document      TYPE REF TO cl_document_bcs, 
recipient     TYPE REF TO if_recipient_bcs, 
lx_document_bcs TYPE REF TO cx_document_bcs VALUE IS INITIAL, 
sent_to_all   TYPE os_boolean, 
g_mailto      TYPE ad_smtpadr VALUE [email protected]', 
bcs_exception TYPE REF TO cx_bcs. 
DATA: pdf_xstring TYPE xstring, 
pdf_content TYPE solix_tab, 
pdf_size    TYPE so_obj_len. 
DATA: lt_mail_body       TYPE bcsy_text, 
ls_mail_body       TYPE soli, 
attachment_subject TYPE so_obj_des. 
DATA: lv_date TYPE c LENGTH 20, 
lv_time TYPE c LENGTH 20. 
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME' 
EXPORTING 
formname           = 'ZMMF025_SH' 
IMPORTING 
fm_name            = gs_name 
EXCEPTIONS 
no_form            = 1 
no_function_module = 2 
OTHERS             = 3. 
"gs_control_parameters-no_dialog = 'X'. 
gs_control_parameters-langu     = sy-langu. 
gs_output_options-tdnewid   = 'X'. 
gs_output_options-tdimmed   = ' '. 
gs_output_options-tddelete  = ' '. 
CALL FUNCTION gs_name 
EXPORTING 
control_parameters = gs_control_parameters 
output_options     = gs_output_options 
wa_ekko            = gs_ekko 
IMPORTING 
job_output_info    = gs_job_output_info 
TABLES 
it_ekpo            = gt_ekpo 
EXCEPTIONS 
formatting_error   = 1 
internal_error     = 2 
send_error         = 3 
user_canceled      = 4 
OTHERS             = 5. 
" 打印池里的详细内容 
MOVE gs_job_output_info-spoolids TO gt_spoolids. 
LOOP AT gt_spoolids INTO gs_spoolids. 
*&---------------------------------------------------------------------* 
*& Create PDF 
*&---------------------------------------------------------------------* 
* Create PDF Content 
* 1) get attributes of spool request 
* 2) convert spool request to PDF dependent on document type 
*----------------------------------------------------------------------* 
*   ------------ get attributes of spool request --------------------- 
CALL FUNCTION 'RSPO_GET_ATTRIBUTES_SPOOLJOB' 
EXPORTING 
rqident     = gs_spoolids 
IMPORTING 
rq          = rq 
TABLES 
attributes  = dummy 
EXCEPTIONS 
no_such_job = 1 
OTHERS      = 2. 
IF sy-subrc <> 0. 
CONTINUE. 
ENDIF. 
*   --- convert spool request into PDF, dependent on document type --- 
IF rq-rqdoctype = 'OTF' OR rq-rqdoctype = 'SMART'. 
CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF' 
EXPORTING 
src_spoolid              = gs_spoolids 
no_dialog                = 'X' 
pdf_destination          = 'X' 
no_background            = 'X' 
IMPORTING 
pdf_bytecount            = bin_size 
bin_file                 = pdf_xstring 
EXCEPTIONS 
err_no_otf_spooljob      = 1 
err_no_spooljob          = 2 
err_no_permission        = 3 
err_conv_not_possible    = 4 
err_bad_dstdevice        = 5 
user_cancelled           = 6 
err_spoolerror           = 7 
err_temseerror           = 8 
err_btcjob_open_failed   = 9 
err_btcjob_submit_failed = 10 
err_btcjob_close_failed  = 11 
OTHERS                   = 12. 
IF sy-subrc <> 0. 
*   error handling 
CONTINUE. 
ENDIF. 
ELSEIF rq-rqdoctype = 'LIST'. 
CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF' 
EXPORTING 
src_spoolid              = gs_spoolids 
no_dialog                = 'X' 
pdf_destination          = 'X' 
no_background            = 'X' 
IMPORTING 
pdf_bytecount            = bin_size 
bin_file                 = pdf_xstring 
EXCEPTIONS 
err_no_abap_spooljob     = 1 
err_no_spooljob          = 2 
err_no_permission        = 3 
err_conv_not_possible    = 4 
err_bad_destdevice       = 5 
user_cancelled           = 6 
err_spoolerror           = 7 
err_temseerror           = 8 
err_btcjob_open_failed   = 9 
err_btcjob_submit_failed = 10 
err_btcjob_close_failed  = 11 
OTHERS                   = 12. 
IF sy-subrc <> 0. 
CONTINUE. 
ENDIF. 
ELSE. 
CONTINUE. 
ENDIF. 
pdf_size = bin_size. 
*&---------------------------------------------------------------------* 
*& Send mail 
*&---------------------------------------------------------------------* 
TRY. 
*     -------- create persistent send request ------------------------ 
send_request = cl_bcs=>create_persistent( ). 
*     -------- create and set document ------------------------------- 
pdf_content = cl_document_bcs=>xstring_to_solix( pdf_xstring ). 
*      document = cl_document_bcs=>create_document( 
*            i_type    = 'PDF' 
*            i_hex     = pdf_content 
*            i_length  = pdf_size 
*            i_subject = '邮件题目' ). 
"邮件正文 
CLEAR ls_mail_body. 
ls_mail_body-line = 'TEST SEND EMAIL(详细请查看附件)'. 
APPEND ls_mail_body TO lt_mail_body. 
ls_mail_body-line = 'SAP自动发送,请勿回复'. 
APPEND ls_mail_body TO lt_mail_body. 
WRITE: sy-datum TO lv_date USING EDIT MASK '____-__-__'. 
WRITE: sy-uzeit TO lv_time USING EDIT MASK '__:__:__'. 
CONCATENATE '发送时间:' lv_date lv_time INTO ls_mail_body-line RESPECTING BLANKS. 
APPEND ls_mail_body TO lt_mail_body. 
TRY. 
document = cl_document_bcs=>create_document( 
i_type = 'RAW' 
i_text =  lt_mail_body 
i_subject = 'EMAIL SUBJECT' ). 
CATCH   cx_document_bcs . 
ENDTRY. 
attachment_subject = 'TEST PDF'. 
TRY. 
document->add_attachment( 
EXPORTING 
i_attachment_type = 'PDF' 
i_attachment_subject = attachment_subject 
i_att_content_hex = pdf_content ). 
CATCH cx_document_bcs INTO lx_document_bcs. 
ENDTRY. 
*     add document object to send request 
send_request->set_document( document ). 
*     --------- add recipient (e-mail address) ----------------------- 
*     create recipient object 
recipient = cl_cam_address_bcs=>create_internet_address( g_mailto ). 
*     add recipient object to send request 
send_request->add_recipient( recipient ). 
*     ---------- send document --------------------------------------- 
sent_to_all = send_request->send( i_with_error_screen = 'X' ). 
COMMIT WORK. 
IF sent_to_all IS INITIAL. 
*       error handling 
CONTINUE. 
ELSE. 
MESSAGE 'send mail successfully' TYPE 'S'. 
ENDIF. 
*   ------------ exception handling ---------------------------------- 
*   replace this rudimentary exception handling with your own one !!! 
CATCH cx_bcs INTO bcs_exception. 
ENDTRY. 
ENDLOOP.

2.2 第二种(不弹出打印框,直接发送)

"主要将getotf = 'X'打开 
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME' 
EXPORTING 
formname           = 'ZMMF025_SH' 
IMPORTING 
fm_name            = gs_name 
EXCEPTIONS 
no_form            = 1 
no_function_module = 2 
OTHERS             = 3. 
gs_control_parameters-no_dialog = 'X'. 
gs_control_parameters-langu     = sy-langu. 
gs_control_parameters-getotf    = 'X'. 
gs_output_options-tdnoprev  = 'X'. 
CALL FUNCTION gs_name 
EXPORTING 
control_parameters = gs_control_parameters 
output_options     = gs_output_options 
wa_ekko            = gs_ekko 
IMPORTING 
job_output_info    = gs_job_output_info 
TABLES 
it_ekpo            = gt_ekpo 
EXCEPTIONS 
formatting_error   = 1 
internal_error     = 2 
send_error         = 3 
user_canceled      = 4 
OTHERS             = 5. 
DATA: lt_tline TYPE STANDARD TABLE OF tline. 
CALL FUNCTION 'CONVERT_OTF' 
EXPORTING 
format                = 'PDF' 
*   MAX_LINEWIDTH         = 255 
*   ARCHIVE_INDEX         = ' ' 
*   COPYNUMBER            = 0 
*   ASCII_BIDI_VIS2LOG    = ' ' 
*   PDF_DELETE_OTFTAB     = ' ' 
IMPORTING 
bin_filesize          = bin_size 
bin_file              = pdf_xstring 
TABLES 
"otf                   = gs_job_output_info-otfdata 
lines                 = lt_tline 
EXCEPTIONS 
err_max_linewidth     = 1 
err_format            = 2 
err_conv_not_possible = 3 
err_bad_otf           = 4 
OTHERS                = 5. 
IF sy-subrc <> 0. 
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

3、其他

邮件主体内容也可以写HTML

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/17987.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论