3

My code is :

// File Name SendEmail.java

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

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

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

      // Sender's email ID needs to be mentioned
      String from = "fromEmail@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("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

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

My program was working correctly last night; I could send email from any address to any other, but now this error is occuring:

javax.mail.SendFailedException: Sending failed;
  nested exception is:
    class javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused
    at javax.mail.Transport.send0(Transport.java:218)
    at javax.mail.Transport.send(Transport.java:80)
    at SendEmail.main(SendEmail.java:49)

Line 49 is:

Transport.send(message);

Can anyone help me fix this error?

My operating system is : Linux,Fedora 16 -kernel: 3.3.7

Tharwen
  • 3,057
  • 2
  • 24
  • 36
M0εiπ
  • 263
  • 3
  • 10

3 Answers3

5

Could not connect to SMTP host: localhost, port: 25;

SMTP must be not running on your system or disabled for you as user. Check with your system administrator and get it enabled for you.

To check if SMTP is working on your Linux system, try the following commands:

  1. To simply verify Sendmail is running, try: netstat -an | grep 25
  2. Use telnet to connect to port 25 of smtp server: telnet <yourhost-or-ipnumber> 25
    a. Connecting To localhost...Could not open connection to the host, on port 25: Connect failed.
    b. if you or your ip is blocked, you would see error message something like this:
    220-localhost ESMTP Exim 4.63 #1 Fri, 01 Jun 2012 19:35:30 +0530
    220-We do not authorize the use of this system to transport unsolicited, and/or bulk e-mail.
  3. echo -e "quit" | nc localhost 25
    localhost.localdomain [127.0.0.1] 25 (?) : Connection refused
  4. mail at shell prompt.
  5. and, may be more...

You should check that sendmail daemon is started and is available always.

And if you have access to any other SMTP servers, try to send mail using their SMTP host name, to check if your code snippet is working.
Example : String host = "smtp.gmail.com";

Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
  • I log in as root, and I've not changed anything related to this – M0εiπ Jun 01 '12 at 15:38
  • 1
    @MoeinFatehi From your shell prompt try use `mail` to send and see if you could send. Alternatively check system services to know if SMTP is running. – Ravinder Reddy Jun 01 '12 at 15:41
  • 1
    Or, from the shell, type `telnet localhost 25`. This will open a telnet connection to your locally running SMTP server. If you get an error about the socket not being open, then it means your SMTP server is not running. – Michael Jun 01 '12 at 15:46
  • after trying: telnet localhost 25: telnet: connect to address ::1: Connection refused Trying 127.0.0.1... telnet: connect to address 127.0.0.1: Connection refused .........now, what should I do? – M0εiπ Jun 01 '12 at 15:48
  • Maybe try running `sudo service smtp start`? I'm not sure. – Michael Jun 01 '12 at 15:54
  • 1
    @MoeinFatehi Alternatively, you need to get `sendmail` daemon started on your system. I suggest you contact your system administrator to enable it permanently. – Ravinder Reddy Jun 01 '12 at 15:55
  • thank you @Ravinder... U helped a lot, I'll enable it permanently myself :) – M0εiπ Jun 01 '12 at 16:00
0

it was because my SendMail Service Stopped working.

to enable it : try this command in shell(as root).

service sendmail start
M0εiπ
  • 263
  • 3
  • 10
0

U on right way..just change Property setting and Session sentence .my java mail work Fine...

final String username = "abc@gmail.com";
            final String password = "****";

            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587");




            Session session = Session.getInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });