100% found this document useful (1 vote)
2K views

Send An HTML Email Using CL - BCS Class

The document describes how to send an HTML email using the CL_BCS class in ABAP. It includes: 1) Creating persistent send request and document objects to hold the HTML content and metadata. 2) Populating an HTML string with sample data from a loop and converting it to the required format. 3) Adding sender, recipient, and document to the send request before calling the send method. 4) Catching any exceptions from the send process and displaying error messages.

Uploaded by

a3scribd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

Send An HTML Email Using CL - BCS Class

The document describes how to send an HTML email using the CL_BCS class in ABAP. It includes: 1) Creating persistent send request and document objects to hold the HTML content and metadata. 2) Populating an HTML string with sample data from a loop and converting it to the required format. 3) Adding sender, recipient, and document to the send request before calling the send method. 4) Catching any exceptions from the send process and displaying error messages.

Uploaded by

a3scribd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Send an Html Email (using CL_BCS class)

DATA: send_request
TYPE REF TO cl_bcs.
DATA: document
TYPE REF TO cl_document_bcs.
DATA: sender
TYPE REF TO cl_sapuser_bcs.
DATA: recipient
TYPE REF TO if_recipient_bcs.
DATA: exception_info
TYPE REF TO if_os_exception_info,
bcs_exception
TYPE REF TO cx_bcs,
v_subj(50),
t_hex TYPE solix_tab,
html_string TYPE string,
xhtml_string TYPE xstring,
v_message(100),
v_mail TYPE sza5_d0700-smtp_addr.
v_subj = 'Subject. This is an Email with HTML'.
CONCATENATE '<html><strong>Example Html</strong><br/><br/>'
'<table style="background-color: #CDCDCD; border: 1px solid black;">'
'<tr><th style="color:red;">Id Sales Order</th>'
'<th style="color:red;">Type</th><th style="color:red">Message</th></tr>'
INTO html_string .
LOOP AT t_htmldata. "some html data
CONCATENATE html_string '<tr><td style="border-right: 1px solid red;">'
t_htmldata-field1 '</td><td style="border-right: 1px solid red;">'
t_htmldata-field2 '</td><td>' t_htmldata-field3
'</td></tr>'
INTO html_string.
ENDLOOP.
CONCATENATE html_string '</table></html>' INTO html_string.
TRY.
*Create persistent send request
send_request = cl_bcs=>create_persistent( ).
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text
= html_string
*
MIMETYPE
= ' '
*
ENCODING
=
IMPORTING
buffer
= xhtml_string
EXCEPTIONS
failed
= 1
OTHERS
= 2.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer
= xhtml_string
*
APPEND_TO_TABLE
= ' '
* IMPORTING
*
OUTPUT_LENGTH
=
TABLES
binary_tab
= t_hex.
document = cl_document_bcs=>create_document(
i_type
= 'HTM'
i_hex
= t_hex
i_subject = v_subj ).
* Add document to send request

CALL METHOD send_request->set_document( document ).


* Get sender object
sender = cl_sapuser_bcs=>create( sy-uname ).
* Add sender
CALL METHOD send_request->set_sender
EXPORTING
i_sender = sender.
v_mail = 'example@example.com'.
recipient = cl_cam_address_bcs=>create_internet_address( v_mail ).
* Add recipient with its respective attributes to send request
CALL METHOD send_request->add_recipient
EXPORTING
i_recipient = recipient.
* Set that you don't need a Return Status E-mail
DATA: status_mail TYPE bcs_stml.
status_mail = 'N'.
CALL METHOD send_request->set_status_attributes
EXPORTING
i_requested_status = status_mail
i_status_mail
= status_mail.
* set send immediately flag
send_request->set_send_immediately( 'X' ).
* Send document
CALL METHOD send_request->send( ).
COMMIT WORK.
CATCH cx_bcs INTO bcs_exception.
v_message = bcs_exception->get_text( ).
MESSAGE e000(su) WITH v_message.
ENDTRY.

SAP ABAP - Sending Mail in HTML format


The following are the simple steps.

Create one standard text at SO10 transaction code. e.g. ZTST_VKO.

Text with HTML code.


<html>
<head>
</head>
<body>
<p>Hello friends</p>
<P>This program demonstrates on how to send mail with HTML body. You can use
HTML tags.</p>
<p><font color=red> This is in red color</font></p>
<p><font size=5> This font size is big</font></p>
<p>Thank You</p>
</body>
</html>

Create one program at SE38. e.g. zvko_simple_html_mail


Program code.
REPORT zvko_simple_html_mail.
*&-------------------------------------------------------------------*
"Declaration
*&-------------------------------------------------------------------*
"Variables
DATA :
g_sent_to_all
TYPE sonv-flag,
g_tab_lines
TYPE i.
"Types
TYPES:
t_document_data TYPE sodocchgi1,
t_packing_list
TYPE sopcklsti1,
t_attachment
TYPE solisti1,
t_body_msg
TYPE solisti1,
t_receivers
TYPE somlreci1.
"Workareas
DATA :
w_document_data TYPE t_document_data,
w_packing_list
TYPE t_packing_list,
w_attachment
TYPE t_attachment,
w_body_msg
TYPE t_body_msg,
w_receivers
TYPE t_receivers.
"Internal Tables
DATA :
i_document_data TYPE STANDARD TABLE OF t_document_data,
i_packing_list
TYPE STANDARD TABLE OF t_packing_list,
i_attachment
TYPE STANDARD TABLE OF t_attachment,
i_body_msg
TYPE STANDARD TABLE OF t_body_msg,

DATA:

i_receivers

TYPE STANDARD TABLE OF t_receivers.

lt_datatab TYPE tdtab_c132,


lw_data
LIKE LINE OF lt_datatab.
DATA :i_body
TYPE STANDARD TABLE OF tline WITH HEADER LINE.
*&-------------------------------------------------------------------*
"Selection Screen
*&-------------------------------------------------------------------*
PARAMETERS:p_mail TYPE char120.
*&-------------------------------------------------------------------*
"Start-of-selection
*&-------------------------------------------------------------------*
START-OF-SELECTION.
"Subject of the mail.
w_document_data-obj_name = 'MAIL_TO_HEAD'.
w_document_data-obj_descr = 'SAP ABAP - Mail in HTML format'.
"Body of the mail
CALL FUNCTION 'READ_TEXT'
EXPORTING
client
= sy-mandt
id
= 'ST'
language = sy-langu
name
= 'ZTST_VKO'
object
= 'TEXT'
TABLES
lines
= i_body[].
CALL FUNCTION 'CONVERT_ITF_TO_ASCII'
EXPORTING
language = sy-langu
tabletype = 'ASC'
IMPORTING
c_datatab = lt_datatab
TABLES
itf_lines = i_body[].
CLEAR i_body[].
LOOP AT lt_datatab INTO lw_data.
w_body_msg-line = lw_data.
APPEND w_body_msg TO i_body_msg.
CLEAR w_body_msg.
ENDLOOP.
"Write Packing List (Body)
DESCRIBE TABLE i_body_msg LINES g_tab_lines.
w_packing_list-head_start = 1.
w_packing_list-head_num
= 0.
w_packing_list-body_start = 1.
w_packing_list-body_num
= g_tab_lines.
w_packing_list-doc_type
= 'HTM'.
APPEND w_packing_list TO i_packing_list.
CLEAR w_packing_list.
"Fill the document data and get size of attachment
READ TABLE i_body_msg INTO w_body_msg INDEX g_tab_lines.
w_document_data-doc_size = ( g_tab_lines - 1 ) * 255 + strlen( w_body_msg ).
"Receivers List.
w_receivers-rec_type
= 'U'.
"Internet address
w_receivers-receiver
= p_mail.
w_receivers-com_type
= 'INT'.
w_receivers-notif_del = 'X'.
w_receivers-notif_ndel = 'X'.
APPEND w_receivers TO i_receivers .

CLEAR:w_receivers.
"Function module to send mail to Recipients
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data
= w_document_data
put_in_outbox
= 'X'
commit_work
= 'X'
IMPORTING
sent_to_all
= g_sent_to_all
TABLES
packing_list
= i_packing_list
contents_txt
= i_body_msg
receivers
= i_receivers
EXCEPTIONS
too_many_receivers
= 1
document_not_sent
= 2
document_type_not_exist
= 3
operation_no_authorization = 4
parameter_error
= 5
x_error
= 6
enqueue_error
= 7
OTHERS
= 8.
IF sy-subrc = 0 .
MESSAGE i303(me) WITH 'Mail has been Successfully Sent.'.
ELSE.
WAIT UP TO 2 SECONDS.
SUBMIT rsconn01 WITH mode = 'INT' WITH output = 'X' AND RETURN.
ENDIF.

Mail in HTML format.

You might also like