發郵件的例子

sapkitty發表於2007-02-02

下面是一個利用通用模SO_NEW_DOCUMENT_ATT_SEND_API1發郵件的例子。

送的檔案內容及附件內容:

***************
Record No. Record Description Error Text
1 1st record 1st record in attachment
2 2st record 2st record in attachment
****************
file name was : Arcos Error Report.TXT.
mail body was : Short description of contents(subject)subject : SOME DESCRIPTION

[@more@]

REPORT ZPRG_MAIL.

*----------------------------------------------------------------------*
* PARAMETERS DEFINITION
*----------------------------------------------------------------------*
PARAMETERS:
P_EMAIL(40) TYPE C DEFAULT .

*----------------------------------------------------------------------*
* DATA DEFINITION
*----------------------------------------------------------------------*
DATA:
G_TABLE_LINES LIKE SY-TABIX, " table index
G_TAB TYPE X VALUE '09'. " TAB value

DATA:
X_DOC_CHNG LIKE SODOCCHGI1, " document attributes
IT_OBJPACK LIKE SOPCKLSTI1 OCCURS 0 WITH HEADER LINE,
" attachment table
IT_OBJHEAD LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE,
" object header table
IT_OBJBIN LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE,
" binary table
IT_OBJTXT LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE,
IT_RECLIST LIKE SOMLRECI1 OCCURS 0 WITH HEADER LINE.

*----------------------------------------------------------------------*
* INITIALIZATION
*----------------------------------------------------------------------*
INITIALIZATION.
CLEAR IT_RECLIST.
REFRESH IT_RECLIST.

*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.

*-fill email ids
IT_RECLIST-RECEIVER = P_EMAIL.
IT_RECLIST-REC_TYPE = 'U'.

*-append receiver table
APPEND IT_RECLIST.
CLEAR IT_RECLIST.

*-set document attributes
CLEAR: X_DOC_CHNG.
X_DOC_CHNG-OBJ_NAME = 'HEADING'.
X_DOC_CHNG-OBJ_DESCR = 'Short description of contents(subject)'.

*-set body text
IT_OBJTXT = 'This is the first line of the mail'.
APPEND IT_OBJTXT.
IT_OBJTXT = 'This is the second line of the mail'.
APPEND IT_OBJTXT.

*-document size
CLEAR G_TABLE_LINES.
DESCRIBE TABLE IT_OBJTXT LINES G_TABLE_LINES.
READ TABLE IT_OBJTXT INDEX G_TABLE_LINES.
X_DOC_CHNG-DOC_SIZE =
( G_TABLE_LINES - 1 ) * 255 + STRLEN( IT_OBJTXT ).

*-set packing list for body text
CLEAR IT_OBJPACK-TRANSF_BIN.
IT_OBJPACK-HEAD_START = 1.
IT_OBJPACK-HEAD_NUM = 0.
IT_OBJPACK-BODY_START = 1.
IT_OBJPACK-BODY_NUM = G_TABLE_LINES.
IT_OBJPACK-DOC_TYPE = 'RAW'.
APPEND IT_OBJPACK.
CLEAR IT_OBJPACK.

*-set object header
IT_OBJHEAD = 'Arcos Error Report'(057).

APPEND IT_OBJHEAD.
CLEAR IT_OBJHEAD.


*--for attachment ---start
*-populate object bin table for attachment
*-column header

CONCATENATE 'Record No.'
'Record Description'
'Error Text'
INTO IT_OBJBIN
SEPARATED BY G_TAB.

APPEND IT_OBJBIN.
CLEAR IT_OBJBIN.

*-error details
CONCATENATE '1'
'1st record'
'1st record in attachment'
INTO IT_OBJBIN
SEPARATED BY G_TAB.

APPEND IT_OBJBIN.
CLEAR IT_OBJBIN.

CONCATENATE '2'
'2st record'
'2st record in attachment'
INTO IT_OBJBIN
SEPARATED BY G_TAB.
APPEND IT_OBJBIN.
CLEAR IT_OBJBIN.

*-get total no.of lines of Object table(attachment)
CLEAR : G_TABLE_LINES.
DESCRIBE TABLE IT_OBJBIN LINES G_TABLE_LINES.

*-populate object header
IT_OBJHEAD = 'Report'.

APPEND IT_OBJHEAD.
CLEAR IT_OBJHEAD.

*-packing list for attachment
IT_OBJPACK-TRANSF_BIN = 'X'.
IT_OBJPACK-HEAD_START = 1.
IT_OBJPACK-HEAD_NUM = 1.
IT_OBJPACK-BODY_START = 1.
IT_OBJPACK-BODY_NUM = G_TABLE_LINES .
IT_OBJPACK-DOC_TYPE = 'RAW' .
IT_OBJPACK-OBJ_NAME = 'ABCD'.
IT_OBJPACK-OBJ_DESCR = 'ERROR REPORT'.
IT_OBJPACK-DOC_SIZE = G_TABLE_LINES * 255.
APPEND IT_OBJPACK.
CLEAR IT_OBJPACK.
*--code for attachment -- end


*-Sending the document
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
DOCUMENT_DATA = X_DOC_CHNG
PUT_IN_OUTBOX = 'X'
* IMPORTING
* SENT_TO_ALL =
* NEW_OBJECT_ID =

TABLES
PACKING_LIST = IT_OBJPACK
OBJECT_HEADER = IT_OBJHEAD
CONTENTS_BIN = IT_OBJBIN
CONTENTS_TXT = IT_OBJTXT
RECEIVERS = IT_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.
ENDIF.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8214011/viewspace-897022/,如需轉載,請註明出處,否則將追究法律責任。

相關文章