线程" main"中的例外情况java.lang.RuntimeException:javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,port:578;

时间:2015-02-07 05:07:18

标签: java email javamail

我是Java新手。我想构建一个应用程序,它将向我的客户发送电子邮件。我搜索了stackoverflow但没有得到任何理想的解决方案。在某个地方,我看到这可能是防火墙的问题,但没有得到修复它的解决方案。 我正在使用Ubuntu 14.10
//这是我的代码。

public class sendMail {

    public void send(String from, String to, String subject, String body) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "587");
        props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");

        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("xxxxxxx@gmail.com","xxxxx");
            }
        });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("xxxxxx@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("xxxx@domain.com"));
            message.setSubject("Testing Subject");
            message.setText("Hello this is not spam," +
                "\n\n This is a JavaMail test...!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }  
}  

我的错误是:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
    nested exception is:
       java.net.ConnectException: Connection timed out
       at dbdemo.sendMail.send(sendMail.java:54)
       at dbdemo.DBDemo.main(DBDemo.java:62)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
    nested exception is:
       java.net.ConnectException: Connection timed out
       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:313)
       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 dbdemo.sendMail.send(sendMail.java:49)
       ... 1 more
Caused by: java.net.ConnectException: Connection timed out
       at java.net.PlainSocketImpl.socketConnect(Native Method)
       at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
       at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
       at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
       at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
       at java.net.Socket.connect(Socket.java:579)
       at java.net.Socket.connect(Socket.java:528)
       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)
       ... 8 more
Java Result: 1

任何帮助将不胜感激。请帮我。

3 个答案:

答案 0 :(得分:2)

丢弃您从不了解如何使用JavaMail并使用code for connecting to Gmail in the JavaMail FAQ的人剪切和粘贴的代码。

如果它不适合您,请按照debugging tips in the JavaMail FAQ

如果您仍然无法弄清楚,并且无法在JavaMail FAQ中找到答案,请在此处发布JavaMail调试输出。

答案 1 :(得分:1)

这段代码对我有用,

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

public class Email {

private static String USER_NAME = "xxxx";  // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "xxxx"; // GMail password
private static String RECIPIENT = "xxx@gmail.com";

public static void main(String[] args) {
    String from = USER_NAME;
    String pass = PASSWORD;
    String[] to = { RECIPIENT }; // list of recipient email addresses
    String subject = "Java send mail example";
    String body = "Welcome to JavaMail!";

    sendFromGMail(from, pass, to, subject, body);
}

private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
    Properties props = System.getProperties();
  String host = "smtp.gmail.com";
   // String host="localhost";
  // props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("mail.smtp.starttls.enable", "true");
   // props.put("mail.smtp.host", host);
   props.put("mail.smtp.ssl.trust", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");//587
    props.put("mail.smtp.auth", "true");
   //System.out.println("success point 1");

    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    try {
         //System.out.println("success point 2");

        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for( int i = 0; i < to.length; i++ ) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for( int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }

         //System.out.println("success point 3");

        message.setSubject(subject);
        message.setText(body);
        // System.out.println("success point 4");

        Transport transport = session.getTransport("smtp");
        // System.out.println("success point 5");

        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        //System.out.println("success 6");
    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }
  }
}

我曾经使用过一些jar文件 活化,javamail的-1.4.4,javamail的-SMTP-1.4.2,POP3,邮件6.0.0来源

答案 2 :(得分:0)

连接超时通常来自

  • 在简单地吃数据包的方式上的某种防火墙 没有告诉发件人“无路由主持人”等事情。
  • 由于错误的网络配置或线路过载导致的数据包丢失
  • 请求重载服务器的次数太多

这是How do I turn off firewall on Ubuntu

如果不起作用,请尝试将props.put("mail.smtp.port", "587");更改为props.put("mail.smtp.port", "465");props.put("mail.smtp.port", "25");


一旦你连接了一些进一步的信息(不是超时相关):

请勿忘记仔细检查您的登录凭据。,因为如果提供错误,可能会导致Authentication Failed Exception