只是方便以后项目用的时候直接copy,呵呵
一、发邮件的FM
ZLM_TEST_MAIL_01
.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(MAIL_TITLE) TYPE SO_OBJ_DES OPTIONAL
*" REFERENCE(MAIL_TEXT) TYPE STRING
*" EXPORTING
*" VALUE(ERROR_TEXT) TYPE STRING
*" TABLES
*" SEND_LIST STRUCTURE SOLISTI1 OPTIONAL
*"----------------------------------------------------------------------
DATA
:
BEGIN
OF GT_SEND
OCCURS
0
.
"E-mail address
INCLUDE
STRUCTURE SOMLRECI1
.
DATA
:
END
OF GT_SEND
.
DATA
: GS_DATA
LIKE SODOCCHGI1
,
"E-mail setting
TAB_LINES
TYPE
I
,
DOC_SIZE
TYPE
I
.
DATA
: L_TEXT
(
50
)
.
DATA
:
BEGIN
OF GT_ITAB
OCCURS
0
,
"E-mail text
TEXT
(
255
)
,
END
OF GT_ITAB
.
DATA
: IT_SITAB
TYPE
TABLE
OF STRING
.
SPLIT MAIL_TEXT
AT
'/n/t'
INTO
TABLE GT_ITAB
.
* Email的接收人员
LOOP
AT SEND_LIST
.
GT_SEND
-RECEIVER
= SEND_LIST
-
LINE
.
GT_SEND
-REC_TYPE
=
'U'
.
GT_SEND
-EXPRESS
=
'X'
.
APPEND GT_SEND
.
ENDLOOP
.
CLEAR
: GS_DATA
.
GS_DATA
-OBJ_NAME
=
'URGENT'
.
GS_DATA
-OBJ_DESCR
= MAIL_TITLE
.
"邮件标题
GS_DATA
-OBJ_LANGU
=
'1'
.
"中文
*GS_DATA-SENSITIVTY = 'P'.
GS_DATA
-OBJ_PRIO
=
'1'
.
GS_DATA
-NO_CHANGE
=
'X'
.
GS_DATA
-PRIORITY
=
'1'
.
* Write Packing List (Main)
DESCRIBE
TABLE GT_ITAB
LINES TAB_LINES
.
READ
TABLE GT_ITAB
INDEX TAB_LINES
.
GS_DATA
-DOC_SIZE
=
( TAB_LINES
-
1
) *
255 +
STRLEN
( GT_ITAB
)
.
* SEND THE EMAIL
CALL
FUNCTION
'SO_NEW_DOCUMENT_SEND_API1'
EXPORTING
DOCUMENT_DATA
= GS_DATA
DOCUMENT_TYPE
=
'RAW'
PUT_IN_OUTBOX
=
'X'
COMMIT_WORK
=
'X'
TABLES
OBJECT_CONTENT
= GT_ITAB
RECEIVERS
= GT_SEND
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
NE
0
.
ERROR_TEXT
=
'邮件发送失败'
.
ElseIF SY
-SUBRC
=
0
.
* 考虑系统时延,等待两秒
WAIT
UP
TO
2 SECONDS
.
* 执行邮件立即发送程序
SUBMIT RSCONN01
WITH
MODE
=
'INT'
WITH
OUTPUT
=
'X'
"显示发送结果 可以使用空
AND
RETURN
.
MESSAGE
'发送成功'
TYPE
'S'
.
ENDIF
.
ENDFUNCTION
.
二、发邮件的Report
*&---------------------------------------------------------------------*
*& Report ZCLQ_SEND_EMAIL_ADDITON
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zlm_send_email.
* Data Declaration
DATA: docdata LIKE sodocchgi1,
objpack LIKE sopcklsti1 OCCURS 1 WITH HEADER LINE,
objhead LIKE solisti1 OCCURS 1 WITH HEADER LINE,
objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE,
objbin LIKE solisti1 OCCURS 10 WITH HEADER LINE,
objhex LIKE solix OCCURS 10 WITH HEADER LINE,
reclist LIKE somlreci1 OCCURS 1 WITH HEADER LINE.
DATA: BEGIN OF it_obj_descr OCCURS 0,
obj_desc LIKE objpack-obj_descr ,
END OF it_obj_descr.
DATA: listobject LIKE abaplist OCCURS 1 WITH HEADER LINE.
DATA: tab_lines TYPE i,
des_lines TYPE i,
doc_size TYPE i,
att_type LIKE soodk-objtp.
PARAMETERS: p_file LIKE rlgrap-filename OBLIGATORY,
p_email TYPE so_recname OBLIGATORY. "收件人邮箱
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
DATA: tmp_filename LIKE rlgrap-filename.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
* def_filename = p_file
mask = ',*.XLSX,*.XLSX;,*.TXT,*.TXT;,*.PDF,*.PDF;,*.*,*.*.'
mode = 'O'
IMPORTING
filename = tmp_filename
EXCEPTIONS
inv_winsys = 01
no_batch = 02
selection_cancel = 03
selection_error = 04.
IF sy-subrc = 0.
p_file = tmp_filename.
ENDIF.
START-OF-SELECTION.
DATA: l_file TYPE string.
l_file = p_file.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = l_file
filetype = 'BIN' "获取时采用二进制
* HAS_FIELD_SEPARATOR = 'X'
TABLES
data_tab = objbin[]
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
* NOTE: 以下部分为制作邮件的标题,文本内容等信息
* Create Message Body
* Title and Description
docdata-obj_name = 'ADAF'. "文档名称 名称随便取
docdata-obj_descr = '测试附件传输'. "邮件的主题
"邮件正文
* Main Text
objtxt = 'Test Document.'.
APPEND objtxt.
objtxt = 'HAVE A NICE DAY.'.
APPEND objtxt.
* Write Packing List (Main)
DESCRIBE TABLE objtxt LINES tab_lines.
READ TABLE objtxt INDEX tab_lines.
docdata-doc_size = ( tab_lines - 1 ) * 255 + strlen( objtxt ).
CLEAR objpack-transf_bin.
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = 1.
objpack-body_num = tab_lines.
objpack-doc_type = 'RAW'.
APPEND objpack.
* 获取附件文件名称
SPLIT l_file AT '\' INTO TABLE it_obj_descr.
DESCRIBE TABLE it_obj_descr LINES des_lines.
READ TABLE it_obj_descr INDEX des_lines.
* 这里开始添加附件相关信息
att_type = 'EXT'. "这里可能的输入参数可以参考表TSOTD, EXT表示外部文件
DESCRIBE TABLE objbin LINES tab_lines.
READ TABLE objbin INDEX tab_lines.
objpack-doc_size = ( tab_lines - 1 ) * 255 + strlen( objbin ). "附件的大小
objpack-transf_bin = 'X'. "表示以二进制传输,由于上传时选择二进制,所以这里必须给值
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = 1. "附件内容开始行,如果添加多个附件则下一个附件的起始行为 TAB_LINES + 1
objpack-body_num = tab_lines. "附件内容总行数
objpack-doc_type = att_type.
objpack-obj_name = 'ATTACHMENT'.
objpack-obj_descr = it_obj_descr-obj_desc. "附件的名称
APPEND objpack.
"收件人信息----邮箱
* CREATE RECEIVER LIST
reclist-receiver = p_email. "接收人邮箱
reclist-rec_type = 'U'.
reclist-express = 'X'.
*RECLIST-REC_TYPE = 'A'.
APPEND reclist.
"调用函数发送邮件
* SEND MESSAGE
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = docdata "邮件标题的属性
put_in_outbox = 'X'
commit_work = 'X' "USED FROM REL. 6.10
* IMPORTING
* SENT_TO_ALL =
* NEW_OBJECT_ID =
TABLES
packing_list = objpack "传输内容格式
object_header = objhead
contents_bin = objbin "附件内容
contents_txt = objtxt "邮件文字内容
* CONTENTS_HEX = OBJHEX
* OBJECT_PARA =
* OBJECT_PARB =
receivers = reclist "接收人列表
* 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.
MESSAGE ID 'SO' TYPE 'S' NUMBER '023'
WITH docdata-obj_name.
ELSE.
* 考虑系统时延,等待两秒
WAIT UP TO 2 SECONDS.
* 执行邮件立即发送程序
SUBMIT rsconn01 WITH mode = 'INT'
WITH output = 'X' "显示发送结果
AND RETURN.
ENDIF.
WRITE: / 'END OF PROGRAM'.
***************************************************************************************
这里都有
SUBMIT
rsconn01
WITH
mode
=
'INT'
WITH
output
=
'X'
"显示发送结果
AND
RETURN
.
会弹出
选中一项点对勾就行了
如果不想显示这个对话框,可以设置 output = ''.
SUBMIT rsconn01 WITH mode = 'INT'
WITH output = '' "不显示发送结果
AND RETURN.
如果前面scot配置的正确,这段代码拷过去就可以直接发送
这里是没有设置发件箱的
因为scot配的是自己公司的域名
我的就是liumeng@***.com
具体是怎么映射的,不知道,可能是basis那边做的,作为开发,权限有限……
有人知道请留言告诉我,谢谢
如果公司邮箱的用户名跟sap用户名不一致,可能发布出去吧,我没试过,有人了解请留言
谢谢!
标签:发邮件,GT,obj,DATA,SEND,objpack,TYPE,FM,邮件 From: https://blog.51cto.com/u_15680210/5751419