SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow

How to Send Email using ABAP Code


ABAP programmers can create email, arrange mail content, set recipients of different recipient types and send email in ABAP code using standard ABAP classes. SAP users can display send status of every email sent within ABAP code using SAP transaction code SOST

ABAP developers can use ABAP classes cl_bcs, cl_document_bcs, cl_cam_address_bcs to create an email in SAP system, to define various recepients of the email and to send the email prepared to its recipients

It is possible to create email content using ABAP class cl_document_bcs method create_document.

data: send_request type ref to cl_bcs,
 mailsubject type so_obj_des,
 mailtext type bcsy_text,
 document type ref to cl_document_bcs,
 sender type ref to cl_cam_address_bcs,
 recipient_to type ref to cl_cam_address_bcs,
 recipient_cc type ref to cl_cam_address_bcs,
 recipient_bcc type ref to cl_cam_address_bcs,
 bcs_exception type ref to cx_bcs.

try.

 send_request = cl_bcs=>create_persistent( ).

 mailsubject = 'ABAP program for sending emails'.
 append 'Hello World,' to mailtext.
 append 'This is a sample ABAP code sending email.' to mailtext.

 document = cl_document_bcs=>create_document(
  i_type = 'RAW'
  i_text = mailtext
  i_subject = mailsubject ).
 send_request->set_document( document ).

 sender = cl_cam_address_bcs=>create_internet_address( 'sender@kodyaz.com' ).
 send_request->set_sender( sender ).

 recipient_to = cl_cam_address_bcs=>create_internet_address( 'recipient@yahoo.com' ).
 send_request->add_recipient( i_recipient = recipient_to ).

 recipient_cc = cl_cam_address_bcs=>create_internet_address( 'cc@hotmail.com' ).
 send_request->add_recipient( i_recipient = recipient_cc
 i_copy = 'X' ).

 recipient_bcc = cl_cam_address_bcs=>create_internet_address( 'bcc@gmail.com' ).
 send_request->add_recipient( i_recipient = recipient_bcc
  i_blind_copy = 'X' ).

 data(lv_sent_to_all) = send_request->send( ).
 if lv_sent_to_all = 'X'.
  write 'Email sent to all recipients'.
 else.
  write 'Email could not be sent to all recipients!'.
 endif.

 commit work.

 catch cx_bcs into bcs_exception.

 write: 'Error occurred while sending email: Error Type', bcs_exception->error_type.

endtry.
Code

After email is created and sent in SAP system, it is possible to display and check their status using SAP SOST transaction as seen in below screenshot.

emails sent from SAP using ABAP program in SOST transaction

SAP users can display content of the highlighted email in SOST transaction as well.

e-Mail created using ABAP code



SAP HANA and ABAP

Install SAP Free
CRM Companies List
Web Based CRM Software


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.