Java mail

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthickkuchanur
    New Member
    • Dec 2007
    • 156

    Java mail

    Hi,

    I am trying to send mail using java api.But i am getting exception.Pleas e find the below code.
    Code:
    package com;
    
    import java.util.Properties;
    
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMessage.RecipientType;
    
    public class SendMail
    {
      private String from;
      private String to;
      private String subject;
      private String text;
      public SendMail(String from, String to, String subject, String text)
      {
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.text = text;
      }
      public void send()
      {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "9000");
        Session mailSession = Session.getDefaultInstance(props);
        Message simpleMessage = new MimeMessage(mailSession);
        InternetAddress fromAddress = null;
        InternetAddress toAddress = null;
        try
        {
          fromAddress = new InternetAddress(from);
          toAddress = new InternetAddress(to);
        }
        catch (AddressException e)
        {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        try
        {
          simpleMessage.setFrom(fromAddress);
          simpleMessage.setRecipient(RecipientType.TO, toAddress);
          simpleMessage.setSubject(subject);
          simpleMessage.setText(text);
          Transport.send(simpleMessage);
          System.out.println("Hi");
        }
        catch (MessagingException e)
        {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      public static void main(String[] args)
      {
        SendMail mail = new SendMail("nk.karthick@gmail.com", "karthickrajkumar.n@cognizant.com",
            "TEst", "fsdf");
        mail.send();
      }
    }
    Error :

    javax.mail.Send FailedException : Sending failed;
    nested exception is:
    class javax.mail.Mess agingException: Could not connect to SMTP host: smtp.gmail.com, port: 9000;
    nested exception is:
    java.net.Connec tException: Connection refused: connect
    at javax.mail.Tran sport.send0(Tra nsport.java:218 )
    at javax.mail.Tran sport.send(Tran sport.java:80)
    at com.SendMail.se nd(SendMail.jav a:52)
    at com.SendMail.ma in(SendMail.jav a:65)
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    Working...