输入参数:
I_SUBJECT:内容的简短描述
IT_MESSAGE_BODY:邮件主体部分(convert string to table)
IT_ATTACHMENTS:附件内容
I_SENDER_MAIL:发送者的电子邮件地址
I_ATTMSG_CHECK:邮件类型
输出参数:
E_RESULT:Boolean Variable (X=True, -=False, Space=Unknown)
传输内表:
RECIPIENTS:接收者的电子邮件地址(可多人)
举例如下
FUNCTION ZCMF_SEND_MAIL.
*"----------------------------------------------------------------------
*" IMPORTING
*" REFERENCE(I_SUBJECT) TYPE SO_OBJ_DES
*" REFERENCE(IT_MESSAGE_BODY) TYPE BCSY_TEXT
*" REFERENCE(IT_ATTACHMENTS) TYPE RMPS_T_POST_CONTENT OPTIONAL
*" VALUE(I_SENDER_MAIL) TYPE ADR6-SMTP_ADDR OPTIONAL
*" VALUE(I_ATTMSG_CHECK) TYPE ZEHRATTYP OPTIONAL
*" EXPORTING
*" REFERENCE(E_RESULT) TYPE BOOLEAN
*" TABLES
*" RECIPIENTS STRUCTURE ADR6 OPTIONAL
*"----------------------------------------------------------------------
*Data Declaration
DATA: LO_SENDER TYPE REF TO IF_SENDER_BCS VALUE IS INITIAL,
L_RECIPIENT_MAIL TYPE ADR6-SMTP_ADDR.
DATA : ITAB TYPE TABLE OF SVAL,
LS_ITAB TYPE SVAL,
I_RETURN.
DATA: L_MSG_TYPE TYPE SO_OBJ_TP,
LO_SEND_REQUEST TYPE REF TO CL_BCS VALUE IS INITIAL.
DATA: LX_DOCUMENT_BCS TYPE REF TO CX_DOCUMENT_BCS VALUE IS INITIAL,
ATTACHMENT_SUBJECT TYPE SO_OBJ_DES.
DATA: LO_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS VALUE IS INITIAL.
DATA: LS_RECIPIENT LIKE LINE OF RECIPIENTS,
LS_ATTACHMENT LIKE LINE OF IT_ATTACHMENTS.
CLEAR: E_RESULT.
* Sender mail setting
IF I_SENDER_MAIL IS INITIAL.
I_SENDER_MAIL = 'xiuxianhai@xxx.com'.
ENDIF.
CASE I_ATTMSG_CHECK.
WHEN 'X'. "
CLEAR:L_MSG_TYPE.
WHEN 'H'.
L_MSG_TYPE = 'HTML'. " HTML格式
WHEN OTHERS.
L_MSG_TYPE = 'RAW'. " 普通格式
ENDCASE.
*Prepare Mail Object
CLASS CL_BCS DEFINITION LOAD.
LO_SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).
* Message body and subject
DATA: LO_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS VALUE IS INITIAL.
LO_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
I_TYPE = L_MSG_TYPE
I_TEXT = IT_MESSAGE_BODY
I_SUBJECT = I_SUBJECT ).
*Send attachment
LOOP AT IT_ATTACHMENTS 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.
* Pass the document to send request
LO_SEND_REQUEST->SET_DOCUMENT( LO_DOCUMENT ).
TRY.
IF I_SENDER_MAIL IS NOT INITIAL.
LO_SENDER = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( I_SENDER_MAIL ).
ENDIF.
* Set sender
LO_SEND_REQUEST->SET_SENDER(
EXPORTING
I_SENDER = LO_SENDER ).
CATCH CX_ADDRESS_BCS.
RETURN.
ENDTRY.
* Set recipients
LOOP AT RECIPIENTS INTO LS_RECIPIENT.
IF LS_RECIPIENT-SMTP_ADDR IS NOT INITIAL.
L_RECIPIENT_MAIL = LS_RECIPIENT-SMTP_ADDR .
TRY.
LO_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( L_RECIPIENT_MAIL ).
CATCH CX_ADDRESS_BCS .
ENDTRY.
ENDIF.
LO_SEND_REQUEST->ADD_RECIPIENT(
EXPORTING
I_RECIPIENT = LO_RECIPIENT
I_EXPRESS = 'X' ).
ENDLOOP.
TRY.
CALL METHOD LO_SEND_REQUEST->SET_STATUS_ATTRIBUTES
EXPORTING
I_REQUESTED_STATUS = 'E'
I_STATUS_MAIL = 'E'.
* set send immediately flag
LO_SEND_REQUEST->SET_SEND_IMMEDIATELY( 'X' ).
* Send email
LO_SEND_REQUEST->SEND(
EXPORTING
I_WITH_ERROR_SCREEN = 'X'
RECEIVING
RESULT = E_RESULT ).
COMMIT WORK.
WAIT UP TO 1 SECONDS.
CATCH CX_SEND_REQ_BCS.
E_RESULT = ''.
ENDTRY.
ENDFUNCTION.