Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
mohit_dev
Contributor
68,036

Introduction


The history of email extends over more than 50 years, entailing an evolving set of technologies. Early dedicated machines and networks for sending text messages existed in the form of the telegraph, Telex and AUTODIN.

There can exist some scenarios in  SAP, where we are suppose to send email to someone with/without attachment.

To send an email from ABAP, you can use the function module "SO_NEW_DOCUMENT_SEND_API1", but in this blog post you will learn how to send an email using object oriented way. Though approach has also become old now, but still it would be good, if we revise it 😛 .

Requirement


Suppose we want to see a simple email like below in SAP:



But, lets first understand that how a simple HTML page looks like: (you can create a simple HTML file with extension ".html", I saved the file with the name "simple.html").
<!DOCTYPE html>
<html>
<body>
Hi Dear
<p> Content Section! </p>
</body>
</html>

If you open the above html file, the output looks like:



Similarly we will also create the email referring the above html code.

Steps


Lets achieve 🙂

  1. Create an executable local test program from SE38.

  2. Understand and refer the below code as per your requirement.


*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA : lo_mime_helper TYPE REF TO cl_gbt_multirelated_service,
lo_bcs TYPE REF TO cl_bcs,
lo_doc_bcs TYPE REF TO cl_document_bcs,
lo_recipient TYPE REF TO if_recipient_bcs,
lt_soli TYPE TABLE OF soli,
ls_soli TYPE soli,
lv_status TYPE bcs_rqst.

*&---------------------------------------------------------------------*
*& Creation of the mail
*&---------------------------------------------------------------------*

" Create the main object of the mail.
CREATE OBJECT lo_mime_helper.

" Create the mail content.-----"CLASSIC WAY"
*ls_soli-line = '<!DOCTYPE html PUBLIC “-//IETF//DTD HTML 5.0//EN">'.
*APPEND ls_soli TO lt_soli.

*ls_soli-line = '<HTML>'.
*APPEND ls_soli TO lt_soli.

*ls_soli-line = '<BODY>'.
*APPEND ls_soli TO lt_soli.

*ls_soli-line = 'Hi Dear,<P>Content Section!</P>'.
*APPEND ls_soli TO lt_soli.

*ls_soli-line = '</BODY>'.
*APPEND ls_soli TO lt_soli.

*ls_soli-line = '</HTML>'.
*APPEND ls_soli TO lt_soli.

" Create the mail content.-----"NEW WAY"
DATA(string) = '<!DOCTYPE html PUBLIC “-//IETF//DTD HTML 5.0//EN">'
&& '<HTML><BODY>Hi Dear,<P>Content Section!</P></BODY></HTML>'.

lt_soli = CL_DOCUMENT_BCS=>STRING_TO_SOLI( string ).

" Set the HTML body of the mail
CALL METHOD lo_mime_helper->set_main_html
EXPORTING
content = lt_soli
description = 'Test Email'.

* Set the subject of the mail.
lo_doc_bcs = cl_document_bcs=>create_from_multirelated(
i_subject = 'Subject of our email'
i_importance = '9' " 1~High Priority 5~Average priority 9~Low priority
i_multirel_service = lo_mime_helper ).

lo_bcs = cl_bcs=>create_persistent( ).

lo_bcs->set_document( i_document = lo_doc_bcs ).

* Set the email address
lo_recipient = cl_cam_address_bcs=>create_internet_address(
i_address_string = 'test@test12.com' ).

lo_bcs->add_recipient( i_recipient = lo_recipient ).

* Change the status.
lv_status = 'N'.
CALL METHOD lo_bcs->set_status_attributes
EXPORTING
i_requested_status = lv_status.

*&---------------------------------------------------------------------*
*& Send the email
*&---------------------------------------------------------------------*
TRY.
lo_bcs->send( ).
COMMIT WORK.
CATCH cx_bcs INTO DATA(lx_bcs).
ROLLBACK WORK.
ENDTRY.

3. Now, execute the above program and go to the transaction SOST, you can see an email against your name as below:



4. Now if you will display the email above, you can see your email as expected 🙂


Note


You can use, email templates as well, which will help you to avoid hard-coding of email content and subjects.

Please find the link below

https://blogs.sap.com/2019/10/12/e-mail-templates-in-s4-hana/

Conclusion


Yes!! In this way we can send a simple email having a subject and a content body with the above piece of code.

 

It would be very helpful in improving and adding more points to this blog post, if you can share your experiences in case you worked or explored with this approach.

In my next blog post, I will try to touch a new add on with the above approach.

For any issues, improvements, additions or any other concerns, please feel free to contact me.

I look forward for your feedback and suggestions.

Keep learning!! Keep improving!!
21 Comments
Labels in this area