Send SMTP Mail using UTL_SMTP Package
Submitted by dancko on Sun, 2016-09-11 09:39
articles:
In this first my blog entry I wish to post the code for send an Email message from one sender to one receiver.
The code is the follow:
DECLARE
v_From VARCHAR2(80) := 'sender.mail.com';
v_Recipient VARCHAR2(80) := 'receiver.mail.com';
v_Subject VARCHAR2(80) := 'test subject';
v_Mail_Host VARCHAR2(30) := 'smtp.libero.it';
v_Mail_Conn utl_smtp.Connection;
crlf VARCHAR2(2) := chr(13)||chr(10);
BEGIN
v_Mail_Conn := utl_smtp.Open_Connection(v_Mail_Host, 25);
utl_smtp.Helo(v_Mail_Conn, v_Mail_Host);
utl_smtp.command(v_Mail_Conn,'AUTH LOGIN');
utl_smtp.command(v_Mail_Conn, utl_encode.text_encode('username','WE8ISO8859P1',UTL_ENCODE.BASE64));
utl_smtp.command(v_Mail_Conn, utl_encode.text_encode('password','WE8ISO8859P1',UTL_ENCODE.BASE64));
utl_smtp.Mail(v_Mail_Conn, v_From);
utl_smtp.Rcpt(v_Mail_Conn, v_Recipient);
utl_smtp.Data(v_Mail_Conn,
'Date: ' || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
'From: ' || v_From || crlf ||
'Subject: '|| v_Subject || crlf ||
'To: ' || v_Recipient || crlf ||
crlf ||
'some message text'|| crlf || -- Message body
'more message text'|| crlf
);
utl_smtp.Quit(v_mail_conn);
EXCEPTION
WHEN utl_smtp.Transient_Error OR utl_smtp.Permanent_Error then
raise_application_error(-20000, 'Unable to send mail', TRUE);
END;
Pay attention to 'utl_smtp.command' statements to authentication login to the server mail.
I hope this article will helpful for any new one.
Thanks.
»
- dancko's blog
- Log in to post comments
