Click to See Complete Forum and Search --> : sending email in java


sunny51
January 28th, 2005, 06:03 AM
am trying to send a mail from a Servlet( tomcat ) and
getting the following exception:

Could not connect to SMTP host: localhost, port 25
The code for a function is :-

public void sendMessage(String hostname,String to, String from,String subject, String message ) throws MessagingException
{
boolean debug = true;

//Set the host smtp address
Properties props = System.getProperties();
props.put("mail.smtp.host", hostname);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
System.getProperties().list(System.out);

// create a message
Message msg = new MimeMessage(session);

// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress addressTo = new InternetAddress(to);
msg.setRecipient(Message.RecipientType.TO, addressTo);

msg.setRecipient(Message.RecipientType.TO, addressTo);

// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}

Pleas help me, have spent 2 days trying to solve the problem but without any luck.

Thanks

Davey
January 28th, 2005, 07:32 AM
Do you have a SMTP server running on localhost?

If you have, is there a firewall that is stopping you access to it? Can you telnet onto the smtp server using something like:

telnet localhost 25

sunny51
January 28th, 2005, 10:01 PM
"telnet localhost 25" gives me following error message :-
Connecting To localhost...Could not open connection to the host, on port 25: Connect failed
How can i check the firewall setting.I have dlink router.

Thanks

Davey
January 29th, 2005, 05:47 PM
That either means you don't have a SMTP server running or you can't get access into it. Do you have a SMTP server running on your local machine?

If you're running Windows XP, check that the included firewall is letting traffic through to port 25.

Sorry, I don't know about DLINK routers.

sunny51
January 30th, 2005, 04:13 PM
Thanks Davey,

I checked the firewall and the program is working now but it does not sends the mail. It comes back to me with the message " Relay access denied". Could you please tell me what i am missing now.

Thanks .

sunny51
January 31st, 2005, 11:23 PM
Hi Davey,

My program is working now. The problem was I was writing the name of mail server instead of localhost in my program.

thanks for your help.