原文链接:https://blog.csdn.net/qq_41757790/article/details/125925792
相关事务代码:
SOST : 所有待发送的邮件清单,也可以监视邮件发送状态,也可以在此页面发送邮件!
SBWP:发送邮件功能
SO01 :邮件/消息
如果没有接收到邮件,可以在 SCOT 查看邮件发送状态。邮件发送一般有延迟。黄灯表示在等待发送中。蓝灯发送成功。
在这里配置邮件主机地址。
基本实现代码1:
DATA: it_document_data TYPE sodocchgi1,
it_content_text TYPE STANDARD TABLE OF solisti1 WITH HEADER LINE,
it_packing_list TYPE TABLE OF sopcklsti1 WITH HEADER LINE,
it_receivers TYPE STANDARD TABLE OF somlreci1 WITH HEADER LINE,
lc_mail_attach TYPE string,
lt_content_hex TYPE STANDARD TABLE OF solix WITH HEADER LINE,
lt_object_header TYPE STANDARD TABLE OF solisti1 WITH HEADER LINE,
lc_mail_xattach TYPE xstring,
lv_send_all TYPE c,
it_makt LIKE TABLE OF makt WITH HEADER LINE,
fir_date TYPE sy-datum,
mimetype TYPE char64.
DATA: it_itab TYPE TABLE OF itab WITH HEADER LINE,
wa_itab TYPE itab.
CONSTANTS:lc_tab TYPE c VALUE cl_bcs_convert=>gc_tab, "excel换格符
lc_ctrl TYPE c VALUE cl_bcs_convert=>gc_crlf. "excel换行符
PARAMETER p_flag TYPE c AS CHECKBOX.
IF p_flag = 'X'.
PERFORM send_email.
ENDIF.
*&---------------------------------------------------------------------*
*& Form SEND_EMAIL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM send_email.
DATA sy_vline TYPE i.
DATA:mblnr TYPE string, "物料凭证
matnr TYPE string, "物料
mjahr TYPE string, "年度
maktx TYPE string. "物料描述
it_document_data-obj_descr = '邮件主题' . "内容的简短描述
it_document_data-priority = '1'. "优先次序 1~9 1:最高优先权
it_document_data-obj_name = 'OFFER'.
it_document_data-obj_langu = sy-langu.
* ****邮件正文信息
it_content_text = '各位好:'.
APPEND it_content_text.
it_content_text = ' 此邮件为测试邮件!!!!!!!'.
APPEND it_content_text.
DESCRIBE TABLE it_content_text LINES sy_vline.
it_document_data-doc_size = 255 * ( sy_vline - 1 ) + strlen( it_content_text ). "SAPoffice 文档的大小(用于 API1)
it_packing_list-transf_bin = space.
it_packing_list-head_start = 1.
it_packing_list-head_num = 0 .
it_packing_list-body_num = sy_vline.
it_packing_list-body_start = 1.
it_packing_list-doc_type = 'RAW'.
APPEND it_packing_list.
***收件人信息
it_receivers-receiver = '收件人邮箱'. "收件人地址
it_receivers-rec_type = 'U'.
it_receivers-com_type = 'INT'.
it_receivers-notif_del = 'X'.
it_receivers-notif_ndel = 'X'.
APPEND it_receivers.
CONCATENATE '物料' lc_tab
'物料凭证' lc_tab
'物料描述' lc_tab
'年度' lc_ctrl INTO lc_mail_attach. "lc_ctrl换行符
CLEAR:mblnr, matnr, mjahr, maktx.
mblnr = '7777777'.
matnr = '7777777'.
mjahr = '7777'.
maktx = 'seven'."物料类型
CONCATENATE lc_mail_attach
matnr lc_tab
mblnr lc_tab
mjahr lc_tab
maktx lc_ctrl INTO lc_mail_attach. "lc_ctrl换行符
CLEAR:mblnr, matnr, mjahr, maktx.
mblnr = '8888888'.
matnr = '8888888'.
mjahr = '8888'.
maktx = 'seven'."物料类型
CONCATENATE lc_mail_attach
matnr lc_tab
mblnr lc_tab
mjahr lc_tab
maktx lc_ctrl INTO lc_mail_attach. "lc_ctrl换行符
mimetype = 'APPLICATION/MSEXCEL;CHARSET=UTF-16LE'.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = lc_mail_attach
mimetype = mimetype
* ENCODING =
IMPORTING
buffer = lc_mail_xattach
EXCEPTIONS
failed = 1
others = 2 .
IF sy-subrc = 0.
CONCATENATE cl_abap_char_utilities=>byte_order_mark_little lc_mail_xattach INTO lc_mail_xattach IN BYTE MODE.
ENDIF.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lc_mail_xattach
* APPEND_TO_TABLE = ' '
* IMPORTING
* OUTPUT_LENGTH =
TABLES
binary_tab = lt_content_hex.
"不知道干啥用的
lt_object_header = 'Seven test'.
APPEND lt_object_header.
DESCRIBE TABLE lt_content_hex LINES sy_vline.
it_packing_list-transf_bin = 'X'.
it_packing_list-head_start = 1 .
it_packing_list-head_num = 0 .
it_packing_list-body_start = 1.
it_packing_list-body_num = sy_vline.
it_packing_list-doc_type = 'XLS'.
it_packing_list-doc_size = 255 * sy_vline.
it_packing_list-obj_name = '附件名'.
it_packing_list-obj_descr = it_packing_list-obj_name.
APPEND it_packing_list.
"方法1 无法指定发送人邮箱
******* CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
******* EXPORTING
******* document_data = it_document_data
******* put_in_outbox = 'X'
******* commit_work = 'X'
******* IMPORTING
******* sent_to_all = lv_send_all
******** NEW_OBJECT_ID =
******* TABLES
******* packing_list = it_packing_list[]
******* object_header = lt_object_header[]
******** contents_bin =
******* contents_txt = it_content_text[]
******* contents_hex = lt_content_hex[]
******** object_para =
******** object_parb =
******* receivers = it_receivers[]
******* EXCEPTIONS
******* too_many_receivers = 1
******* document_not_sent = 2
******* document_type_not_exist = 3
******* operation_no_authorization = 4
******* parameter_error = 5
******* x_error = 6
******* enqueue_error = 7
******* others = 8 .
"方法2 可以指定发送人邮箱
CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
EXPORTING
document_data = it_document_data
put_in_outbox = 'X'
sender_address = '发件人邮箱'
sender_address_type = 'INT'
commit_work = 'X'
* IP_ENCRYPT =
* IP_SIGN =
* IV_VSI_PROFILE =
IMPORTING
sent_to_all = lv_send_all
* NEW_OBJECT_ID =
* SENDER_ID =
TABLES
packing_list = it_packing_list[]
object_header = lt_object_header[]
* CONTENTS_BIN =
contents_txt = it_content_text[]
contents_hex = lt_content_hex[]
* OBJECT_PARA =
* OBJECT_PARB =
receivers = it_receivers[]
* ET_VSI_ERROR =
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
others = 8
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ELSE.
SUBMIT RSCONN01 WITH MODE = 'INT'
WITH OUTPUT = 'X'
AND RETURN.
ENDIF.
ENDFORM. "SEND_EMAIL
实现代码2(写个函数方法封装):
function zfm_send_email.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" REFERENCE(I_SUBJECT) TYPE SO_OBJ_DES OPTIONAL
*" TABLES
*" T_RECEIVERS STRUCTURE SOMLRECI1 OPTIONAL
*" T_CONTENTS_TXT STRUCTURE SOLISTI1 OPTIONAL
*"----------------------------------------------------------------------
data p_com type c .
data: i_packing_list like sopcklsti1 occurs 0 with header line,
i_receivers like somlreci1 occurs 0 with header line, "receivers
* t_contents_txt LIKE solisti1 OCCURS 0 WITH HEADER LINE, "ASCII contents of object and attachments
* i_contents_hex LIKE SOLIX OCCURS 0 WITH HEADER LINE, "Binary contents of object and attachments
i_contents_bin like solisti1 occurs 0 with header line, "Binary contents of object and attachments
i_object_header like solisti1 occurs 0 with header line, "attachments name
w_doc_data like sodocchgi1. "邮件标题及优先级
data: tab_lines like sy-tabix,
w_sent_all(1) type c.
********define subject and priority************
w_doc_data-obj_descr = i_subject. "Email Subject.
w_doc_data-priority = 1. "邮件优先级 1~9 , 1为最高优先级
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = 'OFFER'. "Name of document..
w_doc_data-sensitivty = 'O'. "Document sensitivity.
describe table t_contents_txt lines tab_lines.
read table t_contents_txt index tab_lines.
w_doc_data-doc_size = ( tab_lines - 1 ) * 255 + strlen( t_contents_txt ).
********根据邮件主体内容计算其行数*************
clear i_packing_list.
i_packing_list-transf_bin = space. "ASCII format/binary format
i_packing_list-head_start = 1.
i_packing_list-head_num = 0.
i_packing_list-body_start = 1.
i_packing_list-body_num = tab_lines.
i_packing_list-doc_type = 'RAW'.
append i_packing_list.
* Add the recipients email address
refresh i_receivers.
loop at t_receivers.
clear i_receivers.
i_receivers-receiver = t_receivers-receiver. "Email Address
i_receivers-rec_type = 'U'. "Internet address
i_receivers-notif_read = 'X'.
i_receivers-com_type = 'INT'.
i_receivers-notif_del = 'X'.
i_receivers-notif_ndel = 'X'.
append i_receivers.
endloop.
if sy-tcode+(2) = 'CO'.
p_com = ''.
else.
p_com = 'X'.
endif.
call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'
exporting
document_data = w_doc_data "Attributes of new document
put_in_outbox = 'X' "Flag: Move document to outbox after send
commit_work = p_com "An explicit COMMIT WORK is to be set
importing
sent_to_all = w_sent_all
tables
packing_list = i_packing_list "Information about structure of data tables
object_header = i_object_header
* contents_hex =
contents_bin = i_contents_bin
contents_txt = t_contents_txt
receivers = i_receivers "Document recipients with send attributes
exceptions
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
others = 8.
endfunction.