我正在尝试使用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....
我应该尝试改变什么?
答案 0 :(得分:3)
可能是Gmail帐户未配置为使用安全性较低的身份验证。您可以登录到您尝试发送电子邮件的帐户,然后转到此page来更改此设置。在该页面上,您可以通过不太安全的身份验证打开访问权限这应该可以解决问题