当前位置: 首页 > 知识库问答 >
问题:

无法连接到SMTP主机:smtp.gmail.com,端口:465,响应:-1为什么465不工作

阳宾实
2023-03-14

我想创建电子邮件模板,因为我想附加html代码来创建模板,所以我尝试了下面的代码,在这个端口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不工作的原因,以及什么是响应:-1有人能给我建议一个解决方案吗

共有2个答案

管梓
2023-03-14

我的jre 1.8中的安全文件不允许它连接。我使用了corretto jre 1.8版本,它工作了。这个答案是在访问这个和这个答案的可能克隆2天后得出的。对我来说,这里讨论的文件位于路径$JAVA\u HOME/lib/security/JAVA中。安全编辑:2021 4月20日发布了java 1.8安全版本。该解决方案仅适用于在此之后出现问题的用户

韩安顺
2023-03-14

Google SMTP要求端口465使用SSL而不是STARTTLS。

只需删除:

<代码>道具。put(“mail.smtp.starttls.enable”,“true”)

并添加以使用SSL:

<代码>道具。put(“mail.smtp.socketFactory.class”,“javax.net.ssl.SSLSocketFactory”)

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

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

 类似资料: