ABAP 邮件发送示例代码
*&---------------------------------------------------------------------*
*& Report YN_DEMO_FOR_MAIL
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YN_DEMO_FOR_MAIL.
*----------------------------------------------------------------------------------------------------------------------------------------------------
* MODE1
*----------------------------------------------------------------------------------------------------------------------------------------------------
**类: CL_BCS 发送邮件主要用到的功能类, 包括创建发送请求, 添加发送内容,添加发送地址, 到最终的发送指令发出.
**类: CL_DOCUMENT_BCS, 用来放置发送的内容.
**类: CX_BCS, 不用多说,这是个异常类, 用于捕捉发送邮件过程中出现的异常.
**接口: IF_RECIPIENT_BCS, 用来做邮件地址的存储转换.
*DATA: SEND_REQUEST TYPE REF TO CL_BCS,
* DOCUMENT TYPE REF TO CL_DOCUMENT_BCS,
* FAIL TYPE REF TO CX_BCS,
* RECIPIENT TYPE REF TO IF_RECIPIENT_BCS.
*
*DATA: LS TYPE STRING,
* MAILTO TYPE AD_SMTPADR,
* MAIN_TEXT TYPE BCSY_TEXT,
* TITLE TYPE SO_OBJ_DES.
*LS = '该邮件用于测试演示程序'.
*
*
*APPEND LS TO MAIN_TEXT.
*TITLE = 'WUWEI邮件测试'.
*MAILTO = '1974981766@qq.COM'.
*
*TRY.
** 第一步: 创建发送请求
* SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).
**第二步: 创建整理发送内容
* DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
* I_TYPE = 'RAW'
* I_TEXT = MAIN_TEXT
* I_SUBJECT = TITLE ).
**第三步: 添加邮件内容到发送请求
* SEND_REQUEST->SET_DOCUMENT( DOCUMENT ).
** 第四步: 邮件地址转换
* RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( MAILTO ).
**第五步: 添加邮件地址到发送请求
* SEND_REQUEST->ADD_RECIPIENT( RECIPIENT ).
**第六步: 正式发送并提交作业
* SEND_REQUEST->SEND( I_WITH_ERROR_SCREEN = 'X' ).
* COMMIT WORK AND WAIT.
*
* CATCH CX_BCS INTO FAIL.
*ENDTRY.
*----------------------------------------------------------------------------------------------------------------------------------------------------
* MODE2
*----------------------------------------------------------------------------------------------------------------------------------------------------
*CONSTANTS:
* GC_TAB TYPE C VALUE CL_BCS_CONVERT=>GC_TAB, "CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
* GC_CRLF TYPE C VALUE CL_BCS_CONVERT=>GC_CRLF. "CL_ABAP_CHAR_UTILITIES=>CR_LF
*
*PARAMETERS: MAILTO TYPE AD_SMTPADR DEFAULT 'NATHANSUN@FOXMAIL.COM'. " 收件人
*
*DATA SEND_REQUEST TYPE REF TO CL_BCS.
*DATA DOCUMENT TYPE REF TO CL_DOCUMENT_BCS.
*DATA RECIPIENT TYPE REF TO IF_RECIPIENT_BCS.
*DATA BCS_EXCEPTION TYPE REF TO CX_BCS.
*
*DATA MAIN_TEXT TYPE BCSY_TEXT.
*DATA BINARY_CONTENT TYPE SOLIX_TAB.
*DATA SIZE TYPE SO_OBJ_LEN.
*DATA SENT_TO_ALL TYPE OS_BOOLEAN.
*
*
*START-OF-SELECTION.
*
* PERFORM CREATE_CONTENT.
* PERFORM SEND.
*
**&---------------------------------------------------------------------*
**& Form create_content
**&---------------------------------------------------------------------*
** Create Example Content
** 1) Write example text into a string
** 2) convert this string to solix_tab
**----------------------------------------------------------------------*
*FORM CREATE_CONTENT.
*
* DATA LV_STRING TYPE STRING.
* DATA LS_T100 TYPE T100.
*
** --------------------------------------------------------------
** as example content we use some system messages out of t100
** get them for all installed languages from db
** and write one line for each language into the spread sheet
** columns are separated by TAB and each line ends with CRLF
*
* CONCATENATE 'This Is Just Example Text!'
* GC_CRLF GC_CRLF
* INTO LV_STRING.
*
* DO 10 TIMES.
* CONCATENATE LV_STRING
* '1111111111111111111111111111111111111111111111111111111111111' GC_TAB
* '2222222222222222222222222222222222222222222222222222222222222' GC_TAB
* '3333333333333333333333333333333333333333333333333333333333333' GC_TAB
* 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' GC_TAB
* 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' GC_TAB
* 'ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc' GC_TAB
* 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd' GC_TAB
* 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' GC_TAB
* 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' GC_TAB
* 'ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg' GC_TAB
* 'hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh' GC_TAB
* 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii' GC_TAB
* 'jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj' GC_TAB
* 'kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk' GC_CRLF
* INTO LV_STRING.
* ENDDO.
** --------------------------------------------------------------
** convert the text string into UTF-16LE binary data including
** byte-order-mark. Mircosoft Excel prefers these settings
** all this is done by new class cl_bcs_convert (see note 1151257)
*
* TRY.
* CL_BCS_CONVERT=>STRING_TO_SOLIX(
* EXPORTING
* IV_STRING = LV_STRING
* IV_CODEPAGE = '4103' "suitable for MS Excel, leave empty
* IV_ADD_BOM = 'X' "for other doc types
* IMPORTING
* ET_SOLIX = BINARY_CONTENT
* EV_SIZE = SIZE ).
* CATCH CX_BCS.
* MESSAGE E445(SO).
* ENDTRY.
*
*ENDFORM. "create_content
*
**---------------------------------------------------------------
** NOTES:
**---------------------------------------------------------------
** UTF-16LE including the BOM (Byte order mark)
** is preferred by Microsoft Excel. If you want to create
** other binary content you may choose another codepage (e.g.
** '4110' (UTF-8) which is standard for e-mails).
** Find SAP codepage names in the drop down list
** for the codepage setting of node SMTP in transaction SCOT.
** Or: leave iv_codepage and iv_add_bom empty. Then the target
** codepage is set according to SAPconnect settings
**
** Important:
** SAP neither guarantees that the attachment created
** by this report can be opened by all Excel Versions nor
** that it can be opened by any 3rd party software at all
*
**&---------------------------------------------------------------------*
**& Form send
**&---------------------------------------------------------------------*
*FORM SEND.
*
* TRY.
*
** -------- create persistent send request ------------------------
* SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).
*
** -------- create and set document with attachment ---------------
** create document object from internal table with text
* APPEND 'Hello world!' TO MAIN_TEXT. " 邮件内容
* DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
* I_TYPE = 'RAW'
* I_TEXT = MAIN_TEXT
* I_SUBJECT = 'Test Created By BCS_EXAMPLE_7' ). " 邮件主题名
*
** add the spread sheet as attachment to document object
* DOCUMENT->ADD_ATTACHMENT(
* I_ATTACHMENT_TYPE = 'xls' " 附件格式
* I_ATTACHMENT_SUBJECT = 'ExampleSpreadSheet' " attachment name
* I_ATTACHMENT_SIZE = SIZE "附件大小
* I_ATT_CONTENT_HEX = BINARY_CONTENT ). "附件内容
*
** add document object to send request
* SEND_REQUEST->SET_DOCUMENT( DOCUMENT ).
*
** --------- add recipient (e-mail address) -----------------------
** create recipient object
* RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( MAILTO ).
*
** add recipient object to send request
* SEND_REQUEST->ADD_RECIPIENT( RECIPIENT ).
*
** ---------- send document ---------------------------------------
* SENT_TO_ALL = SEND_REQUEST->SEND( I_WITH_ERROR_SCREEN = 'X' ).
*
* COMMIT WORK.
* WAIT UP TO 2 SECONDS.
* SUBMIT RSCONN01 WITH MODE = 'INT'
* WITH OUTPUT = 'X'
* AND RETURN.
* IF SENT_TO_ALL IS INITIAL.
* MESSAGE I500(SBCOMS) WITH MAILTO.
* ELSE.
* MESSAGE S022(SO).
* ENDIF.
** ------------ exception handling ----------------------------------
** replace this rudimentary exception handling with your own one !!!
* CATCH CX_BCS INTO BCS_EXCEPTION.
* MESSAGE I865(SO) WITH BCS_EXCEPTION->ERROR_TYPE.
* ENDTRY.
*
*ENDFORM. "send
*----------------------------------------------------------------------------------------------------------------------------------------------------
* MODE3
*----------------------------------------------------------------------------------------------------------------------------------------------------
DATA: OBJBIN LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE,
DOCDATA LIKE SODOCCHGI1,
OBJTXT LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE,
OBJPACK LIKE SOPCKLSTI1 OCCURS 1 WITH HEADER LINE,
RECLIST LIKE SOMLRECI1 OCCURS 1 WITH HEADER LINE,
OBJHEAD LIKE SOLISTI1 OCCURS 1 WITH HEADER LINE.
DATA: TAB_LINES TYPE I.
*----------------------------------------------------------------------*
START-OF-SELECTION.
DOCDATA-OBJ_DESCR = '发到邮件自动OBJ_DESCR'.
DOCDATA-OBJ_NAME = 'OBJ_NAME'.
DOCDATA-OBJ_LANGU = SY-LANGU.
OBJTXT = '标题OBJTXT,你的用户名看到这个就代表成功了!'.
APPEND OBJTXT.
OBJTXT = DOCDATA-OBJ_DESCR.
APPEND OBJTXT.
OBJTXT = '这个是新发的!'.
APPEND OBJTXT.
* Write Packing List (Main)
* 3 has been fixed because OBJTXT has fix three lines
READ TABLE OBJTXT INDEX 3.
DOCDATA-DOC_SIZE = ( 3 - 1 ) * 255 + STRLEN( OBJTXT ).
CLEAR OBJPACK-TRANSF_BIN.
OBJPACK-HEAD_START = 1.
OBJPACK-HEAD_NUM = 0.
OBJPACK-BODY_START = 1.
OBJPACK-BODY_NUM = 3.
OBJPACK-DOC_TYPE = 'RAW'.
APPEND OBJPACK.
* RECLIST-RECEIVER = 'nathansun@foxmail.com'."如果是发到工作台就填登陆用户名,如果是发到email就添email地址
RECLIST-RECEIVER = 'IT08'.
TRANSLATE RECLIST-RECEIVER TO UPPER CASE.
RECLIST-REC_TYPE = 'B'."发到工作台
* RECLIST-REC_TYPE = 'U'."发到外部信箱
APPEND RECLIST.
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
DOCUMENT_DATA = DOCDATA
* PUT_IN_OUTBOX = ' '
COMMIT_WORK = 'X'
* IMPORTING
* SENT_TO_ALL =
* NEW_OBJECT_ID =
TABLES
PACKING_LIST = OBJPACK
* OBJECT_HEADER = OBJHEAD
* CONTENTS_BIN =
CONTENTS_TXT = OBJTXT
* CONTENTS_HEX =
* 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 SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
业务工作台(可查看邮件)事物码:SO01