MailConnectException:无法连接到主机,端口:smtp.gmail.com,465;超时-1

问题描述 投票:3回答:2

我需要使用Gmail作为SMTP服务器从我的应用程序发送电子邮件。这是我的邮件连接器类,我在一个单独的属性文件中设置了值

    public class EmailConnector {


    public static Session sessionCreate() {
        final String fromEmail = ConfigurationManager.getInstance().getProperty(EmailConfig.SENDER_EMAIL.toString());

        final String password = ConfigurationManager.getInstance().getProperty(EmailConfig.SENDER_PASSWORD.toString());

        Properties props = new Properties();
        props.put("mail.smtp.host", ConfigurationManager.getInstance().getProperty(EmailConfig.SMTP_HOST.toString()));

        props.put("mail.smtp.socketFactory.port",
                ConfigurationManager.getInstance().getProperty(EmailConfig.SSL_PORT.toString()));

        props.put("mail.smtp.socketFactory.class",
                ConfigurationManager.getInstance().getProperty(EmailConfig.SSL_FACTORY_CLASS.toString()));

        props.put("mail.smtp.auth",
                ConfigurationManager.getInstance().getProperty(EmailConfig.SMTP_AUTHENTICATION.toString()));

        props.put("mail.smtp.port", ConfigurationManager.getInstance().getProperty(EmailConfig.SMTP_PORT.toString()));

        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail, password);
            }
        };
        return Session.getDefaultInstance(props, auth);
    }
}

属性:

#Email send configuration
SENDER_EMAIL = [email protected]
SENDER_PASSWORD = abcalert321
SMTP_HOST = smtp.gmail.com
SSL_PORT = 465
SMTP_AUTHENTICATION = true
SMTP_PORT = 465
SSL_FACTORY_CLASS = javax.net.ssl.SSLSocketFactory

然后我实现了一个名为“GroupEmail.class”的邮件发件人类

public class GroupEmail {

    public void sendEmail() throws IOException {
        String recipient = "[email protected]";

        Session session = EmailConnector.sessionCreate();
        /* subject of email */
        String emailSubject = "ABC_Alert";
        try {
            MimeMessage msg = new MimeMessage(session);
            msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
            msg.addHeader("format", "flowed");
            msg.addHeader("Content-Transfer-Encoding", "8bit");

            msg.setFrom(new InternetAddress("[email protected]", "ABC Alerts"));

            msg.setReplyTo(InternetAddress.parse("[email protected]"));

            msg.setSubject(emailSubject, "UTF-8");

            msg.setSentDate(new Date());
            /* buyer email address */
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));

            /* Create the message body part */
            msg.setText("A new Test-Alert from AB_Alerts");

            /* Send message */
            Transport.send(msg, "[email protected]", "abcalert321");

        } catch (MessagingException | UnsupportedEncodingException e) {
            SystemLogger.logErrorMessege(this, e);
        }
    }

}

毕竟我在一个需要触发要发送的电子邮件的地方调用了“GroupEmail.class”。

GroupEmail groupEmail = new GroupEmail();
        groupEmail.sendEmail(); 

我在localhost上使用了Tomcat v8服务器,当应用程序工作时,我得到了以下异常。

98656 [http-nio-8080-exec-9] ERROR it.codegen.rnd.cgalert.notification.template.email.GroupEmail  - Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
  nested exception is:
    java.net.SocketException: Permission denied: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
    at javax.mail.Service.connect(Service.java:388)
    at javax.mail.Service.connect(Service.java:246)
    at javax.mail.Service.connect(Service.java:195)
    at javax.mail.Transport.send0(Transport.java:254)
    at javax.mail.Transport.send(Transport.java:124)
..more
java-ee javamail
2个回答
2
投票

修复这些common JavaMail mistakes

关注connection debugging tips in the JavaMail FAQ

很可能是防火墙或防病毒产品阻止您连接。

如果Tomcat正在运行Java安全管理器,那就是JavaMail FAQ has information about configuring security permissions。如果这没有帮助,JavaMail FAQ also has information about debugging problems with security permissions

我提到你应该阅读JavaMail FAQ吗? :-)


1
投票

我知道这是一个非常晚的答案,但想分享我的经验,当我面对同样的问题,并尝试几乎Java核心邮件api,Apache邮件api和Spring也为MimeMessage

我用Java JDK 1.7.0_80尝试了50次以上并且多次失败并且上面遇到异常然后我转移到了JDK 1.8.0_151。

现在,在说明源代码之前,我想分享我的配置。

在qazxsw poi> Gmail下去qazxsw poi> qazxsw poi> qazxsw poi。

允许不太安全的应用程序:关闭(意味着我的应用程序安全性不低)

两步验证:关闭

应用密码(点击它,谷歌将为您生成一个16个字符长的密码,稍后完成,用这个16个字符长的密码(没有任何空间)更改您的Gmail密码)。

Settings

现在,我的源代码是:

Other Google Account settings

Accounts and Import调试记录在下面说:

Sign-in & security

另外,请检查您的互联网连接,防病毒设置和防火墙访问是否不间断,希望这将有助于许多人浪费时间。

© www.soinside.com 2019 - 2024. All rights reserved.