gpt4 book ai didi

java - 尝试使用 gmail 在 Java 中发送电子邮件总是导致用户名和密码不被接受

转载 作者:行者123 更新时间:2023-11-29 07:22:03 25 4
gpt4 key购买 nike

当我调用发送方法时(在设置 studentAddress 之后),我得到这个:

javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted. Learn more at<br/>
535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 y15sm906936wfd.10

我非常确定代码是正确的,并且 100% 肯定我输入的用户名和密码详细信息是正确的。那么这是 gmail 有问题还是什么?

这是我的代码:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendEmail {

private String host = "smtp.gmail.com";
private String emailLogin = "xxx@gmail.com";
private String pass = "xxx";
private String studentAddress;
private String to;
private Properties props = System.getProperties();

public SendEmail() {
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", emailLogin);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
to = "xxx@gmail.com";
}

public void setStudentAddress(String newAddress) {
studentAddress = newAddress;
}

public void send() {
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(emailLogin));

InternetAddress[] studentAddressList = {new InternetAddress(studentAddress)};
message.setReplyTo(studentAddressList);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Test Email");
message.setText("This is a test email!");
Transport transport = session.getTransport("smtps");
transport.connect(host, emailLogin, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException me) {
System.out.println("There has been an email error!");
me.printStackTrace();
}
}

}

任何想法...

最佳答案

有两种解决方法:

您可以通过链接生成应用程序专用密码,即“https://accounts.google.com/IssuedAuthSubTokens”和使用生成的应用程序专用密码代替您的原始密码.我已经完成了这个及其工作

出现异常(javax.mail.AuthenticationFailedException:535-5.7.1 需要特定应用程序密码)的原因是您可能已经激活了 gmail 帐户的两步验证。如果您使用未激活两步验证的帐户,则可以使用原始密码发送电子邮件。我也试过这个并且工作正常。

关于java - 尝试使用 gmail 在 Java 中发送电子邮件总是导致用户名和密码不被接受,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2831610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com