SAP 邮件发送
1.********************
- *调用发送邮件函数
- CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
- EXPORTING
- DOCUMENT_DATA = DOC_CHNG
- "subject and description
- PUT_IN_OUTBOX = 'X' "send in_outbox
- TABLES
- PACKING_LIST = OBJPACK
- OBJECT_HEADER = OBJHEAD
- CONTENTS_BIN = OBJBIN "attachments
- CONTENTS_TXT = OBJTXT "mail body
- RECEIVERS = RECLIST "RECEIVE LIST
- EXCEPTIONS
- TOO_MANY_RECEIVERS = 1
- DOCUMENT_NOT_SENT = 2
- OPERATION_NO_AUTHORIZATION = 4
- OTHERS = 99.
- CASE SY-SUBRC.
- WHEN 0.
- * 当执行邮件发送成功后,确认提交,不调用此过程邮件将队列等待直到确认后才正式发送
- * 遇到错误信息输出提示
- SUBMIT RSCONN01
- WITH MODE EQ 'INT'
- AND RETURN.
- MESSAGE S001(00) WITH 'Sent Mail success!'.
- WHEN 1.
- WRITE: / 'No authorization for sending to the specified number',
- 'of recipients'.
- WHEN 2.
- WRITE: / 'Document could not be sent to any recipient'.
- WHEN 4.
- WRITE: / 'No send authorization'.
- WHEN OTHERS.
- WRITE: / 'Error occurred while sending'.
- ENDCASE.
2。********************************
DATA: OBJECT_HD_CHANGE LIKE SOOD1 OCCURS 10 WITH HEADER LINE, " 邮件正文的头信息 RECEIVERS LIKE SOOS1 OCCURS 10 WITH HEADER LINE, PACKING_LIST LIKE SOXPL OCCURS 10 WITH HEADER LINE, " 邮件附件的头信息 OBJCONT LIKE SOLI OCCURS 10 WITH HEADER LINE, " 邮件正文 ATT_CONT LIKE SOLI OCCURS 10 WITH HEADER LINE, " 邮件附件 ATT_HEAD LIKE SOLI OCCURS 10 WITH HEADER LINE. " 头行 OBJECT_HD_CHANGE-OBJLA = 'E'. "创建文档使用的语言 OBJECT_HD_CHANGE-OBJNAM = 'LIST'. "文档,文件夹或分配清单的名称 OBJECT_HD_CHANGE-OBJDES = TITLE. "内容的简短描述CHAR50 OBJECT_HD_CHANGE-OBJSNS = 'O'. "对象:灵敏度 P机密 F功能 O标准 APPEND OBJECT_HD_CHANGE. LOOP AT REC_NAME. RECEIVERS-RECEXTNAM = REC_NAME. RECEIVERS-RECESC = 'U'. APPEND RECEIVERS. ENDLOOP. LOOP AT ZOBJCONT. OBJCONT-LINE = ZOBJCONT-LINE. APPEND OBJCONT. ENDLOOP. CALL FUNCTION 'SO_OBJECT_SEND' EXPORTING OBJECT_HD_CHANGE = OBJECT_HD_CHANGE OBJECT_TYPE = 'RAW' " RAW SAP编辑程序文件 SENDER = SY-UNAME " 发送者用户名 TABLES OBJCONT = OBJCONT " Content RECEIVERS = RECEIVERS " 接收人地址 PACKING_LIST = PACKING_LIST " 邮件内容 ATT_CONT = ATT_CONT " 附件 ATT_HEAD = ATT_HEAD " 标题 EXCEPTIONS ACTIVE_USER_NOT_EXIST = 1 COMMUNICATION_FAILURE = 2 COMPONENT_NOT_AVAILABLE = 3 FOLDER_NOT_EXIST = 4 FOLDER_NO_AUTHORIZATION = 5 FORWARDER_NOT_EXIST = 6 NOTE_NOT_EXIST = 7 OBJECT_NOT_EXIST = 8 OBJECT_NOT_SENT = 9 OBJECT_NO_AUTHORIZATION = 10 OBJECT_TYPE_NOT_EXIST = 11 OPERATION_NO_AUTHORIZATION = 12 OWNER_NOT_EXIST = 13 PARAMETER_ERROR = 14 SUBSTITUTE_NOT_ACTIVE = 15 SUBSTITUTE_NOT_DEFINED = 16 SYSTEM_FAILURE = 17 TOO_MUCH_RECEIVERS = 18 USER_NOT_EXIST = 19 ORIGINATOR_NOT_EXIST = 20 X_ERROR = 21 OTHERS = 22.
3.**************************
TRY. * 创建发送请求 l_send_request = cl_bcs=>create_persistent( ). * 设定发送内容 l_document = cl_document_bcs=>create_document( i_type = 'RAW' i_text = i_content[] I_IMPORTANCE = '1' i_subject = l_subject ). * 增加发送内容到发送请求 CALL METHOD l_send_request->set_document( l_document ). * 取得发送者(取得发件人,前提是这个邮箱地址能发邮件,并且不需要密码) l_uname = sy-uname. l_sender = cl_sapuser_bcs=>create( l_uname ). CALL METHOD l_send_request->set_sender EXPORTING i_sender = l_sender. * 设置收件人 LOOP AT it_mailaccept. CONCATENATE it_mailaccept-name '@XXXXX.COM' INTO i_email. TRANSLATE i_email TO LOWER CASE. l_recipient = cl_cam_address_bcs=>create_internet_address( i_email ). CALL METHOD l_send_request->add_recipient EXPORTING i_recipient = l_recipient i_express = 'X' i_copy = ' ' i_blind_copy = ' ' i_no_forward = ' '. ENDLOOP. * 设置抄送人 LOOP AT it_mailaccept_copy. CONCATENATE it_mailaccept_copy-name '@XXXXX INTO i_email. TRANSLATE i_email TO LOWER CASE. l_recipient = cl_cam_address_bcs=>create_internet_address( i_email ). CALL METHOD l_send_request->add_recipient EXPORTING i_recipient = l_recipient i_express = 'X' i_copy = 'X' i_blind_copy = ' ' i_no_forward = ' '. ENDLOOP. *& 立即发送 l_send_request->set_send_immediately( 'X' ). *& 发送 CALL METHOD l_send_request->send( ). COMMIT WORK. CATCH cx_document_bcs INTO l_bcs_exception. CATCH cx_send_req_bcs INTO l_send_exception. CATCH cx_address_bcs INTO l_addr_exception. ENDTRY.