gpt4 book ai didi

java - 使用java发送邮件验证错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:30:03 28 4
gpt4 key购买 nike

我看到很多关于发送电子邮件时的身份验证错误且主机是 gmail 的错误。我在没有身份验证的情况下尝试了不同的属性,但仍然没有任何反应。我不知道为什么在其他教程中,此代码可以正常工作,但当我尝试在这里运行它时,出现身份验证错误。我已经导入了我的库,但仍然是错误。我也尝试过不同的 Gmail 帐户,但没有任何反应。我尝试过的所有帐户均经过验证。怎么了?代码如下:

import java.util.Properties;

import javax.mail.Authenticator;
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 SendEmail {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");


Session session = Session.getDefaultInstance(properties, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("mine@gmail.com", "minepass");
}
});


try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("yours@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("theirs@yahoo.com"));
message.setSubject("Send meessage");
message.setText("Email received");
Transport.send(message);

System.out.println("Sent");
}
catch(MessagingException e){
throw new RuntimeException(e);
}


}
}

这是输出日志:

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/answer/14257 hg3sm8230925pbb.52 - gsmtp

at SendEmail.main(SendEmail.java:40)
Caused by: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/answer/14257 hg3sm8230925pbb.52 - gsmtp

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at SendEmail.main(SendEmail.java:35)

最佳答案

如果您使用 Gmail 帐户发送电子邮件 (SMTP),请确保您在应用程序中拥有正确的电子邮件密码,并为您的 Gmail 帐户启用此设置允许安全性较低的应用

允许安全应用程序-->转到管理帐户设置-->左侧导航单击安全性-->然后启用不太安全的应用程序。

关于java - 使用java发送邮件验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32284898/

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