Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package neethu;

/**
 *
 * @author neethu
 */

//I'm trying to send an email using Java:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;


public class MailCheck {
   public static void main(String [] args) {
      String to = "neethu.jayachandran1@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "neethujayan48@gmail.com";

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);

      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

      try{
 
         MimeMessage message = new MimeMessage(session);
         message.setFrom(new InternetAddress(from));
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));
         message.setSubject("This is the Subject Line!");

         
         message.setText("This is actual message");
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
         System.out.println("errorr");
      }
   }

}


when i excecute this code i got an error of ::
init:
deps-jar:
compile-single:
run-single:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
        java.net.ConnectException: Connection refused: connect
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
        at javax.mail.Service.connect(Service.java:275)
        at javax.mail.Service.connect(Service.java:156)
        at javax.mail.Service.connect(Service.java:105)
        at javax.mail.Transport.send0(Transport.java:168)
        at javax.mail.Transport.send(Transport.java:98)
        at neethu.MailCheck.main(MailCheck.java:59)
Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
BUILD SUCCESSFUL (total time: 6 seconds)




Now I got the error ....

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at neethu.MailCheck.main(MailCheck.java:59)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
errorr
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 7 more


Thank you for your help
please help me to solve this...
Posted
Updated 14-May-13 5:54am
v3
Comments
Richard MacCutchan 24-Apr-13 15:51pm    
Connection refused
The SMTP host does not accept your connection request, probably because you user credentials are not valid.
Richard MacCutchan 14-May-13 13:30pm    
Look at the first error message: what is it telling you? As Bernhard already explained, in his answer below, you need an SMTP server running on localhost (i.e. your PC) listening on port 25. Do you have this set up?

1 solution

properties.setProperty("mail.smtp.host", host); with host = "localhost"; means that you try to contact an SMTP server on your local machine. Do you have an SMTP server running on your local computer? Likely not. And because of that, your computer refuses the connection.
Either set up an SMTP server, or configure your program to use an exisiting SMTP server.
 
Share this answer
 
Comments
Richard MacCutchan 25-Apr-13 7:05am    
I missed that. :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900