File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JavaMail" Watch "JavaMail" New topic
Author

JavaMail

Rashmee H S
Greenhorn

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

Properties props=new Properties();
props.put("smtp.google.host","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));
message.setRecipients
(Message.RecipientType.TO,InternetAddress.parse(s2,false));
message.setSubject(s3);
message.setText(s4);
HttpClient hc=new HttpClient();
Transport.send(message);
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:
java.net.ConnectException: Connection refused: connect

whtas the problem .....
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Do you have an SMTP server running locally?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Praveen Sangolli
Greenhorn

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;
try
{
Properties props = new Properties();
props.put("mail.smtp.host", "10.100.10.10"); //better keep the IP address here instead of localhost.
props.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(props,null);
session.setDebug(debug);

Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress("fromaddress@x.com","somename");
msg.setFrom(addressFrom);
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("toaddress@z.com"));
msg.setSubject("Hi");
msg.setText("The text........\n");

Transport.send(msg);
}
catch (Exception e)
{
e.printStackTrace();
}
}

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

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: http://aspose.com/file-tools
 
subject: JavaMail