File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other JSE/JEE APIs and the fly likes JavaMail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JavaMail" Watch "JavaMail" New topic


Rashmee H S

Joined: May 29, 2008
Posts: 6
Hi guys I have this code

Properties props=new Properties();
props.put("","localhost"); // 'localhost' for testing
Session session1 = Session.getDefaultInstance(props,null);
String s1 = request.getParameter("text1"); //sender (from)
String s2 = request.getParameter("text2");
String s3 = request.getParameter("text3");
String s4 = request.getParameter("area1");
Message message =new MimeMessage(session1);
message.setFrom(new InternetAddress(s1));
HttpClient hc=new HttpClient();
out.println("mail has been sent");

But its giving me error:
ERROR.....javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is: Connection refused: connect

whtas the problem .....
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Do you have an SMTP server running locally?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Praveen Sangolli

Joined: May 22, 2008
Posts: 28
You need to check for the proxy settings i guess..
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
HI Rashmee,

Earlier I was also working with similar API and below is my test code which was successful:

(1) Please modify this code like the host,port,from address, to address, etc and try out.....
(2) Also do check where the server is?(which Paul Sturrock is referring to)
(3)In my project, my mail admin used to enable my IP address in the Mail server, so that this application
runs fine from my system. If I was trying to run this application from other system it will not work.
This is just for security otherwise some may use it for wrong purpose by changing the from address. I guess something is to be done at your mail server side.

public static void main(String[] args)
boolean debug = false;
Properties props = new Properties();
props.put("", ""); //better keep the IP address here instead of localhost.
props.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(props,null);

Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress("","somename");
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(""));
msg.setText("The text........\n");

catch (Exception e)

[ June 06, 2008: Message edited by: K Kiran Kumar ]
AmitDBond Sethi

Joined: Jun 06, 2008
Posts: 11
Try to ping the smtp server from the command promt(check whether you could connnect to it or not).
I agree. Here's the link:
subject: JavaMail
It's not a secret anymore!