MailConnectException:无法连接到主机,端口:smtp.gmail.com,465;超时-1 [英] MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1

查看:2199
本文介绍了MailConnectException:无法连接到主机,端口:smtp.gmail.com,465;超时-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to send an email from my application using Gmail as the SMTP server. This is my mail connector class and I have set values in a separate property file

    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 = abcalerts@gmail.com
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的邮件发件人类

Then I implemented a mail sender class, called "GroupEmail.class"

public class GroupEmail {

    public void sendEmail() throws IOException {
        String recipient = "nirmalauwucst@gmail.com";

        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("abcalerts@gmail.com", "ABC Alerts"));

            msg.setReplyTo(InternetAddress.parse("abcalerts@gmail.com"));

            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, "abcalerts@gmail.com", "abcalert321");

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

}

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

After all I called the "GroupEmail.class" in a place that I needed to trigger the email to be sent.

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

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

I used Tomcat v8 server on localhost and when the application works, I got the below exception.

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


推荐答案

修复这些常见的JavaMail错误

按照 JavaMail FAQ中的连接调试技巧

最有可能出现在那里防火墙或防病毒产品,阻止您连接。

Most likely there's a firewall or anti-virus product that's preventing you from connecting.

如果Tomcat与Java安全管理器一起运行, JavaMail FAQ包含有关配置安全权限的信息。如果这没有帮助, JavaMail FAQ也有关于调试安全权限问题

If Tomcat is running with a Java security manager, the JavaMail FAQ has information about configuring security permissions. If that doesn't help, the JavaMail FAQ also has information about debugging problems with security permissions.

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

Did I mention that you should read the JavaMail FAQ? :-)

这篇关于MailConnectException:无法连接到主机,端口:smtp.gmail.com,465;超时-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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