Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

unable to send mail using UTL_SMTP

521905Oct 14 2010 — edited Oct 14 2010
Hi,
I have been trying to send mail using the UTL_SMTP package. I don't get any errors or exceptions when running the program below but i don't get any mail in my mailbox. Additionally the mail server also seems to be responding in a I am using Oracle 10g database version 10.2.0.1.0. I have a Zimbra mail server on the network which i am sending to.

The program is as follows:
DECLARE
c UTL_SMTP.CONNECTION;
v_result UTL_SMTP.REPLY;

PROCEDURE send_header(name IN VARCHAR2, header IN VARCHAR2) AS
BEGIN
UTL_SMTP.WRITE_DATA(c, name || ': ' || header || UTL_TCP.CRLF);
END;

BEGIN
--c := UTL_SMTP.OPEN_CONNECTION('smtp-server.acme.com');
c := UTL_SMTP.OPEN_CONNECTION('mail.private.lan');

--UTL_SMTP.HELO(c, 'foo.com');
v_result:= UTL_SMTP.HELO(c, 'private.lan');
dbms_output.put_line('1 after helo, reply code= '||v_result.code||', reply_msg= '||v_result.text);
v_result:= utl_smtp.command( c, 'AUTH LOGIN');
dbms_output.put_line('1.1 after command AUTH LOGIN, reply code= '||v_result.code||', reply_msg= '||v_result.text);
v_result:= utl_smtp.command( c, utl_raw.cast_to_varchar2( utl_encode.base64_encode( utl_raw.cast_to_raw( 'parthiv.dixit' ))) );
dbms_output.put_line('1.2 after setting username, reply code= '||v_result.code||', reply_msg= '||v_result.text);
v_result:= utl_smtp.command( c, utl_raw.cast_to_varchar2( utl_encode.base64_encode( utl_raw.cast_to_raw( 'parthiv' ))) );
dbms_output.put_line('1.3 after setting password, reply code= '||v_result.code||', reply_msg= '||v_result.text);
v_result:=UTL_SMTP.MAIL(c, 'sender@foo.com');
dbms_output.put_line('2 after mail, reply code= '||v_result.code||', reply_msg= '||v_result.text);
v_result:=UTL_SMTP.RCPT(c, 'parthiv.dixit@dcdmc.co.bw');
dbms_output.put_line('3 after RCPT, reply code= '||v_result.code||', reply_msg= '||v_result.text);
v_result:=UTL_SMTP.OPEN_DATA(c);
dbms_output.put_line('4 after open_data, reply code= '||v_result.code||', reply_msg= '||v_result.text);
send_header('From', '"Sender" <sender@foo.com>');
send_header('To', '"Recipient" <parthiv.dixit@dcdmc.co.bw>');
send_header('Subject', 'Hello');
UTL_SMTP.WRITE_DATA(c, UTL_TCP.CRLF || 'Hello, world!');
dbms_output.put_line('5 after write_data');
v_result:= UTL_SMTP.CLOSE_DATA(c);
dbms_output.put_line('6 after close_data, reply code= '||v_result.code||', reply_msg= '||v_result.text);
v_result:= UTL_SMTP.QUIT(c);
dbms_output.put_line('7 after quit, reply code= '||v_result.code||', reply_msg= '||v_result.text);
EXCEPTION
WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
BEGIN
dbms_output.put_line('8 error 1= '||sqlerrm);
UTL_SMTP.QUIT(c);
EXCEPTION
WHEN UTL_SMTP.TRANSIENT_ERROR OR UTL_SMTP.PERMANENT_ERROR THEN
dbms_output.put_line('9 error 2= '||sqlerrm);
NULL; When the SMTP server is down or unavailable, we don't have
-- a connection to the server. The QUIT call will raise an
-- exception that we can ignore.
END;
raise_application_error(-20000,
'Failed to send mail due to the following error: ' || sqlerrm);
END;

The output from the program above is as follows:
1 after helo, reply code= 250, reply_msg= mail.dcdmc.co.bw
1.1 after command AUTH LOGIN, reply code= 334, reply_msg= VXNlcm5hbWU6
1.2 after setting username, reply code= 334, reply_msg= UGFzc3dvcmQ6
1.3 after setting password, reply code= 235, reply_msg= 2.7.0 Authentication successful
2 after mail, reply code= 250, reply_msg= 2.1.0 Ok
3 after RCPT, reply code= 250, reply_msg= 2.1.5 Ok
4 after open_data, reply code= 354, reply_msg= Please start mail input.
5 after write_data
6 after close_data, reply code= 250, reply_msg= Mail queued for delivery.
7 after quit, reply code= 221, reply_msg= Closing connection. Good bye.

However if i use the following sample Java program to send mail, I am able to receive mail in my mail box without problems:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class MailSend{
public MailSend(){
try{
//String smtphost ="smtpapacgc.oracle.com";
String smtphost ="10.113.200.4";
//String smtphost ="10.150.0.1";
//String from = "parthiv.dixit@dcdmc.co.bw";
String from = "codiwe_support@roads";
String to = "parthiv.dixit@dcdmc.co.bw";
//String to = "mmbaiwa@gov.bw";
//String to = "codiwe_support@roads";
// start a session
Properties prop = System.getProperties();
prop.put("mail.smtp.host",smtphost);
Session session1 = Session.getInstance(prop,null);
// construct a message
MimeMessage msg = new MimeMessage(session1);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
msg.setSubject("Test Example 2");
msg.setText("from Java mail program..2");
// connect to the transport
Transport trans = session1.getTransport("smtp");
System.out.println("before connect");
trans.connect(smtphost,"parthiv.dixit","parthiv");
System.out.println("after connect");
// send the message
trans.sendMessage(msg,msg.getAllRecipients());
//smtphost
trans.close();
}catch(Exception e){
e.printStackTrace();
}
}// Constructor

public static void main(String[] s){
MailSend ms = new MailSend();
}
}


Therefore i am confused as to the source of the problem. Is it on the database or the mail server? If it is the mail server, how come the java program is working and the UTL_SMTP is not working?

Can anyone please help me figure out where the problem could be?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 11 2010
Added on Oct 14 2010
2 comments
851 views