Step1:Design selection-screen for email id input Normal Top^
This tutorial explains you sending emails using SAP ABAP programming language.
Go to SE38, create a program ZSAPN_SEND_EMAIL, save it in a local object.
Design a screen to enter email id, email subject, send immediatly flag.
PARAMETERS : P_EMAIL TYPE ADR6-SMTP_ADDR. "Emai input PARAMETERS: P_SUB TYPE CHAR50. "email subject PARAMETERS : P_SEND AS CHECKBOX. "send immediatly flag
Step2:Set email subject and body Important Top^
Declare and prepare email object for cl_bcs
*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( ).
Technically email is a binary document, we need to add document to the email object.Document contains parameters like email type (HTM, TXT etc), email subject, email body.
EMAIL BODY: We can send email body of 255 characters per line, email body might be more than 255 characters, so we need to pass it in the form of lines in an internal table (see example below).
* 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 email document with subject, body and type to send request.
* Pass the document to send request LO_SEND_REQUEST->SET_DOCUMENT( LO_DOCUMENT ).
Step3:Set Sender and Recipient to send request Important Top^
For every email, there is a sender and reciever (recipient), set sender and recipient for send request.
TRY...ENDTRY
is used for exception handleing in Object Oriented programming.
*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 for the send request
**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.
Step4:Set send email for send request Important Top^
In some servers network management team (BASIS) set mail sending frequency to reduce server load in SMTP(Simple Mail Transfer Protocol) configuration, in such cases the mails will be sent based on server load (may be some time later depends on server load)…In such cases we can set send immediately for the send request.
*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.
Finally send email using send request.
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.
Step5:Code to send email in SAP ABAP programming Important Top^
The final code for sending email in SAP ABAP using CL_BCS class is below.
REPORT ZSAPN_SEND_EMAIL. *Prepare Mail Object DATA: LO_SEND_REQUEST TYPE REF TO CL_BCS VALUE IS INITIAL. CLASS CL_BCS DEFINITION LOAD. 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 DATA: LO_SENDER TYPE REF TO IF_SENDER_BCS VALUE IS INITIAL. "sender DATA: LO_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS VALUE IS INITIAL. "recipient **Selection screen PARAMETERS : P_EMAIL TYPE ADR6-SMTP_ADDR. "Emai input PARAMETERS: P_SUB TYPE CHAR50. "email subject PARAMETERS : P_SEND AS CHECKBOX. "send immediatly flag START-OF-SELECTION. LO_SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ). * Message body and subject *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. 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 ). 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 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. TRY. CALL METHOD LO_SEND_REQUEST->SET_SEND_IMMEDIATELY EXPORTING I_SEND_IMMEDIATELY = P_SEND. "here selection screen input p_send * 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. "mail sent successfully WRITE :/ 'Mail sent successfully'. ENDIF. * CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION . *catch exception here ENDTRY.
Tutorial Comments
Total Comments:
Add your Comment
Good Sharing ! Everybody will write the program using OOPS Concept Good Sharing for every
Good Sharing ! Everybody will write the program using OOPS Concept Good Sharing for every
Hello, I have written the same code. But i get blank mail body infact it is attached as pdf (un-formtted text) please refer below code: FUNCTION ZMAIL_PR_CREATED . *”———————————————————————- *”*”Local Interface: *” IMPORTING *” REFERENCE(LV_SEND_USER_ID) TYPE FITP_USER-UNAME OPTIONAL *” REFERENCE(LV_REC_MAIL_ID) TYPE AD_SMTPADR OPTIONAL *” REFERENCE(LV_SEND_MAIL_ID) TYPE AD_SMTPADR OPTIONAL *” REFERENCE(LV_BANFN) TYPE BANFN *”———————————————————————- *FUNCTION-pool ZMAIL_PO_CREATED. *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 DATA: lx_document_bcs TYPE REF TO cx_document_bcs. DATA: lx_ADDRESS_BCS TYPE REF TO CX_ADDRESS_BCS. DATA: P_SUB TYPE CHAR50, “email subject P_EMAIL TYPE ADR6-SMTP_ADDR. “Email input DATA: wa_eban_temp TYPE eban, wa_zt16fc TYPE zt16fc. *CONCATENATE ‘PR Created : ‘lv_banfn ‘ Wating for ‘ wa_zt16fc-zfrgct ‘ Release’ INTO p_sub. SELECT SINGLE * from eban into wa_eban_temp WHERE banfn = lv_banfn. SELECT SINGLE * from zt16fc INTO wa_zt16fc WHERE frggr = wa_eban_temp-frggr and frgco = wa_eban_temp-FRGSt. P_EMAIL = wa_zt16fc-ZMAIL_ID. CONCATENATE ‘PR Created : ‘lv_banfn ‘, ‘ wa_zt16fc-zfrgct ‘ Release’ INTO p_sub. *Set body CONCATENATE ‘Dear ‘ wa_zt16fc-zfrgct ‘,’ INTO W_TEXT-LINE. *W_TEXT-LINE = ‘Dear,’. APPEND W_TEXT TO I_TEXT. CLEAR W_TEXT. W_TEXT-LINE = ‘ ‘. APPEND W_TEXT TO I_TEXT. CLEAR W_TEXT. CONCATENATE ‘PR’ lv_banfn ‘Created In System, Waiting for your release’ INTO W_TEXT-LINE. APPEND W_TEXT TO I_TEXT. CLEAR W_TEXT.W_TEXT-LINE = ‘ ‘. APPEND W_TEXT TO I_TEXT. CLEAR W_TEXT. W_TEXT-LINE = ‘This is the system generated mail for your notification’. APPEND W_TEXT TO I_TEXT. CLEAR W_TEXT. W_TEXT-LINE = ‘Do Not Reply’. 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 ). * Declare internal table and work area for MARA, add logic to get data from MARA. DATA : IT_eban TYPE TABLE OF eban, WA_eban TYPE eban. DATA : IT_makt TYPE TABLE OF makt, ” WA_makt TYPE makt. TYPES: BEGIN OF ty_final. include structure eban. types:maktx TYPE maktx, qty TYPE c, unit_price TYPE c, value_price TYPE c, END OF ty_final. DATA : IT_final TYPE TABLE OF ty_final, “internal table for Ekko WA_final LIKE LINE OF it_final. SELECT * from eban INTO TABLE it_eban WHERE banfn = lv_banfn. LOOP AT it_eban INTO wa_eban. wa_final-banfn = wa_eban-banfn. wa_final-matnr = wa_eban-matnr. SELECT SINGLE * from makt INTO wa_makt WHERE matnr = wa_eban-matnr. wa_final-maktx = wa_makt-maktx. wa_final-bsart = wa_eban-bsart. wa_final-ernam = wa_eban-ernam. wa_final-afnam = wa_eban-afnam. wa_final-menge = wa_eban-menge. wa_final-qty = wa_eban-menge. wa_final-meins = wa_eban-meins. wa_final-preis = wa_eban-preis. wa_final-unit_price = wa_final-preis. wa_final-rlwrt = wa_eban-rlwrt. wa_final-value_price = wa_final-rlwrt. APPEND wa_final to it_final. CLEAR: wa_final, wa_makt, wa_eban. ENDLOOP. *Declare strings, convert internal table data into string with horizantal tab (tab) and new line. DATA : LV_STRING TYPE STRING, “declare string LV_DATA_STRING TYPE STRING. “declare string CONCATENATE ‘PR NO.’ ‘Materal Name’ ‘Material No’ ‘Dco. Type’ ‘Requisitioner’ ‘Quantity’ ‘UOM’ ‘Unit_Price’ ‘Value’ 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. LOOP AT IT_final INTO WA_final. CONCATENATE WA_final-banfn WA_final-maktx WA_final-matnr WA_final-bsart WA_final-afnam WA_final-qty WA_final-meins WA_final-unit_price WA_final-value_price INTO LV_STRING SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB. * CONCATENATE ‘Vendor NO.’ ‘Vendor Name”Document Type’ ‘Created BY’ ‘Value’ 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_final, LV_STRING. ENDLOOP. *Convert string data into xstring using function module HR_KR_STRING_TO_XSTRING. 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. *Convert xstring data to binary data using function module SCMS_XSTRING_TO_BINARY . 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. *Create attachment with attachment name, attachment type (ex: XLS, TXT etc) and attachment data . DATA L_ATTSUBJECT TYPE SOOD-OBJDES. **add attachment name CLEAR L_ATTSUBJECT . CONCATENATE ‘PR 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 and Recipient to send request *For every email, there is a sender and reciever (recipient), set sender and recipient for send request. *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 INTO lx_ADDRESS_BCS. ****Catch exception here ENDTRY. *Set recipient for the send request **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 send email for send request *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. *Finally send email using send request. 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. ENDFUNCTION.
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/20133.html
Thank you very very very much…it is really an useful information to understand the sending email in root level, for oops learners…..Thanks you once again and please keep share the information on many like same…wishing you all the best……