由于 javax.mail.AuthenticationFailedException,无法使用 JavaMail 发送邮件
Can't send mail using JavaMail due to javax.mail.AuthenticationFailedException
我正在尝试配置我的应用程序以使用 gmail 发送邮件。我查阅了 gmail javamail 文档,我已经能够编写那段代码:
String from = mymail;
String pass = mypass;
String[] to = { somemail }; // list of recipient email addresses
String subject = "Java send mail example";
String body = "Welcome to JavaMail!";
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.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
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();
}
但我不断收到以下错误:
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.c....
我应该尝试改变什么?
可能是 Gmail 帐户未配置为使用不太安全的身份验证。您可以通过登录您尝试发送电子邮件的帐户然后转到此 page 来更改它。在该页面上,您可以通过不太安全的身份验证打开访问权限。这应该可以解决问题
我正在尝试配置我的应用程序以使用 gmail 发送邮件。我查阅了 gmail javamail 文档,我已经能够编写那段代码:
String from = mymail;
String pass = mypass;
String[] to = { somemail }; // list of recipient email addresses
String subject = "Java send mail example";
String body = "Welcome to JavaMail!";
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.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
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();
}
但我不断收到以下错误:
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.c....
我应该尝试改变什么?
可能是 Gmail 帐户未配置为使用不太安全的身份验证。您可以通过登录您尝试发送电子邮件的帐户然后转到此 page 来更改它。在该页面上,您可以通过不太安全的身份验证打开访问权限。这应该可以解决问题