UTL_MAIL & UTL_SMTP
Install / Configure
sqlplus / as sysdba
@$ORACLE_HOME/rdbms/admin/utlmail.sql
@$ORACLE_HOME/rdbms/admin/prvtmail.plb
GRANT EXECUTE ON UTL_MAIL TO user;
ALTER SYSTEM SET smtp_out_server = 'mail.domain.com:25' SCOPE=BOTH;
SMTP_OUT_SERVER
SMTP_OUT_SERVER = host_name[ :port ] , [ host_name [ :port ] ]
SMTP_OUT_SERVER specifies the SMTP host and port to which UTL_MAIL delivers out-bound E-mail. Multiple servers may be specified, separated by commas.If the first server in the list is unavailable, then UTL_MAIL tries the second server, and so on.If SMTP_OUT_SERVER is not specified, then the SMTP server name defaults to the value of DB_DOMAIN, the port number defaults to 25, and the SMTP domain defaults to the suffix of DB_DOMAIN. (1)This parameter was introduced in release 10.1DB_DOMAIN
DB_DOMAIN = domain_name
Any legal string of name components, separated by periods and up to 128 characters long (including the periods). This value cannot be NULL (4) < NOT SURE THIS IS TRUEACLs
Use the following SQL to check that any less privileged users who may need to use UTL_MAIL/UTL_SMTP have the correct ACL to do so...
This example identifies which ACL, if any, allows myUser access to myMailserver.com (as identified by SMTP_OUT_SERVER)...
COLUMN host FORMAT a10
COLUMN acl FORMAT a40
SET LINESIZE 132
SELECT dna.host,
dna.lower_port,
dna.upper_port,
dna.acl,
DECODE(DBMS_NETWORK_ACL_ADMIN.CHECK_PRIVILEGE_ACLID(dna.aclid, 'MYUSER', 'connect'),
1, 'GRANTED',
0, 'DENIED',
null) privilege
FROM dba_network_acls dna
WHERE host IN (SELECT *
FROM TABLE(DBMS_NETWORK_ACL_UTILITY.DOMAINS('mymailserver.com')))
ORDER BY DBMS_NETWORK_ACL_UTILITY.DOMAIN_LEVEL(host) desc,
lower_port,
upper_port
/
If the user does not have connect access, it will be unable to send mail. Refer to the ACL documentation for instructions on how to create a new ACL if required, along with steps to take if you change the name of your mailserver.
Usage Examples
BEGIN
UTL_MAIL.SEND(sender => 'myuser@mydomain.com',
recipients => 'youruser@yourdomain.com',
cc => 'usera@domaina.com',
bcc => 'userb@domainb.com',
subject => 'UTL_MAIL Test',
message => 'Hello');
END;
/
Calling UTL_MAIL from a Trigger
CREATE OR REPLACE TRIGGER schemaname.triggername
before shutdown on database
begin
utl_mail.send(
sender => 'myuser@mydomain.com',
recipients => 'youruser@yourdomain.com',
subject => 'Testing utl_mail',
message => 'The receipt of this email means'||
' that shutting down the database'||
' works for UTL_MAIL '
);
end;
/
Troubleshooting
Calling UTL_SMTP directly
This can be useful for troubleshooting...
DECLARE
c utl_smtp.connection;
r utl_smtp.reply;
BEGIN
c := utl_smtp.open_connection('mail.domain.com', 25);
utl_smtp.helo(c,'mydomain.com');
END;
/
If the above works then try this...
DECLARE
c utl_smtp.connection;
r utl_smtp.reply;
BEGIN
c := utl_smtp.open_connection('mail.domain.com', 25);
utl_smtp.helo(c,'mydomain.com');
utl_smtp.mail(c,'myuser@mydomain.com');
END;
/
Troubleshooting outside the database instance
Can your SMTP server ping your mail server?
ping mail.domain.com
Can your SMTP server resolve the mail server name?
nslookup mail.domain.com
Does your host name have access to connect to the SMTP server on port 25?
telnet mail.domain.com 25
Trying... Connected to mail.domain.com. Escape character is '^]'. 220 mailhost.domain.com ESMTP Service (mailserver type/version) ready at datequit
221 mailhost.domain.com SMTP Service closing transmission channelConnection closed.Can you send mail via telnet?
telnet mail.domain.com 25
Trying... Connected to mail.domain.com. Escape character is '^]'. 220 mailhost.domain.com ESMTP Service (Lotus Domino Release 7.0.2) ready at Thu, 21 Jun 2007 03:46:39 -0500HELO mydomain
250 mailhost.domain.com Hello mydomain ([ipadress]), pleased to meet youMAIL FROM: myuser@mydomain.com
554 Mail from myuser@mydomain.com rejected for policy reasons.NOTE: this particular error could be due to a DNS mis-configurationBibliography & References
Refer to Metalink Note: 269375.1 for example code: https://metalink.oracle.com/metalink/plsql/f?p=130:14:11161156237372401642::::p14_database_id,p14_docid,p14_show_header,p14_show_help,p14_black_frame,p14_font:NOT,269375.1,1,1,1,helvetica
https://docs.oracle.com/database/121/ARPLS/u_smtp.htm
403215.1 - PLSQL Package UTL_MAIL Fails with Error ORA-29278 or ORA-29261 when SMTP_OUT_SERVER is Incorrect341235.1 - ORA-29278 When doing UTL_MAIL.SEND339445.1 - Causes for ORA-29261: Bad Argument using UTL_MAIL402678.1 - Receiving Ora-29279: Smtp Permanent Error 551 Using Utl_mail Ora-29279269375.1 - How to Use the UTL_MAIL Package
http://www.psoug.org/reference/utl_mail.html (UTL_MAIL) http://www.psoug.org/reference/utl_smtp.html (UTL_SMTP) http://www.psoug.org/reference/utl_tcp.html (UTL_TCP)
http://searchoracle.techtarget.com/tip/0,289483,sid41_gci1195514,00.html http://www.orafaq.com/forum/t/42619/0/ (UTL_MAIL) http://builder.com.com/5100-6388_14-6078020.html (UTL_MAIL)
http://www.netadmintools.com/art276.html (SMTP) http://www.activexperts.com/activemail/telnet/ (SMTP)
http://aplawrence.com/Blog/B961.html (DNS)
Using Notes/Domino SMTP with a DMZ, Part 1 http://www-128.ibm.com/developerworks/lotus/library/smtp-dmz1/
http://www-1.ibm.com/support/docview.wss?uid=swg21101300
http://chris-linfoot.net/d6plinks/CWLT-6XYKQQ