Creating a simple e-mail message

In this recipe, we will see how we can create a simple program that will send an e-mail (SAP Office Mail) to an SAP user AJON1. There are no attachments involved in this recipe. However, we will want the SAP user AJON1 to see a pop-up express message when the e-mail document is received in his or her inbox.

How to do it...

We will now perform the steps shown as follows:

  1. Declare two reference variables sendrequest and myrecpient to the classes cl_bcs and cl_sapuser_bcs.
    How to do it...
  2. We will then declare a variable for specifying the content of the e-mail email_text. This is based on the type bcsy_text. We also declare an object reference to the class cl_document_bcs with the name document.
    How to do it...
  3. We call the static factory method create_persistent of the class cl_bcs.
  4. The returned reference is stored in the sendrequest reference variable declared earlier.
    How to do it...
  5. An SAP user object is then created using the CREATE method. This will be used later for specifying the e-mail recipient. The returned object is stored in myrecipient variable. The corresponding object for the user AJON1 is created.
    How to do it...
  6. Next, we call the add_recipient method of the cl_bcs class for the object reference sendrequest. We supply the recipient through the myrecipient object and we pass the value 'X' for the i_express parameter.
    How to do it...
  7. Next, we create the e-mail text. A simple one-line text My first email content is added to the internal table email_text. In addition, the factory method create_document is called for class cl_document_bcs. The type RAW is specified through the parameter i_type.
    How to do it...
  8. We then call the SET_DOCUMENT method and pass the document object as the parameter. The send method of the class cl_bcs for the object reference sendrequest is also called.
    How to do it...
  9. Finally, we call the COMMIT WORK statement.
    How to do it...
  10. The entire code block is placed within a TRY .. ENDTRY statement and the cx_bcs exception class will be used for catching exceptions.

How it works...

We created object references for the send request and the e-mail document as well for the recipient user object. The static factory method create_persistent of the cl_bcs class is called in order to create a send request.

Then, we define appropriate text for the content of the e-mail body and add it to the created send request along with the subject of the e-mail.

A recipient object (based upon the class cl_sapuser_bcs) is created for the SAP user AJON1. This recipient is then added to the send request using the method add_recipient of the cl_bcs class. For the i_express parameter of the add_recipient method, the value 'X' is passed so that the user receives an express message when the e-mail is received in his or her inbox. A new document is created using the static create_document method of the cl_document_bcs class. The document is having type RAW and relevant subject and content. The add_document method is then called in order to add the document to the send request. Finally, the COMMIT WORK statement is called and the e-mail is sent.

An express message is generated as shown in the following screenshot:

How it works...

The message appears in the document (Unread Documents) of the SAP Business Workplace (transaction SBWP). The message header for the corresponding message appears as shown in the following screenshot:

How it works...

It contains the red icon that denotes an express message has been generated. The title (Title), author name (Author), and the date received (Date received) are shown as well. The preview of the document body is shown in the following screenshot:

How it works...

The My first email content text is the e-mail text, whereas as the my email subject text is the subject as coded in the program.

There's more...

In case the e-mail is to be sent immediately, the SET_SEND_IMMEDIATELY method of the cl_bcs class must be called before the SEND method (and the COMMIT WORK statement), as shown as follows:

     SENDREQUEST->SET_SEND_IMMEDIATELY( 'X' ).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.57.126