sendmailusingutl_smtp
TRANSCRIPT
-
7/31/2019 sendMailUsingUTL_SMTP
1/2
createorreplaceproceduresendmail(sendervarchar2,recipientvarchar2,subjectvarchar2,text2.3.varchar2)4.IS5.mailhostVARCHAR2(64):='192.168.1.32';6.--ThenameoftheSMTPserverhost7.portconstantnumber(2):=25;8.--TheportnumberonwhichSMTPserverislistening(usually25).9.timeoutnumber:=180;10.--ThetimeinsecondsthattheUTL_SMTPpackagewaitsbeforegivingupinareadorwrite11.12.operationinthisconnection.13.--Inreadoperations,thispackagegivesupifnodataisavailableforreadingimmediately.14.--Inwriteoperations,thispackagegivesupiftheoutputbufferisfullandnodataistobe15.16.sentintothenetworkwithoutbeingblocked.17.--Zero(0)indicatesnottowaitatall.18.--NULLindicatestowaitforever.19.mail_connutl_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.--HeloperformsinitialhandshakingwithSMTPserverafterconnecting24.utl_smtp.helo(mail_conn,mailhost);25.--MailInitiatesamailtransactionwiththeserver26.utl_smtp.mail(mail_conn,sender);27.--Specifiestherecipientofane-mailmessage28.utl_smtp.rcpt(mail_conn,recipient);29.--open_data(),write_data(),andclose_data()intoasinglecalltodata().30.--SendstheDATAcommand31.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.--Writesaportionofthee-mailmessage36.utl_smtp.write_data(mail_conn,text);37.--Closesthedatasession38.utl_smtp.close_data(mail_conn);39.utl_smtp.quit(mail_conn);40.--dbms_output.put_line('Yourmessagehasbeensent...!');41.EXCEPTION42.WHENUTL_SMTP.PERMANENT_ERRORTHEN43.BEGIN44.utl_smtp.quit(mail_conn);45.END;
46.RAISE_APPLICATION_ERROR(-20101,'ThisidhasPermanentError');47.WHENUTL_SMTP.TRANSIENT_ERRORTHEN48.BEGIN49.utl_smtp.quit(mail_conn);50.END;51.RAISE_APPLICATION_ERROR(-20102,'SMTPtransienterror:');52.WHENUTL_SMTP.INVALID_OPERATIONTHEN53.BEGIN54.utl_smtp.quit(mail_conn);55.END;
-
7/31/2019 sendMailUsingUTL_SMTP
2/2
56.RAISE_APPLICATION_ERROR(-20103,'InvalidOperationinMailusingUTL_SMTP.');57.WHENOTHERSTHEN58.RAISE_APPLICATION_ERROR(-20104,'SomeotherError...!');59.end;60./61.62.Toexecutetheaboveproceduretrythefollowingcode.execsendmail('[email protected]','[email protected]','Hi','TestMail');