1.SE37创建FUNCTION :Z_COM_AUTO_MAIL
2. Import 参数。
3.Export 参数
4.table参数
5.table参数中ZTMAILADD邮件地址表参数,SE11创建表类型ZTMAILADD
SE11创建结构ZSMAILADD
SE11创建数据元素
SE11创建域
6.源码
FUNCTION Z_COM_AUTO_MAIL.
*"----------------------------------------------------------------------
*"*"區域介面:
*" IMPORTING
*" REFERENCE(I_SUBJECT) TYPE SO_OBJ_DES OPTIONAL
*" REFERENCE(I_MAILTO) TYPE CHAR255 OPTIONAL
*" REFERENCE(I_MAILCC) TYPE CHAR255 OPTIONAL
*" EXPORTING
*" VALUE(E_RETURN) TYPE CHAR255
*" TABLES
*" T_MAILADD TYPE ZTMAILADD OPTIONAL
*" T_CONTENT TYPE SOLI_TAB
*" T_ATTACHMENT TYPE SOLI_TAB OPTIONAL
*"----------------------------------------------------------------------
DATA:LO_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS, "用来放发送的内容的类
LIT_CONTENTS TYPE SOLI_TAB,
L_CC TYPE ADR6-SMTP_ADDR,
L_TO TYPE ADR6-SMTP_ADDR,
L_BCS_TO TYPE REF TO IF_RECIPIENT_BCS,
L_BCS_CC TYPE REF TO IF_RECIPIENT_BCS,
LO_SENDER TYPE REF TO CL_SAPUSER_BCS,
L_RESULT TYPE OS_BOOLEAN,
* cl_bcs发送邮件主要用到的功能类, 包括创建发送请求, 添加发送内容,添加发送地址, 到最终的发送指令发出.
W_DOCUMENT TYPE REF TO CL_BCS,
L_FILE_SIZE_CHAR TYPE SO_OBJ_LEN,
L_FILEN TYPE STRING,
LIT_MAILHEX TYPE SOLIX_TAB,
L_FILE_SIZE TYPE I,
L_RC TYPE I,
L_STRING TYPE STRING,
L_SUBJECT TYPE SO_OBJ_DES,
* cx_bcs异常类, 捕捉发送邮件过程中出现的异常.
LO_FAIL TYPE REF TO CX_BCS.
* 邮件正文
LOOP AT T_CONTENT INTO L_STRING.
APPEND L_STRING TO LIT_CONTENTS.
ENDLOOP.
TRY.
* CREATE THE DOCUMENT WITH CONTENTS
CREATE OBJECT LO_DOCUMENT.
LO_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
I_TYPE = 'HTM'
I_SUBJECT = I_SUBJECT "邮件标题
I_LENGTH = L_FILE_SIZE_CHAR
I_LANGUAGE = SY-LANGU
I_IMPORTANCE = '1'
I_TEXT = LIT_CONTENTS
).
* 附件类型为BIN接收任何类型的附件,包括视频、音频文件等。
* 读取附件
IF T_ATTACHMENT IS NOT INITIAL.
LOOP AT T_ATTACHMENT INTO DATA(P_FILE).
L_FILEN = P_FILE.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD
EXPORTING
FILENAME = 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 =
IMPORTING
FILELENGTH = L_FILE_SIZE
* header =
CHANGING
DATA_TAB = LIT_MAILHEX
* isscanperformed = SPACE
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
NOT_SUPPORTED_BY_GUI = 17
ERROR_NO_GUI = 18
OTHERS = 19.
IF SY-SUBRC <> 0.
CASE SY-SUBRC.
WHEN '1' .
E_RETURN = 'FILE_OPEN_ERROR ' .
WHEN '2' .
E_RETURN = 'FILE_READ_ERROR ' .
WHEN '3' .
E_RETURN = 'NO_BATCH ' .
WHEN '4' .
E_RETURN = 'GUI_REFUSE_FILETRANSFER ' .
WHEN '5' .
E_RETURN = 'INVALID_TYPE ' .
WHEN '6' .
E_RETURN = 'NO_AUTHORITY ' .
WHEN '7' .
E_RETURN = 'UNKNOWN_ERROR ' .
WHEN '8' .
E_RETURN = 'BAD_DATA_FORMAT ' .
WHEN '9' .
E_RETURN = 'HEADER_NOT_ALLOWED ' .
WHEN '10'.
E_RETURN = 'SEPARATOR_NOT_ALLOWED ' .
WHEN '11'.
E_RETURN = 'HEADER_TOO_LONG ' .
WHEN '12'.
E_RETURN = 'UNKNOWN_DP_ERROR ' .
WHEN '13'.
E_RETURN = 'ACCESS_DENIED ' .
WHEN '14'.
E_RETURN = 'DP_OUT_OF_MEMORY ' .
WHEN '15'.
E_RETURN = 'DISK_FULL ' .
WHEN '16'.
E_RETURN = 'DP_TIMEOUT ' .
WHEN '17'.
E_RETURN = 'NOT_SUPPORTED_BY_GUI ' .
WHEN '18'.
E_RETURN = 'ERROR_NO_GUI ' .
WHEN OTHERS.
E_RETURN = 'OTHERS mistake' .
ENDCASE.
* Implement suitable error handling here
RETURN.
ENDIF.
* 附件长度,这个很重要,一定要有
L_FILE_SIZE_CHAR = L_FILE_SIZE_CHAR.
* 获取附件名字及附件文件类型
DATA(L_FILE) = P_FILE .
DO .
SPLIT L_FILE AT '\' INTO DATA(LV_TMP) L_FILE .
SEARCH L_FILE FOR '\' .
IF SY-SUBRC <> 0.
EXIT .
ENDIF.
ENDDO.
L_SUBJECT = L_FILE .
* 添加附件,可以添加多个附件
CALL METHOD LO_DOCUMENT->ADD_ATTACHMENT
EXPORTING
I_ATTACHMENT_TYPE = 'BIN'
I_ATTACHMENT_SUBJECT = L_SUBJECT "附件名称
I_ATTACHMENT_SIZE = L_FILE_SIZE_CHAR "附件大小
I_ATT_CONTENT_HEX = LIT_MAILHEX. "附件内容
ENDLOOP.
ENDIF.
* CREATING PERSISTENT OBJECT WILL ALLOW YOU TO SET THE DOCUMENT IN THE MAIL
W_DOCUMENT = CL_BCS=>CREATE_PERSISTENT( ).
* 发件人
* 前提是这个邮箱地址能发邮件,并且不需要密码
LO_SENDER = CL_SAPUSER_BCS=>CREATE( SY-UNAME ).
W_DOCUMENT->SET_SENDER( LO_SENDER ).
* 收件人
IF I_MAILTO IS NOT INITIAL.
L_TO = I_MAILTO.
L_BCS_TO = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( L_TO ).
* Add recipient to send request
CALL METHOD W_DOCUMENT->ADD_RECIPIENT
EXPORTING
I_RECIPIENT = L_BCS_TO.
ENDIF.
* 抄送人
IF I_MAILCC IS NOT INITIAL.
L_CC = I_MAILCC. "
L_BCS_CC = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( L_CC ).
CALL METHOD W_DOCUMENT->ADD_RECIPIENT
EXPORTING
I_RECIPIENT = L_BCS_CC
I_COPY = 'X'.
" i_express = 'X'.
ENDIF.
"邮件地址表有值。
IF T_MAILADD IS NOT INITIAL.
LOOP AT T_MAILADD INTO DATA(LS_MAILADD).
IF LS_MAILADD-MAILTYPE = 'T'.
L_TO = LS_MAILADD-MAILADDR.
L_BCS_TO = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( L_TO ).
* Add recipient to send request
CALL METHOD W_DOCUMENT->ADD_RECIPIENT
EXPORTING
I_RECIPIENT = L_BCS_TO.
ELSEIF LS_MAILADD-MAILTYPE = 'C'.
L_CC = LS_MAILADD-MAILADDR. "
L_BCS_CC = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( L_CC ).
CALL METHOD W_DOCUMENT->ADD_RECIPIENT
EXPORTING
I_RECIPIENT = L_BCS_CC
I_COPY = 'X'.
" i_express = 'X'.
ENDIF.
ENDLOOP.
ENDIF.
* SEND THE MAIL
W_DOCUMENT->SET_SEND_IMMEDIATELY( 'X' ). "设置立即发送
W_DOCUMENT->SEND_REQUEST->SET_LINK_TO_OUTBOX( 'X' ). "与outbox关联
CALL METHOD W_DOCUMENT->SET_DOCUMENT( LO_DOCUMENT ).
CALL METHOD W_DOCUMENT->SEND(
EXPORTING
I_WITH_ERROR_SCREEN = 'X'
RECEIVING
RESULT = L_RESULT ).
CATCH CX_BCS INTO LO_FAIL.
ENDTRY.
* YOU CAN VERIFY THE STATUS IN THE LIST, YOU CAN ALSO SUBMIT THIS AS A BACKGROUND JOB.
IF L_RESULT = 'X'.
COMMIT WORK AND WAIT.
ELSE.
ROLLBACK WORK.
E_RETURN = '发送失败' .
ENDIF.
ENDFUNCTION.
以上,function已经创建完成,可以使用了。返回消息为空就代表发送成功了。下面我们创建将测试程序。
se38创建测试程序(代码中的邮件地址改为自己的哟,目前为本人邮箱)。
*&---------------------------------------------------------------------*
*& Report ZLQT_COM_AUTO_MAIL
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZLQT_COM_AUTO_MAIL.
DATA I_SUBJECT TYPE SO_OBJ_DES . "邮件主题
DATA I_MAILTO TYPE CHAR255 . "收件人
DATA I_MAILCC TYPE CHAR255 . "抄送
DATA E_RETURN TYPE CHAR255 . "返回消息
DATA T_MAILADD TYPE TABLE OF ZSMAILADD WITH HEADER LINE . "邮件地址表
DATA T_CONTENT TYPE TABLE OF SOLI WITH HEADER LINE . "邮件正文内容
DATA T_ATTACHMENT TYPE TABLE OF SOLI WITH HEADER LINE . "邮件附件地址
I_SUBJECT = '邮件主题' .
*T_MAILADD-MAILTYPE = 'T' .
*T_MAILADD-MAILADDR = 'szmis11@lotestech.com' .
*APPEND T_MAILADD .
*
*T_MAILADD-MAILTYPE = 'C' .
*T_MAILADD-MAILADDR = '979621537@QQ.com' .
*APPEND T_MAILADD .
I_MAILTO = 'szmis11@lotestech.com'.
I_MAILCC = '979621537@QQ.com' .
T_CONTENT-LINE = SY-UNAME && SY-DATUM .
APPEND T_CONTENT .
T_ATTACHMENT-LINE = 'C:\Users\szmis11\Desktop\WeChat 圖片_202]]]]]01109190311.jpg' .
APPEND T_ATTACHMENT.
T_ATTACHMENT-LINE = 'C:\Users\szmis11\Desktop\automail.docx' .
APPEND T_ATTACHMENT.
CALL FUNCTION 'Z_COM_AUTO_MAIL'
EXPORTING
I_SUBJECT = I_SUBJECT
I_MAILTO = I_MAILTO
I_MAILCC = I_MAILCC
IMPORTING
E_RETURN = E_RETURN
TABLES
T_MAILADD = T_MAILADD
T_CONTENT = T_CONTENT
T_ATTACHMENT = T_ATTACHMENT
.
WRITE : E_RETURN .
以上为-自动发邮件FUNCTION,AUTOMAIL功能全部内容,若代码存在问题,欢迎评论指正。