10

Here is my code

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


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

      // Recipient's email ID needs to be mentioned.
      String to = "abc82@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "xyz@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{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("Thanks for registering on our website!");

         // Now set the actual message
         message.setText("Welcome To Job Portal !!!!  Again Thanks ");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }

    }
}

And I am getting this error everytime

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:1706)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
    at javax.mail.Service.connect(Service.java:291)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at MailSendClass.main(MailSendClass.java:58)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
    ... 7 more
BUILD SUCCESSFUL (total time: 3 seconds)

I am not getting the error why this is happening. Please help me in fixing this error.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Ketan G
  • 507
  • 1
  • 5
  • 21

5 Answers5

4

Error is self explainatory: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

You have no SMTP server on localhost, but you configure it there :

  // Assuming you are sending email from localhost
  String host = "localhost";
  ...
  // Setup mail server
  properties.setProperty("mail.smtp.host", host);

So you must:

  • either configure a local SMTP server as a relay on your local system (Postfix or sendmail are two well knows servers)
  • of configure a dummy server that simply traces the mail request but does not even try to deliver mail (Python is known to have such dummy servers out of the box)
  • or configure your application with a server that you are allowed to use - contact your system admin in a corporate environment, or your ISP in an individual one. Anyway, you will need that even to configure a true relay.
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • I am using GlassFish server in my application. – Ketan G Oct 01 '15 at 09:46
  • @KetanGupta: AFAIK GlassFish is **not** a SMTP server... Just try `telnet localhost 25` at command line, if you get an error, no SMTP server is running locally. – Serge Ballesta Oct 01 '15 at 10:12
  • i am not getting you completely .Please give me complete answer because i don't know about these things . – Ketan G Oct 01 '15 at 10:33
  • 1
    @KetanGupta: Apache is a HTTP server, it knows how to deal with HTTP or HTTPS requests and send back resources or relay request to another HTTP[S] server. Tomcat or GlassFish are HTTP servlet containers, they know how to deal with HTTP requests and pass them to servlets and filters. Postfix or sendmail know how to process SMTP requests (default port 25). It is a different protocol and is processed by different server applications. Do you know how to send mail from the development machine, and how the local mailer is configured? – Serge Ballesta Oct 01 '15 at 11:56
3

You should use the free Google SMTP server as a test.

mail.host=smtp.gmail.com
mail.username=//your gmail
mail.password=//your password
mail.defaultEncoding=UTF-8
mail.smtp.auth=true
mail.smtp.starttls.required=true
mail.smtp.starttls.enable=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
mail.smtp.port=465
mail.smtp.socketFactory.port=465

Next, login with your gmail , and turn on less secure apps.

bryan kennedy
  • 6,969
  • 5
  • 43
  • 64
QHuy
  • 51
  • 5
  • 1
    First, don't set those socketFactory properties, [they're not needed](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes). Second, there is no mail.password property, nor a mail.defaultEncoding property. Read the [JavaMail FAQ](http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail) for the right way to use Gmail. – Bill Shannon Oct 01 '15 at 21:19
3

Here is the working solution bro. it's guranteed

1) First of all open your gmail account from which you wanted to send mail, like in you case ""xyz@gmail.com"

2) open this link below https://support.google.com/accounts/answer/6010255?hl=en

3) click on "Go to the "Less secure apps" section in My Account." option

4) Then turn on it

5) that's it (:

Zulfiqar Ali
  • 121
  • 6
1

You should look at this two lines:

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
Caused by: java.net.ConnectException: Connection refused: connect

The error is: "There is nothing listening on localhost at port 25".

You are trying to use localhost:25 as mail server, but there is no server there.

emiliollbb
  • 95
  • 3
-1

Just use this provided solution : javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25

and make sure to turn on less secure app access on your google account.