472,333 Members | 1,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,333 developers and data experts.

Sending mail using UTL_SMTP.

debasisdas
8,127 Expert 4TB
The follwing sample code uses predefined package UTL_SMTP to send mail.

This package was first available in the version 8.1.7 .

Expand|Select|Wrap|Line Numbers
  1. create or replace procedure sendmail(sender varchar2,recipient varchar2,subject varchar2, text 
  2.  
  3. varchar2)
  4. IS
  5. mailhost    VARCHAR2(64) := '192.168.1.32';
  6. --The name of the SMTP server host
  7. port constant number(2):=25;
  8. --The port number on which SMTP server is listening (usually 25).
  9. timeout number :=180;
  10. --The time in seconds that the UTL_SMTP package waits before giving up in a read or write 
  11.  
  12. operation in this connection. 
  13. --In read operations, this package gives up if no data is available for reading immediately. 
  14. --In write operations, this package gives up if the output buffer is full and no data is to be 
  15.  
  16. sent into the network without being blocked. 
  17. --Zero (0) indicates not to wait at all. 
  18. --NULL indicates to wait forever.
  19. mail_conn  utl_smtp.connection;
  20. BEGIN
  21. --dbms_output.put_line(UTL_SMTP.VRFY (mail_conn,recipient));
  22. mail_conn := utl_smtp.open_connection(mailhost, port,timeout);
  23. --Helo performs initial handshaking with SMTP server after connecting
  24. utl_smtp.helo(mail_conn, mailhost);
  25. --Mail Initiates a mail transaction with the server
  26. utl_smtp.mail(mail_conn, sender);
  27. --Specifies the recipient of an e-mail message
  28. utl_smtp.rcpt(mail_conn, recipient);
  29. -- open_data(), write_data(), and close_data() into a single call to data().
  30. --Sends the DATA command
  31. utl_smtp.open_data(mail_conn);
  32. utl_smtp.write_data(mail_conn,'From'||':'|| Sender || UTL_TCP.CRLF);
  33. utl_smtp.write_data(mail_conn,'To'||':'|| recipient || UTL_TCP.CRLF);
  34. utl_smtp.write_data(mail_conn,'Subject' ||':'|| subject || UTL_TCP.CRLF);
  35. --Writes a portion of the e-mail message
  36. utl_smtp.write_data(mail_conn, text);
  37. --Closes the data session
  38. utl_smtp.close_data(mail_conn);
  39. utl_smtp.quit(mail_conn);
  40. --dbms_output.put_line('Your message has been sent...!');
  41. EXCEPTION
  42. WHEN UTL_SMTP.PERMANENT_ERROR THEN
  43.   BEGIN
  44.     utl_smtp.quit(mail_conn);
  45.   END;
  46. RAISE_APPLICATION_ERROR(-20101,'This id has Permanent Error');
  47. WHEN UTL_SMTP.TRANSIENT_ERROR THEN
  48.   BEGIN
  49.     utl_smtp.quit(mail_conn);
  50.   END;
  51. RAISE_APPLICATION_ERROR(-20102,'SMTP transient error:');
  52. WHEN UTL_SMTP.INVALID_OPERATION THEN
  53.   BEGIN
  54.     utl_smtp.quit(mail_conn);
  55.   END;
  56. RAISE_APPLICATION_ERROR(-20103,'Invalid Operation in Mail using UTL_SMTP.');
  57. WHEN OTHERS THEN
  58. RAISE_APPLICATION_ERROR(-20104,'Some other Error ...!');
  59. end;
  60. /
  61.  
  62.  
To execute the above procedure try the following code.

Expand|Select|Wrap|Line Numbers
  1. exec sendmail('sender@sender.com','recipient@recipient.com','Hi','Test Mail');
  2.  
Feb 15 '08 #1
0 16114

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: savvy | last post by:
i'm trying to compile a simple console application for sending a mail, my main idea is to schedule it to a particular time for sending mails using...
2
by: Mr. x | last post by:
Hello, I am sending emails with Hebrew contents. When receiving emails - I cannot see the Hebrew characters (it is not outlook express...
9
by: B-Dog | last post by:
I've built a small app that sends mail through our ISP's SMTP server but when I try to send through my local exchange server I get CDO error. Does...
3
by: Ant | last post by:
Hi, I'm using the MailMessage & smtpMail classes in System.Web.Mail to send mail, however it's not sending any emails. I'm using it on a Windows...
3
by: HoustonComputerGuy | last post by:
I am working on getting my web applications moved to .Net 2.0 and am having some problems with System.Net.Mail. I get the following error when...
6
by: Anuradha | last post by:
Dear All How can i send mails using vb.net Thanx all
1
by: Eric Sheu | last post by:
Greetings, I have been searching the web like mad for a solution to my SMTP problem. I am using Windows Server 2003 and ASP.NET 2.0 w/ C# to send...
3
by: mfleet1973 | last post by:
Hello Again. I have a program that sends e-mails as follows: Try Dim mail As New MailMessage mail.To = "me@comp.com" mail.From =...
8
by: Robert Dufour | last post by:
Dim message As New MailMessage("mymail@mydomain.com", "mymail@mydomain.com", "Test", "Test") Dim emailClient As New SmtpClient("localhost") ...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.