Skip to content
  • About Me: Shailender Thallam
  • Contact
  • Oracle Apps Interview Questions
    • Ask a Question
  • About Me: Shailender Thallam
  • Contact
  • Oracle Apps Interview Questions
    • Ask a Question

UTL_SMTP API to send HTML Emails from PL/SQL

  • by Shailender Thallam
  • January 19, 2013January 11, 2015
  • 4 Comments

Oracle has provided an API UTL_SMTP which can be used to send email from PL/SQL. UTL_SMTP uses SMTP (Simple Mail Transfer Protocol) sever to send the emails. To know more about UTL_SMTP API read oracle documentation here.

Below is a sample code snippet to send email using UTL_SMTP API.

DECLARE
--
v_from VARCHAR2(80)  := 'noreply@OracleAppsDNA.com';
v_recipient VARCHAR2(180) := 'shailender.thallam@xyz.com';
v_recipient2 VARCHAR2(180) := 'shailender@gmail.com';
v_subject VARCHAR2(180) := 'Test Email using UTL_SMTP';
v_mail_host VARCHAR2(80):= 'localhost'; --SMTP host name
v_mail_conn utl_smtp.connection;
crlf VARCHAR2(2) := chr(13)||chr(10); --can be replaced by utl_tcp.crlf
v_body VARCHAR2(10000);
v_msg varchar2(1222) := '<html><head><meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
</head><body>
Test Message from <a href="http://www.oracleappsdna.com" target="_blank">www.OracleAppsDNA.com</a>
This Test Email is sent using <span style="color: #0000ff;">UTL_SMTP API</span>
</body></html>';
--
BEGIN
--
v_mail_conn := utl_smtp.open_connection(v_mail_host, 25); --25 is SMTP Port Number
utl_smtp.helo(v_mail_conn, v_mail_host);
utl_smtp.mail(v_mail_conn, v_from);
utl_smtp.rcpt(v_mail_conn, v_recipient);
utl_smtp.rcpt(v_mail_conn, v_recipient2);
--
v_body :=  'Date: ' || TO_CHAR(sysdate, 'dd Mon yy hh24:mi:ss') || crlf ||
'From: ' || v_From || crlf ||
'Subject: '||v_Subject || crlf ||
'To: ' || v_Recipient || crlf ||
'CC: ' || v_Recipient2 || crlf ||v_msg|| '';
--
utl_smtp.DATA (v_mail_conn, 'MIME-Version: 1.0' || crlf || 'Content-type: text/html' || crlf || v_body );
utl_smtp.quit(v_mail_conn);
--
EXCEPTION WHEN OTHERS
THEN
--
dbms_output.put_line(SQLCODE ||' - '||SQLERRM);
--
END;

DECLARE -- v_from VARCHAR2(80) := 'noreply@OracleAppsDNA.com'; v_recipient VARCHAR2(180) := 'shailender.thallam@xyz.com'; v_recipient2 VARCHAR2(180) := 'shailender@gmail.com'; v_subject VARCHAR2(180) := 'Test Email using UTL_SMTP'; v_mail_host VARCHAR2(80):= 'localhost'; --SMTP host name v_mail_conn utl_smtp.connection; crlf VARCHAR2(2) := chr(13)||chr(10); --can be replaced by utl_tcp.crlf v_body VARCHAR2(10000); v_msg varchar2(1222) := '<html><head><meta http-equiv="Content-Language" content="en-us" /> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" /> </head><body> Test Message from <a href="http://www.oracleappsdna.com" target="_blank">www.OracleAppsDNA.com</a> This Test Email is sent using <span style="color: #0000ff;">UTL_SMTP API</span> </body></html>'; -- BEGIN -- v_mail_conn := utl_smtp.open_connection(v_mail_host, 25); --25 is SMTP Port Number utl_smtp.helo(v_mail_conn, v_mail_host); utl_smtp.mail(v_mail_conn, v_from); utl_smtp.rcpt(v_mail_conn, v_recipient); utl_smtp.rcpt(v_mail_conn, v_recipient2); -- v_body := 'Date: ' || TO_CHAR(sysdate, 'dd Mon yy hh24:mi:ss') || crlf || 'From: ' || v_From || crlf || 'Subject: '||v_Subject || crlf || 'To: ' || v_Recipient || crlf || 'CC: ' || v_Recipient2 || crlf ||v_msg|| ''; -- utl_smtp.DATA (v_mail_conn, 'MIME-Version: 1.0' || crlf || 'Content-type: text/html' || crlf || v_body ); utl_smtp.quit(v_mail_conn); -- EXCEPTION WHEN OTHERS THEN -- dbms_output.put_line(SQLCODE ||' - '||SQLERRM); -- END;

Now lets get into some inner details of the above code snippet.

SMTP Host Name

v_mail_host VARCHAR2(80):= 'localhost'; --SMTP host name

v_mail_host VARCHAR2(80):= 'localhost'; --SMTP host name

SMTP host name is the name of the email server which uses SMTP (Simple Mail Transfer Protocol) protocol to handle all outgoing e-mail messages. SMTP protocol uses port number 25 to handle out going emails, this is given as input to utl_smtp.open_connection as shown below.

v_mail_conn := utl_smtp.open_connection(v_mail_host, 25);

v_mail_conn := utl_smtp.open_connection(v_mail_host, 25);

How to find SMTP host name?
Execute the following query to findout the SMTP host name of your instance.

 SELECT fscpv.parameter_value,
  fscpt.display_name         ,
  fscpt.description
   FROM fnd_svc_comp_params_tl fscpt,
  fnd_svc_comp_param_vals fscpv
  WHERE 1              = 1
AND fscpt.display_name = 'Outbound Server Name'
AND fscpt.language     = 'US'
AND fscpt.source_lang  = 'US'
AND fscpt.parameter_id = fscpv.parameter_id;

SELECT fscpv.parameter_value, fscpt.display_name , fscpt.description FROM fnd_svc_comp_params_tl fscpt, fnd_svc_comp_param_vals fscpv WHERE 1 = 1 AND fscpt.display_name = 'Outbound Server Name' AND fscpt.language = 'US' AND fscpt.source_lang = 'US' AND fscpt.parameter_id = fscpv.parameter_id;

CRLF

crlf VARCHAR2(2) := chr(13)||chr(10); --can be replaced by utl_tcp.crlf

crlf VARCHAR2(2) := chr(13)||chr(10); --can be replaced by utl_tcp.crlf

chr(13) + chr(10) = new line character.

chr(13) = Carriage Return (moves cursor to leftmost side)
chr(10) = New Line (drops cursor down one line)

So the combination of chr(13) and chr(10) is to create a line break within a string.
In short its just like ‘dbms_output.put_line’.

SMTP Commands

utl_smtp.helo, utl_smtp.mail, utl_smtp.rcpt, etc. are the procedures which call SMTP commands, below are the list of standard SMTP commands and their description.

  • HELO – introduce yourself
  • EHLO – introduce yourself and request extended mode
  • MAIL – specify the sender
  • RCPT – specify the recipient
  • DATA – specify the body of the message (To, From and Subject should be the first three lines.)
  • RSET – reset
  • QUIT – quit the session
  • HELP – get help on commands
  • VRFY – verify an address
  • EXPN – expand an address
  • VERB – verbose

Note:
The above script is for sending HTML emails which is achieved by the following code

utl_smtp.DATA (v_mail_conn, 'MIME-Version: 1.0' || crlf || 'Content-type: text/html' || crlf || v_body );

utl_smtp.DATA (v_mail_conn, 'MIME-Version: 1.0' || crlf || 'Content-type: text/html' || crlf || v_body );

No related posts.

Tags:HTML-EMAILUTL_SMTP

Search on this Site

Popular Questions Asked

  • What is the difference between XML publisher and BI Publisher? asked by Questions Master
  • What is the difference between CASE and DECODE? asked by Questions Master
  • What are the different types of Sales Orders in Order Management? asked by Questions Master
  • Can you have multiple layout templates for a singe data template? asked by Questions Master
  • How to display the names of the employees who earn highest salary in their respective departments? asked by Questions Master

Categories

Recent Posts

  • Oracle ERP Cloud Instance Clone/Refresh Guidelines
  • ESS Request History with Time took to Run
  • Payload to Update AR Invoice DFF in Oracle ERP Cloud
  • Emails generated by Oracle ERP are delivered to customer/Supplier with from email address as “no-reply@oracle.com” instead of no-reply@client-DomainName.com
  • Oracle ERP Cloud SQL Query to find Bank Accounts Associated with a Supplier
OracleAppsDNA.com

OracleAppsDNA.com 2025 | All Posts on this blog are based on the views of the Author. Reproducing of articles without permission is prohibited.