Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaMail

 
Rashmee H S
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have an SMTP server running locally?
 
Praveen Sangolli
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to check for the proxy settings i guess..
 
K Kiran Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to ping the smtp server from the command promt(check whether you could connnect to it or not).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic