Error in Sending mail by Java

2019-07-07 04:02发布

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

3条回答
倾城 Initia
2楼-- · 2019-07-07 04:40

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);
                }
            });
查看更多
不美不萌又怎样
3楼-- · 2019-07-07 04:44

it was because my SendMail Service Stopped working.

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

service sendmail start
查看更多
别忘想泡老子
4楼-- · 2019-07-07 04:58

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";

查看更多
登录 后发表回答