Technical Articles
Object Oriented way of sending an email from ABAP side.
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 🙂
- Create an executable local test program from SE38.
- 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!!
Appreciate your efforts, Small suggestion – Add TRY…CATCH…ENDTRY block
Thanks S Nalluri for your appreciation; Regarding exception handling, I will surely add that soon as well. 🙂
Yes, your sy-subrc check isn't useful.
It’s sad that ABAP gurus promoted BCS for 15 years, as SAP did, and that people still use this old function module (and other old-fashioned ones).
There are also demo programs provided in every ABAP system, prefixed BCS_EXAMPLE_*.
I’m interested by your “add on”, I don’t know what it is, but if it solves the main issue which is, according to me, that a text or binary document needs to be passed via an internal table instead of string/xstring (many other old “APIs” use internal tables too, unfortunately), which leads to frequent errors like the actual length of the document being “corrupted” (error with PDF files especially), I would be happy!
Thanks Sandra Rossi for your feedback.
It's chilling that the 2006 blog title Unknown Thus Unloved remains somewhat accurate to this day.
I usually just use class CL_DOCUMENT_BCS.
A quick suggestion for a project, use GITHUB. Blog and then point people to your project.
Very cool. Now the blogs to get the people there and helping out.
Thank you!
I published a blog based on email templates which will help to avoid Hardcoding of email content and subjects.
You can try to integrate in your program.
https://blogs.sap.com/2019/10/12/e-mail-templates-in-s4-hana/
Thanks Prabhjot, Its an excellent blog. 🙂
I will try it; at the meantime instead, I will add the link of your blog to present as a good practice. Thanks!!
Hi,
nice blog! I create a send_mail method using cl_bcs framework myself like this:
Signature:
You can attach files and overcome the limitation of 50 characters in the subject (in SOST the short subject is displayed).
Best regards,
Mark
Thanks Mark, it indeed is a good way too 🙂
You should also make your code compliant with the published SAP guidelines on variable naming (and also part of the clean coding project).
Stop using Hungarian notation! 🙂
Other than that, it's good to promote this 15 year old knowledge, as people still insist on using the function modules. Maybe one day they'll learn how to use hashed tables... :-p
Thanks Matthew for your feedback 🙂
Hello Mohit,
I have tried the same procedure that you have shown with simple mail body(HTML content). But in SOST it shows error like 'Currently no delivery to Recipient'.
I can send mail of body type 'RAW', but not HTM.
I have tried following two ways. But still it is not working
1. DATA(lref_document) = cl_document_bcs=>create_document(
i_type = 'HTM'
i_text = lt_body
i_subject = 'Testing(html)'
i_importance = '1'
).
2. 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 ).
Could you please help me out, how can i resolve my issue.
And also please let me know that if i need to do any additional configuration for HTML content mail sending.
Thanks in advance.
Best Regards,
Jagadeesh
Hi Jagadeesh,
You will have to check this with your BASIS colleagues, as this sometimes occurs due to insufficient resources on the application server.
In case, you find any other solution or reason, please help us all with that as well.
Thanks and Regards,
Mohit Sharma
Hi Mohit,
Thank you for nice blog.
I want set multiple email address as 'Reply To' email address using method SET_REPLY_TO, but unfortunately, only last email address in internal table can set as 'Reply To' email address.
For your information, I can add multiple e-mail addresses as 'reply to' email adresses in Microsoft Outlook (please refer to How to Change the “Reply To” Address for Email Messages in Outlook).
Could you give advice for solve this problem?
Best regards,
Andy Pataselano
Hi Andy,
You can loop over your recipients and add them via method add_recipient.
Regards,
Mohit Sharma
Hi Mohit,
'Reply To' is not same as recipients. So, I can't add 'Reply To' via method add_recipient.
Best regards,
Andy Pataselano
How to send mail with 2 attachments from application server.