java - 使用 Java 发送电子邮件时出现 AuthenticationFailedException

标签 java spring jakarta-mail

我正在尝试使用 java API 发送电子邮件,并且提供了正确的电子邮件 ID 和密码,但仍然收到 AuthenticationFailedException。

我还尝试提供 host=mail.smtp.port 并将端口更改为 587,但我最终还是遇到了相同的错误..

请帮助我哪里出错了..?

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

  // Sender's email ID needs to be mentioned
  final String from = "from@gmail.com";

  // Assuming you are sending email from localhost
  final String host = "smtp.googlemail.com";

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);
  properties.setProperty("mail.smtp.starttls.enable", "true");
  properties.setProperty("mail.smtp.user", from);
  //properties.setProperty("mail.smtp.password", "xyz");

  properties.setProperty("mail.debug", "false");
  properties.setProperty("mail.smtp.auth", "true"); 
//  properties.setProperty("mail.smtp.port", "587");

  // Get the default Session object.
 // Session session = Session.getDefaultInstance(properties);

  Session session = Session.getInstance(properties,
          new javax.mail.Authenticator() {

              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(from, "xyz");
              }
          });

  session.setDebug(true);

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

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

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

     // Set Subject: header field
     message.setSubject("This is the Subject Line!");

     // Now set the actual message
     message.setText("This is actual message");

     // Send message
    // Transport.send(message);


     Transport.send(message);
     System.out.println("Sent message successfully....");
  }catch (MessagingException mex) {
     mex.printStackTrace();
  }
 }
}

错误:

javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)

最佳答案

只需检查您是否已使用this link启用从“不太安全的应用程序”登录。需要为帐户 from@gmail.com 启用此设置。

关于java - 使用 Java 发送电子邮件时出现 AuthenticationFailedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26271923/

相关文章:

java - openfire.xml 中指定的属性 'provider.user.className' 与数据库中存储的属性不同

java - Camel - REST DSL (2.14.0) 和 String bean

java - 使用 Spring 3.1 的 Java 配置来配置 spring-data-mongodb 存储库

spring - Java 邮件客户端无法从 Docker 容器访问 SMTP 服务器

java - 在没有用户干预或 Intent 的情况下发送电子邮件

java - 错误 - trustAnchors 参数必须非空

java - 如何使用基本身份验证从网站检索数据?

Java Mission Control - 访问被拒绝连接到远程

java - ACR122 USB SDK - JNI 调用 Winscard.dll

java - 如何让 Eclipse 调试器在某些类的对象发生更改时通知我