无法连接到 SMTP 主机: smtp.gmail.com, 端口: 465, 响应: -1

2022-09-01 21:04:02

发送邮件时,我收到此错误

java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed;嵌套的异常是: 类 javax.mail.Messaging异常: 无法连接到 SMTP 主机: smtp.gmail.com, 端口: 465, 响应: -1

我的代码是:

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
        new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("email","password");
                }
        });

try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("email"));
        message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(this.to));
        message.setSubject("Testing");
        message.setText("Hey, this is the testing email.");



        Transport.send(message);

任何帮助将不胜感激。

提前致谢。


答案 1

您需要告诉它您正在使用SSL:

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

如果您错过了任何内容,这里是工作代码:

String  d_email = "address@gmail.com",
            d_uname = "Name",
            d_password = "urpassword",
            d_host = "smtp.gmail.com",
            d_port  = "465",
            m_to = "toAddress@gmail.com",
            m_subject = "Indoors Readable File: " + params[0].getName(),
            m_text = "This message is from Indoor Positioning App. Required file(s) are attached.";
    Properties props = new Properties();
    props.put("mail.smtp.user", d_email);
    props.put("mail.smtp.host", d_host);
    props.put("mail.smtp.port", d_port);
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", d_port);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    SMTPAuthenticator auth = new SMTPAuthenticator();
    Session session = Session.getInstance(props, auth);
    session.setDebug(true);

    MimeMessage msg = new MimeMessage(session);
    try {
        msg.setSubject(m_subject);
        msg.setFrom(new InternetAddress(d_email));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

Transport transport = session.getTransport("smtps");
            transport.connect(d_host, Integer.valueOf(d_port), d_uname, d_password);
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();

        } catch (AddressException e) {
            e.printStackTrace();
            return false;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }

答案 2

在使用 NetBeans 进行调试时,甚至在执行实际的 jar 文件时,我都遇到了此问题。防病毒软件会阻止发送电子邮件。您应该在调试期间暂时禁用防病毒软件,或者从扫描中排除 NetBeans 和实际的 jar 文件。就我而言,我正在使用Avast。

请参阅此链接,了解如何排除:如何将文件/网站异常添加到avast中!防病毒 2014

它对我有用。


推荐