By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

Java mail

karthickkuchanur
100+
P: 156
Hi,

I am trying to send mail using java api.But i am getting exception.Please find the below code.
Expand|Select|Wrap|Line Numbers
  1. package com;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.Session;
  8. import javax.mail.Transport;
  9. import javax.mail.internet.AddressException;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12. import javax.mail.internet.MimeMessage.RecipientType;
  13.  
  14. public class SendMail
  15. {
  16.   private String from;
  17.   private String to;
  18.   private String subject;
  19.   private String text;
  20.   public SendMail(String from, String to, String subject, String text)
  21.   {
  22.     this.from = from;
  23.     this.to = to;
  24.     this.subject = subject;
  25.     this.text = text;
  26.   }
  27.   public void send()
  28.   {
  29.     Properties props = new Properties();
  30.     props.put("mail.smtp.host", "smtp.gmail.com");
  31.     props.put("mail.smtp.port", "9000");
  32.     Session mailSession = Session.getDefaultInstance(props);
  33.     Message simpleMessage = new MimeMessage(mailSession);
  34.     InternetAddress fromAddress = null;
  35.     InternetAddress toAddress = null;
  36.     try
  37.     {
  38.       fromAddress = new InternetAddress(from);
  39.       toAddress = new InternetAddress(to);
  40.     }
  41.     catch (AddressException e)
  42.     {
  43.       // TODO Auto-generated catch block
  44.       e.printStackTrace();
  45.     }
  46.     try
  47.     {
  48.       simpleMessage.setFrom(fromAddress);
  49.       simpleMessage.setRecipient(RecipientType.TO, toAddress);
  50.       simpleMessage.setSubject(subject);
  51.       simpleMessage.setText(text);
  52.       Transport.send(simpleMessage);
  53.       System.out.println("Hi");
  54.     }
  55.     catch (MessagingException e)
  56.     {
  57.       // TODO Auto-generated catch block
  58.       e.printStackTrace();
  59.     }
  60.   }
  61.   public static void main(String[] args)
  62.   {
  63.     SendMail mail = new SendMail("nk.karthick@gmail.com", "karthickrajkumar.n@cognizant.com",
  64.         "TEst", "fsdf");
  65.     mail.send();
  66.   }
  67. }
  68.  
Error :

javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 9000;
nested exception is:
java.net.ConnectException: Connection refused: connect
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.SendMail.send(SendMail.java:52)
at com.SendMail.main(SendMail.java:65)
Mar 19 '13 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
It says it can't connect to smtp.gmail.com, port: 9000; so confirm what the right port is and try it. Most documents say you should use 465 as the port.
Mar 19 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.