无法连接到SMTP主机:smtp.gmail.com,端口:587;嵌套的异常是:java.net.ConnectException:连接超时:connect [英] Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect

查看:303
本文介绍了无法连接到SMTP主机:smtp.gmail.com,端口:587;嵌套的异常是:java.net.ConnectException:连接超时:connect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是应用程序的代码。我一直试图使用Eclipse IDE来运行它。我还添加了所有必需的Java邮件jar文件,即
dsn.jar,imap.jar,mailapi.jar,pop3.jar,smtp.jar,mail.jar
但它给出以下错误无法连接到SMTP主机:smtp.gmail.com,端口:587

Here is the code of the application. I have been trying to run this using eclipse IDE. I also added all the required java mail jar files namely dsn.jar,imap.jar,mailapi.jar,pop3.jar,smtp.jar,mail.jar. But it gives the following error Could not connect to SMTP host: smtp.gmail.com, port: 587.

没有防火墙阻止访问,因为在ping smtp.gmail.com时收到了回复。
我什至尝试过这种方式:

There's no firewall blocking access because a reply is received on pinging smtp.gmail.com. I have even tried it this way :

  • First sign into the Gmail account in a browser on the device where you are setting up/using your client
  • Go here and enable access for "less secure" apps: https://www.google.com/settings/security/lesssecureapps
  • Then go here: https://accounts.google.com/b/0/DisplayUnlockCaptcha and click Continue.
  • Then straightaway go back to your client and try again.

javax.mail.MessagingException:无法连接到SMTP主机:
smtp.gmail.com,端口:587;
嵌套的异常是:
java.net.ConnectException:连接超时:在com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)上连接
在com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)在javax.mail.Service.connect(Service.java:317)
在javax.mail.Service.connect在
(Service.java:176)
在javax.mail.Service.connect(Service.java:125)
在javax.mail.Transport.send0(Transport.java:194)
在javax.mail.Transport.send(Transport.java:124)
在PlainTextEmailSender.sendPlainTextEmail(PlainTextEmailSender.java:50)
在PlainTextEmailSender.main(PlainTextEmailSender.java:73)
由:java.net.ConnectException:连接超时:java.net.DualStackPlainSocketImpl.connect0(本机方法)连接
java.net.DualStackPlainSocketImpl.socketConnect(本地方法)
java 。净。 java.net上的AbstractPlainSocketImpl.doConnect(未知源)
java.net上的AbstractPlainSocketImpl.connectToAddress(未知源) connect(未知源)
在java.net.SocksSocketImpl.connect(未知源)
在java.net.Socket.connect(未知源)
在java.net.Socket.connect(未知来源)com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)的
com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)的
b $ b com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642) at javax.mail.Service.connect(Service.java:317) at javax.mail.Service.connect(Service.java:176) at javax.mail.Service.connect(Service.java:125) at javax.mail.Transport.send0(Transport.java:194) at javax.mail.Transport.send(Transport.java:124) at PlainTextEmailSender.sendPlainTextEmail(PlainTextEmailSender.java:50) at PlainTextEmailSender.main(PlainTextEmailSender.java:73) Caused by: java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)



    package net.codejava.mail;

    import java.util.Date;
    import java.util.Properties;

    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;

    public class PlainTextEmailSender {

        public void sendPlainTextEmail(String host, String port,
                final String userName, final String password, String toAddress,
                String subject, String message) throws AddressException,
                MessagingException {

            // sets SMTP server properties
            Properties properties = new Properties();
            properties.put("mail.smtp.host", host);
            properties.put("mail.smtp.port", port);
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.starttls.enable", "true");

            // creates a new session with an authenticator
            Authenticator auth = new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(userName, password);
                }
            };

            Session session = Session.getInstance(properties, auth);

            // creates a new e-mail message
            Message msg = new MimeMessage(session);

            msg.setFrom(new InternetAddress(userName));
            InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
            msg.setRecipients(Message.RecipientType.TO, toAddresses);
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            // set plain text message
            msg.setText(message);

            // sends the e-mail
            Transport.send(msg);

        }

        /**
         * Test the send e-mail method
         *
         */
        public static void main(String[] args) {
            // SMTP server information
            String host = "smtp.gmail.com";
            String port = "587";
            String mailFrom = "user_name";
            String password = "password";

            // outgoing message information
            String mailTo = "email_address";
            String subject = "Hello my friend";
            String message = "Hi guy, Hope you are doing well. Duke.";

            PlainTextEmailSender mailer = new PlainTextEmailSender();

            try {
                mailer.sendPlainTextEmail(host, port, mailFrom, password, mailTo,
                        subject, message);
                System.out.println("Email sent.");
            } catch (Exception ex) {
                System.out.println("Failed to sent email.");
                ex.printStackTrace();
            }
        }
    }


推荐答案

正如我所说,您的代码没有错。如果有的话,只是做一些测试,尝试删除Authentication部分,看看是否可行:

As I said, there's nothing wrong with your code. If anything, just to do some testing, try to drop the Authentication part to see if that works:

    public void sendPlainTextEmail(String host, String port,
            final String userName, final String password, String toAddress,
            String subject, String message) throws AddressException,
            MessagingException {

        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
// *** BEGIN CHANGE
        properties.put("mail.smtp.user", userName);

        // creates a new session, no Authenticator (will connect() later)
        Session session = Session.getDefaultInstance(properties);
// *** END CHANGE

        // creates a new e-mail message
        Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(userName));
        InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
        msg.setRecipients(Message.RecipientType.TO, toAddresses);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        // set plain text message
        msg.setText(message);

// *** BEGIN CHANGE
        // sends the e-mail
        Transport t = session.getTransport("smtp");
        t.connect(userName, password);
        t.sendMessage(msg, msg.getAllRecipients());
        t.close();
// *** END CHANGE

    }

我每天使用的代码从我的应用程序发送数十封电子邮件,并且保证100%有效-只要 smtp.gmail.com:587 是可以到达的。

That's the code I'm using every day to send dozens of emails from my application, and it is 100% guaranteed to work -- as long as smtp.gmail.com:587 is reachable, of course.

这篇关于无法连接到SMTP主机:smtp.gmail.com,端口:587;嵌套的异常是:java.net.ConnectException:连接超时:connect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆