[go: up one dir, main page]

0% found this document useful (0 votes)
218 views3 pages

EMAIL

This document shows the code to send an email with an attachment using SAP ABAP. It creates a mail object, sets the subject and body, retrieves data from MARA and converts it to an attachment, adds the attachment to the mail, sets the sender and recipient, and sends the email immediately, committing if sent successfully.

Uploaded by

Swati Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
218 views3 pages

EMAIL

This document shows the code to send an email with an attachment using SAP ABAP. It creates a mail object, sets the subject and body, retrieves data from MARA and converts it to an attachment, adds the attachment to the mail, sets the sender and recipient, and sends the email immediately, committing if sent successfully.

Uploaded by

Swati Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

PARAMETERS P_MTART TYPE MARA-MTART.

"material type input


PARAMETERS : P_EMAIL TYPE ADR6-SMTP_ADDR. "Email input
PARAMETERS: P_SUB TYPE CHAR50. "email subject
PARAMETERS : P_SEND AS CHECKBOX. "send immediately flag

*Prepare Mail Object


DATA: LO_SEND_REQUEST TYPE REF TO CL_BCS VALUE IS INITIAL.
CLASS CL_BCS DEFINITION LOAD.
LO_SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).

* Message body and subject


DATA: LO_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS VALUE IS INITIAL. "document
object
DATA : I_TEXT TYPE BCSY_TEXT. "Table for body
DATA : W_TEXT LIKE LINE OF I_TEXT. "work area for message body

*Set body
W_TEXT-LINE = 'This is the first tutorial of sending email using SAP ABAP
programming by SAPNuts.com'.
APPEND W_TEXT TO I_TEXT.
CLEAR W_TEXT.
W_TEXT-LINE = 'SAPNuts.com is the best SAP ABAP learning portal'.
APPEND W_TEXT TO I_TEXT.
CLEAR W_TEXT.

*Create Email document


LO_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT( "create document
I_TYPE = 'TXT' "Type of document HTM, TXT etc
I_TEXT = I_TEXT "email body internal table
I_SUBJECT = P_SUB ). "email subject here p_sub input parameter

* Pass the document to send request


LO_SEND_REQUEST->SET_DOCUMENT( LO_DOCUMENT ).

DATA : IT_MARA TYPE TABLE OF MARA, "internal table for MARA


WA_MARA TYPE MARA. "work area for MARA

**Get data from MARA


SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS
WHERE MTART = P_MTART.

DATA : LV_STRING TYPE STRING, "declare string


LV_DATA_STRING TYPE STRING, "declare string
LOOP AT IT_MARA INTO WA_MARA.
CONCATENATE WA_MARA-MATNR WA_MARA-MTART WA_MARA-MEINS WA_MARA-MBRSH WA_MARA-
MATKL INTO LV_STRING SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
CONCATENATE LV_DATA_STRING LV_STRING INTO LV_DATA_STRING SEPARATED BY
CL_ABAP_CHAR_UTILITIES=>NEWLINE.
CLEAR: WA_MARA, LV_STRING.
ENDLOOP.

DATA LV_XSTRING TYPE XSTRING .


**Convert string to xstring
CALL FUNCTION 'HR_KR_STRING_TO_XSTRING'
EXPORTING
* codepage_to = '8300'
UNICODE_STRING = LV_DATA_STRING
* OUT_LEN =
IMPORTING
XSTRING_STREAM = LV_XSTRING
EXCEPTIONS
INVALID_CODEPAGE = 1
INVALID_STRING = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
IF SY-SUBRC = 1 .

ELSEIF SY-SUBRC = 2 .
WRITE:/ 'invalid string ' .
ENDIF.
ENDIF.

DATA: LIT_BINARY_CONTENT TYPE SOLIX_TAB.


***Xstring to binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = LV_XSTRING
TABLES BINARY_TAB = LIT_BINARY_CONTENT.

DATA L_ATTSUBJECT TYPE SOOD-OBJDES.


**add attachment name
CLEAR L_ATTSUBJECT .
CONCATENATE 'Material Master Report' SY-DATUM INTO L_ATTSUBJECT.
* Create Attachment
TRY.
LO_DOCUMENT->ADD_ATTACHMENT( EXPORTING
I_ATTACHMENT_TYPE = 'XLS'
I_ATTACHMENT_SUBJECT = L_ATTSUBJECT
I_ATT_CONTENT_HEX = LIT_BINARY_CONTENT ).
* CATCH cx_document_bcs INTO lx_document_bcs.

ENDTRY.

*Set Sender
DATA: LO_SENDER TYPE REF TO IF_SENDER_BCS VALUE IS INITIAL.
TRY.
LO_SENDER = CL_SAPUSER_BCS=>CREATE( SY-UNAME ). "sender is the logged in
user

* Set sender to send request


LO_SEND_REQUEST->SET_SENDER(
EXPORTING
I_SENDER = LO_SENDER ).
* CATCH CX_ADDRESS_BCS.
****Catch exception here
ENDTRY.

**Set recipient
DATA: LO_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS VALUE IS INITIAL.
LO_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( P_EMAIL ). "Here
Recipient is email input p_email
TRY.
LO_SEND_REQUEST->ADD_RECIPIENT(
EXPORTING
I_RECIPIENT = LO_RECIPIENT
I_EXPRESS = 'X' ).
* CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
**Catch exception here
ENDTRY.

*Set immediate sending


TRY.
CALL METHOD LO_SEND_REQUEST->SET_SEND_IMMEDIATELY
EXPORTING
I_SEND_IMMEDIATELY = 'X'.
* CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
**Catch exception here
ENDTRY.

TRY.
** Send email
LO_SEND_REQUEST->SEND(
EXPORTING
I_WITH_ERROR_SCREEN = 'X' ).
COMMIT WORK.
IF SY-SUBRC = 0.
WRITE :/ 'Mail sent successfully'.
ENDIF.
* CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
*catch exception here
ENDTRY.

You might also like