我在我的应用程序中设置了一个Java邮件代码,它使用spring-mail发送电子邮件。我突然得到这个错误。
o.s.b.actuate.mail.MailHealthIndicator : Mail health check failed
javax.mail.AuthenticationFailedException: 454 4.7.0 Too many login attempts, please try again later. s189-20020a1f5ec6000000b0034e6f1fd03bsm1688226vkb.5 - gsmtp
导致这个问题的确切原因是什么?我该如何解决?使用的gmail帐户是一个简单的帐户,而不是一个组织帐户。
如果您想用Java发送电子邮件,请尝试以下代码:
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendEmail {
public static void sendMail(String recepient) throws Exception {
System.out.println("Preparing to send email");
Properties properties = new Properties();
properties.put("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.host", "smtp.gmail.com");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "465");
// properties.put("mail.debug", "true");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
String myAccountEmail = "yourmail";
String password = "your passsword";
//nebamhselddwspis
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myAccountEmail, password);
}
});
Message message = prepareMessage(session, myAccountEmail, recepient);
Transport.send(message);
System.out.println("Messaggio successfully sent");
}
private static Message prepareMessage(Session session, String myAccountEmail, String recepient) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(myAccountEmail));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
message.setSubject("Prova™");
message.setText("Ciao,ci sono delle nuove attività su Casotto, n effettua il Login per dare un'occhiata e prenotarti!");
return message;
} catch (Exception ex) {
Logger.getLogger(SendEmail.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
记住生成密码应用程序,如果你有两个因素的谷歌邮件认证。https://support.google.com/mail/answer/185833?hl=en