SAP 邮件发送

时间:2022-06-21 17:22:20

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  WITH HEADER LINE, " 邮件正文的头信息
        RECEIVERS LIKE SOOS1 OCCURS  WITH HEADER LINE,
        PACKING_LIST LIKE SOXPL OCCURS  WITH HEADER LINE, " 邮件附件的头信息
        OBJCONT LIKE SOLI OCCURS  WITH HEADER LINE, " 邮件正文
        ATT_CONT LIKE SOLI OCCURS  WITH HEADER LINE, " 邮件附件
        ATT_HEAD LIKE SOLI OCCURS  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      = 
      COMMUNICATION_FAILURE      = 
      COMPONENT_NOT_AVAILABLE    = 
      FOLDER_NOT_EXIST           = 
      FOLDER_NO_AUTHORIZATION    = 
      FORWARDER_NOT_EXIST        = 
      NOTE_NOT_EXIST             = 
      OBJECT_NOT_EXIST           = 
      OBJECT_NOT_SENT            = 
      OBJECT_NO_AUTHORIZATION    = 
      OBJECT_TYPE_NOT_EXIST      = 
      OPERATION_NO_AUTHORIZATION = 
      OWNER_NOT_EXIST            = 
      PARAMETER_ERROR            = 
      SUBSTITUTE_NOT_ACTIVE      = 
      SUBSTITUTE_NOT_DEFINED     = 
      SYSTEM_FAILURE             = 
      TOO_MUCH_RECEIVERS         = 
      USER_NOT_EXIST             = 
      ORIGINATOR_NOT_EXIST       = 
      X_ERROR                    = 
      OTHERS                     = .

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 = ''
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.