This is a test message to test the length of the message box.
ABAP RAP send mail
Created by Software-Heroes

RAP - Send mail


After we made the configuration in the last article, this time we will send an email and look at the specifics with RAP.

Last week we defined the basic configuration for sending mail and set up the connection to Office 365 in the ABAP environment. In the next step we want to send an email and implement an email dispatch in our RAP application.



In order to send the mail, we access the released APIs from SAP, which enable us to send this. With the classes under "CL_BCS_MAIL*" we have all the necessary components to send a first test email. In the first step we need a mail instance that we can prepare with the appropriate data.

DATA(lo_mail) = cl_bcs_mail_message=>create_instance( ).


In the second step, we define the sender and set possible recipients who should then receive the mail:

lo_mail->set_sender( '' ).
lo_mail->add_recipient( '' ).


In the third step we set the subject and fill the e-mail with a body. In our example, we rely on an HTML mail that we can still design accordingly. We define an H1 heading and a paragraph with some text in the text.

lo_mail->set_subject( 'Test Mail' ).

lo_mail->set_main( cl_bcs_mail_textpart=>create_instance(
    iv_content      = '<h1>Hello</h1><p>Hello world send from RAP!</p>'
    iv_content_type = 'text/html' ) ).


In the last step we can start the actual shipping. No commit has to be carried out here, this is triggered by the class and the mail dispatch is started. We then receive a table with the status of the recipients, so we can check the output to see whether all recipients have received the e-mail.

lo_mail->send( IMPORTING et_status = DATA(lt_status) ).


After execution, the e-mail is created and ends up in our inbox, here is the corresponding preview of the e-mail:


The complete executable class can be found here, a corresponding error handling and outputs to the console have also been implemented:

    INTERFACES if_oo_adt_classrun.

CLASS zcl_bs_demo_crap_mail IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
        DATA(lo_mail) = cl_bcs_mail_message=>create_instance( ).

        lo_mail->set_sender( '' ).
        lo_mail->add_recipient( '' ).

        lo_mail->set_subject( 'Test Mail' ).

        lo_mail->set_main( cl_bcs_mail_textpart=>create_instance(
            iv_content      = '<h1>Hello</h1><p>Hello world send from RAP!</p>'
            iv_content_type = 'text/html' ) ).

        lo_mail->send( IMPORTING et_status = DATA(lt_status) ).

        out->write( lt_status ).

      CATCH cx_bcs_mail INTO DATA(lo_err).
        out->write( lo_err->get_longtext( ) ).



In the ABAP Environment you will also find a corresponding app for sending mail to check the outgoing mail and view the corresponding status. To do this, you can find the "Monitor Email Transmissions" application via the search function.


It is still possible to navigate deeper to check the status for each recipient and thus analyze further error messages.



Let's now implement an email dispatch in our complex RAP object by providing an action. This action should display a popup in which we can enter a corresponding recipient. The selected entries should be sent as a file attachment via email. You can find out how to implement an action with a popup in another article, so here is a brief summary:

  • Definition of the abstract CDS entity for the popup
  • Definition of the action in the behavior definition
  • Enabling the action in the projection
  • Extension of the metadata to display the action
  • Implementation of the logic


During the implementation, we first create the file content for all selected entries in order to then transfer this to the e-mail:

     ENTITY Invoice
     RESULT DATA(lt_invoice).

DATA(ld_mail_content) = ``.
LOOP AT lt_invoice INTO DATA(ls_invoice).
  ld_mail_content &&= |Doc: { ls_invoice-Document }, From: { ls_invoice-Partner }


In the next step we determine the recipient, this was given via the popup of the action and is therefore variable. We receive the entered parameters via an entry in the KEYS table:

keys[ 1 ]-%param-ReceiverMail


We can now attach the attachment to the email using the appropriate method and determine the appropriate format and file name:

lo_mail->add_attachment( cl_bcs_mail_textpart=>create_instance( iv_content      = id_mail_content
                                                                iv_content_type = 'text/plain'
                                                                iv_filename     = 'Attachment.txt' ) ).


We can then send the e-mail again and receive it in our inbox after a short time:


Hint: If you want to send an email from a RAP campaign, this works up to STRICT MODE 1. With higher modes, there is a stricter check and there is a dump in the system when sending, and no more emails are sent. The background is the safeguarded data integrity, since commits are forbidden during RAP processing and outside of the save sequence.



To validate an e-mail address in the ABAP environment, there is the class CL_MAIL_ADDRESS with the method VALIDATE. For more information, see the official documentation.



In the current commit of our GitHub project to the blog, you will find the changes to the RAP object and the example code shown to understand the adjustments. Since many small changes were made, we refrain from displaying them in this blog.



You now know how to send an e-mail from the ABAP environment and what to watch out for. Likewise, you should now know the integration in RAP and what special features you should consider.

Included topics:
RAPBTPSend mail
Comments (0)

And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.

RAP - Deep Action in OData v4

Category - ABAP

In this article we will look at actions with deep structures, how we can create them and pass data to an API endpoint.


BTP - Connect On-Premise (Consumption Model v2)

Category - ABAP

In this article we want to provide another update on the connection of on-premise systems and how this is done with a communication arrangement.


RAP - Show app count (Tile)

Category - ABAP

This example is about displaying a counter on the tile of a Fiori Elements application and how such a thing can be implemented.


RAP - Generator (Fiori)

Category - ABAP

In this article we will look at the more complex RAP generator as a Fiori Elements application and what advantages you have with it.


RAP - Generator (ADT)

Category - ABAP

Today, let's take a look at the RAP Generator, which is already integrated into ABAP Development Tools, and how you can use it to easily build new RAP apps.