Sample Email Program in SAP ABAP
CONSTANTS:
LC_SUBJECT TYPE SO_OBJ_DES VALUE 'NEW Mail', " Subject LineLC_RAW TYPE CHAR03 VALUE 'RAW'.
DATA:
LV_MLREC TYPE SO_OBJ_NAM,
LV_SENT_TO_ALL TYPE OS_BOOLEAN,
LV_EMAIL TYPE ADR6-SMTP_ADDR,
LV_SUBJECT TYPE SO_OBJ_DES,
LV_TEXT TYPE BCSY_TEXT,
LR_SEND_REQUEST TYPE REF TO CL_BCS,
LR_BCS_EXCEPTION TYPE REF TO CX_BCS,
LR_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS,
LR_SENDER TYPE REF TO CL_SAPUSER_BCS,
LR_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS.
TRY.
"Create send request
LR_SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).
"Email From
LR_SENDER = CL_SAPUSER_BCS=>CREATE( SY-UNAME ).
"Add sender to send request
CALL METHOD LR_SEND_REQUEST->SET_SENDER
EXPORTING
I_SENDER = LR_SENDER.
"Email Send To...
LV_EMAIL = 'abap.support@XXXXXX.com'.
LR_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( LV_EMAIL ).
"Add recipient to send request
CALL METHOD LR_SEND_REQUEST->ADD_RECIPIENT
EXPORTING
I_RECIPIENT = LR_RECIPIENT
I_EXPRESS = 'X'.
"Email BODY
APPEND 'Hello world! My first ABAP email using Class CL_BCS!' TO LV_TEXT.
LR_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
I_TYPE = LC_RAW
I_TEXT = LV_TEXT
I_SUBJECT = LC_SUBJECT ).
"Attach doc
CALL METHOD LR_SEND_REQUEST->SET_DOCUMENT( LR_DOCUMENT ).
"Mail Trigger
CALL METHOD LR_SEND_REQUEST->SEND(
EXPORTING
I_WITH_ERROR_SCREEN = 'X'
RECEIVING
RESULT = LV_SENT_TO_ALL ).
IF LV_SENT_TO_ALL = 'X'.
WRITE 'Email sent successully!'.
ENDIF.
"Commit to send email
COMMIT WORK. "MANDATORY*
"Exception handling
CATCH CX_BCS INTO LR_BCS_EXCEPTION.
WRITE:
'Error!',
'Error type:',
LR_BCS_EXCEPTION->ERROR_TYPE.
ENDTRY.
No comments:
Post a Comment