【问题标题】:Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1 why 465 is not working无法连接到 SMTP 主机:smtp.gmail.com,端口:465,响应:-1 为什么 465 不起作用
【发布时间】:2017-08-12 11:51:37
【问题描述】:

我想创建电子邮件模板,因为我想附加 html 代码来创建模板,所以我尝试了下面的代码,其中 port 465 号码不起作用 谁能帮帮我?

package com.indoabus2.mail;

import java.util.Properties;

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

public class SendHTMLEmail {

public static void main(String[] args) {
      // Recipient's email ID needs to be mentioned.
      String to = "vpenchalaprasad2@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "vpenchalaprasad2@gmail.com";
      final String username = "vpenchalaprasad2";//change accordingly
      final String password = "100509732041";//change accordingly

      // Assuming you are sending email through relay.jangosmtp.net
      String host = "smtp.gmail.com";

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

      // Get the Session object.
      Session session = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(username, password);
            }
    });

      try {
            // Create a default MimeMessage object.
            Message message = new MimeMessage(session);

       // Set From: header field of the header.
       message.setFrom(new InternetAddress(from));

       // Set To: header field of the header.
       message.setRecipients(Message.RecipientType.TO,
              InternetAddress.parse(to));

       // Set Subject: header field
       message.setSubject("Testing Subject");

       // Send the actual HTML message, as big as you like
       message.setContent(
              "<h1>This is actual message embedded in HTML tags</h1>",
             "text/html");

       // Send message
       Transport.send(message);

       System.out.println("Sent message successfully....");

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



}

坚果代码没有被执行异常是

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

我无法追踪为什么465 不起作用,以及response: -1 是什么,任何人都可以建议我一个解决方案

【问题讨论】:

    标签: java email


    【解决方案1】:

    Google SMTP 需要 SSL 而不是 STARTTLS 端口 465。

    只需删除:

    props.put("mail.smtp.starttls.enable", "true");

    并添加以使用 SSL:

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

    或者您可以将端口更改为 587。

    https://support.google.com/a/answer/176600?hl=en-EN

    【讨论】:

    • 不指定 socketfactory,“更好”的方法是使用 JavaMail smtps 协议而不是 smtp。
    • 感谢您在更改该行后重播以下异常'javax.mail.AuthenticationFailedException:535-5.7.8 用户名和密码不被接受。但我的密码和邮件 ID 是正确的
    • 您需要启用“不太安全的应用程序才能访问您的帐户”,看看这个page
    【解决方案2】:

    我的 jre 1.8 中的安全文件不允许它连接 我使用了 corretto jre 1.8 版本,它工作正常。 这个答案是在访问这个和这个答案的可能克隆 2 天之后出现的。 对我来说,这里有问题的文件位于路径 $JAVA_HOME/lib/security/java.security 编辑:2021 年 4 月 20 日有一个 java 1.8 安全版本 此解决方案仅对之后遇到问题的人有效

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 2014-03-08
      • 2015-12-27
      • 1970-01-01
      • 2012-01-25
      • 2011-12-09
      相关资源
      最近更新 更多