Easily send emails from ABAP using Custom utility class ZCL_SEND_MAIL

in the previous post,s I shared how to use CL_BCS class to send emails. and I also shared how to use cl_bcs_message class to send email in user-exit and BADI.

The standard libraries even though useful, are cumbersome to use so I have created a utility that combines the power of both CL_BCS and CL_BCS_MESSAGE to let you send an email with ease from anywhere in the system. Below are examples and source links to the utility class ZCL_SEND_MAIL

A simple example:

"Initiate Send request
" Provide Subject and EMail bodu text.
DATA(lo_send_request) = zcl_send_mail=>init_send_request( iv_subject = `my test email`
it_body_text = cl_bcs_convert=>string_to_soli(`My Email body`)
).
"SEt sender(optional) by providing username/No input(Current username)/email address
lo_send_request->set_sender( ).
"add recipient by providing username/No input(Current username)/email address
lo_send_request->add_recipient( 'USER@domain.com' )."Call multiple times to add multiple recipients.
"Add Text or Binary attachment.. call as many times as the number of attachments.
lo_send_request->add_soli_attachment( iv_ext = 'TXT'
iv_title = 'My Text File'
it_content = cl_bcs_convert=>string_to_soli(`My text file contents`)
).
lo_send_request->send_with_commit( )."Auto commit post send.

Sending email from user exit/Badi

"Initiate Send request
" Provide Subject and EMail bodu text.
DATA(lo_send_request) = zcl_send_mail=>init_update_task_send_request( iv_subject = `my test email`
it_body_text = cl_bcs_convert=>string_to_soli(`My Email body`)
).
"SEt sender(optional) by providing username/No input(Current username)/email address
lo_send_request->set_sender( ).
"add recipient by providing username/No input(Current username)/email address
lo_send_request->add_recipient( 'USER@domain.com' )."Call multiple times to add multiple recipients.
"Add Text or Binary attachment.. call as many times as the number of attachments.
lo_send_request->add_soli_attachment( iv_ext = 'TXT'
iv_title = 'My Text File'
it_content = cl_bcs_convert=>string_to_soli(`My text file contents`)
).
lo_send_request->send( ). "the email is sent in update task which is triggered on next commit work

Link to Source: ZCL_SEND_MAIL

As usual, Hope you like the post and use the information in your day to day work.


Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP