3

I have used the following Java code to send email.

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail
{
   public static void main(String [] args)
   {    

      String to = "abcd@gmail.com";
      String from = "web@gmail.com";
      String host = "localhost";
      Properties properties = System.getProperties();
      properties.setProperty("smtp.gmail.com", host);
      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();
      }
   }
}

When I run the file, I get the following errors:

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)

Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)

Would really appreciate if someone could help me on this.

How to solve the ConnectException?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Biswajit das
  • 51
  • 1
  • 2
  • 7
  • Can you try to telnet to your localhost on the configured SMTP Port (Default:25). `telnet localhost 25` and check? – Srinivas Dec 30 '12 at 09:01
  • Well, do you have an SMTP server running on your machine? – JB Nizet Dec 30 '12 at 09:01
  • I tried telnet localhost 25 bt it ws errored showing Connecting To localhost...Could not open connection to the host, on port 25: Connect failed I have enabled telnet client and telnet server feature in windows feature, I am using windows 7 – Biswajit das Dec 30 '12 at 09:23
  • There might be more info on debugging the telnet timeout here: http://stackoverflow.com/questions/5179807/could-not-connect-to-smtp-host-localhost-port-25-nested-exception-is-java-n – obesechicken13 Aug 15 '13 at 19:04

5 Answers5

6

I can see you are trying to use Gmail as your SMTP server. This line is incorrect:

properties.setProperty("smtp.gmail.com", host);

You are using the host name as the property name, which is incorrect. Because you do not set the mail.smtp.host property JavaMail attempts to connect to 'localhost'. Set these following properties instead:

properties.put("mail.smtp.starttls.enable", "true"); 
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.user", "username"); // User name
properties.put("mail.smtp.password", "password"); // password
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
Perception
  • 79,279
  • 19
  • 185
  • 195
Jayamohan
  • 12,734
  • 2
  • 27
  • 41
0

Change host name to : smtp.gmail.com

String host = "localhost";

Change this to:

String host = "smtp.gmail.com"
Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
0

You have to try something like this

String host = "smtp.gmail.com";
String from = "user name";
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", "asdfgh");
props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo mail
props.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));

InternetAddress[] to_address = new InternetAddress[to.length];
int i = 0;
// To get the array of addresses
while (to[i] != null) {
    to_address[i] = new InternetAddress(to[i]);
    i++;
}
System.out.println(Message.RecipientType.TO);
i = 0;
while (to_address[i] != null) {

    message.addRecipient(Message.RecipientType.TO, to_address[i]);
    i++;
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
// alternately, to send HTML mail:
// message.setContent("<p>Welcome to JavaMail</p>", "text/html");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.mail.yahoo.co.in", "user name", "asdfgh");
transport.sendMessage(message, message.getAllRecipients());
transport.close();

Rigth now you are trying to connect to localhost

Courtesy this answer

Community
  • 1
  • 1
Ankur
  • 12,676
  • 7
  • 37
  • 67
0

Try adding this to your Properties:

private static final String SMTP_HOST = "smtp.gmail.com"; //Use Gmail's SMTP Host.
private static final String SMTP_PORT = ""; //Use Gmail's Port Number for SMTP.
properties.setProperty("mail.smtp.host", SMTP_HOST);
properties.setProperty("mail.smtp.port", SMTP_PORT);
Srinivas
  • 1,780
  • 1
  • 14
  • 27
0

suppose my

email: liferayasif@gmail.com

paswd: password

public Session getSession() {
        
        Properties props = new Properties();
    
        props.put("mail.smtp.starttls.enable", "true"); 
        
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.user", "liferayasif"); // User name
        props.put("mail.smtp.password", "password"); // password
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        
        props.put("mail.session.mail.pop3.host", "pop.gmail.com");

        props.put("mail.session.mail.pop3.password", "password");

        props.put("mail.session.mail.pop3.port", "110");

        props.put(" mail.session.mail.pop3.user", "USER");

        props.put("mail.session.mail.imap.host", "imap.gmail.com");

        props.put("mail.session.mail.imap.port", "993");

        props.put("mail.session.mail.store.protocol", "imap");

        props.put("mail.session.mail.transport.protocol", "smtp");

        props.put("mail.session.mail.smtp.host", "smtp.gmail.com");

        props.put("mail.session.mail.smtp.password", "password");

        props.put("mail.session.mail.smtp.user", "liferayasif@gmail.com");

        props.put("mail.session.mail.smtp.port", "465");

        props.put("mail.session.mail.smtp.auth", "true");

        props.put("mail.session.mail.smtp.starttls.enable", "true");

        props.put("mail.session.mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
              
        Session session = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication("liferayasif", "password");
            }
         });
      
      return session;
    }
    
asifaftab87
  • 1,315
  • 24
  • 28