我发现有很多关于发送电子邮件时身份验证错误的错误,主机是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)
答案 0 :(得分:1)
如果您使用 Gmail 帐户发送电子邮件 (SMTP),请确保您在应用程序中拥有正确的电子邮件密码,并为您的应用启用此设置允许安全性较低的应用 Gmail 帐户。
允许安全应用-->进入管理账户设置-->左侧导航点击安全-->然后启用不太安全的应用。
答案 1 :(得分:0)
确保您已从外部和未经认证的应用启用了与您的Gmail帐户的连接。 有关启用安全性较低的应用以连接到Google帐户的详细信息,请检查:https://support.google.com/accounts/answer/6010255?hl=en 从一开始,这可能是您的问题,使安全性较低的应用程序能够连接到您的Gmail帐户并重新运行您的应用程序。
如果这不会有助于尝试将username
和password
放入属性中,则无需使用Authenticator
。
这是一个能够发送多个电子邮件的方法的工作示例。
注意:需要javax.mail
库
方法参数:
from
- 是您的电子邮件地址(来自gmail)
pass
- 您的密码
to
- 一系列收件人
subject
- 邮件标题
body
- 邮件正文
public static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i = 0; i < to.length; i++ ) {
toAddress[i] = new InternetAddress(to[i]);
}
for( int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (AddressException ae) {
ae.printStackTrace();
}
catch (MessagingException me) {
me.printStackTrace();
}
}