HOW TO SEND EMAIL IN SAP ABAP


HOW TO SEND EMAIL IN SAP ABAP OR OOPS ABAP

*&---------------------------------------------------------------------*
*& Report ZSEND_EMAIL
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zsend_email.

*data: lv_value
*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 : it_text TYPE bcsy_text. "Table for body
DATA : wa_text LIKE LINE OF it_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_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
***Attachment data
DATA : it_mara TYPE TABLE OF mara,
wa_mara TYPE mara.
DATA : lv_string TYPE string,
lv_data_string TYPE string,
lv_xstring TYPE xstring.
DATA: lit_binary_content TYPE solix_tab,
l_attsubject TYPE sood-objdes.
START-OF-SELECTION.
"create object for sending email

lo_send_request = cl_bcs=>create_persistent( ).
*****************************start of bocy***************************************************
* Message body
*1st Step - Setting up the body
* wa_text-line = 'Hi Students,'.
* APPEND wa_text TO it_text.
* wa_text-line = 'This is the Inner Circle Call and Try to Learn how we can send the email with attachment'.
* APPEND wa_text TO it_text.
* CLEAR wa_text.
* wa_text-line = 'Best Regards, Pankaj kumar'.
* APPEND wa_text TO it_text.
* CLEAR wa_text.
"Message Body
wa_text-line = '<HTML><BODY>'.
APPEND wa_text TO it_text.
wa_text-line = 'Dear Recepient,'.
APPEND wa_text TO it_text.
wa_text-line = '<br></br>'.
APPEND wa_text TO it_text.
wa_text-line = 'Please find the Flight Details as requested in the attachment.'.
APPEND wa_text TO it_text.
wa_text-line = '<br></br>'.
APPEND wa_text TO it_text.
wa_text-line = 'Thanks & Regards , Sender'.
APPEND wa_text TO it_text.
wa_text-line = '</BODY></HTML>'.
APPEND wa_text TO it_text.
CLEAR wa_text.
*************************end of body*******************************************************
lo_document = cl_document_bcs=>create_document(
EXPORTING "create document
i_type = 'HTM' "Type of document HTM, TXT etc
i_text = it_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 ).
***Set attachment
SELECT * FROM mara INTO TABLE it_mara UP TO 100 ROWS
WHERE mtart = p_mtart.

*
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.
ENDLOOP.
***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.
***Xstring to binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_xstring
TABLES
binary_tab = lit_binary_content.
**add attachment
CLEAR l_attsubject .
CONCATENATE 'Innercircle_Excel' 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 DATA(lx_document_bcs).
ENDTRY.
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.
"scheduel when you want send the email
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.

Pankaj Kumar
A California-based travel writer, lover of food, oceans, and nature.

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
SAP TECH MADE EASY 2024 Privacy policy Terms of use Contact us Refund policy