Java发送Gmail遇AuthenticationFailedException(无低安全应用选项)解决方案
问题
我尝试用Java发送邮件,代码如下。已确认所有凭据正确,但仍抛出错误:javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted。之前常用的“开启低安全应用”选项已被谷歌禁用,请问该怎么解决?
import java.util.Properties; 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) { // Recipient's email ID needs to be mentioned. String to = "sender@gmail.com"; // Sender's email ID needs to be mentioned String from = "receiver@gmail.com"; // Assuming you are sending email from through gmails smtp String host = "smtp.gmail.com"; // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", "465"); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // Get the Session object.// and pass username and password Session session = Session.getInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("receiver@gmail.com", "password"); } }); // Used to debug SMTP issues session.setDebug(true); try { // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Now set the actual message message.setText("This is actual message"); System.out.println("sending..."); // Send message Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } } }
错误提示:javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted
解决方法
1. 启用两步验证并生成应用专用密码
谷歌已经禁用低安全应用权限,现在必须通过应用专用密码来登录SMTP服务,前提是你的谷歌账号已经开启两步验证:
- 登录谷歌账号,进入「安全」设置页面,找到并开启「两步验证」,按照提示完成绑定;
- 开启后,在同一页面找到「应用专用密码」选项,点击创建;
- 选择应用类型为「邮件」,设备类型选「其他」(比如输入“Java邮件客户端”),点击生成;
- 把生成的16位无空格密码替换代码里的原密码。
2. 修正代码中的邮箱配置错误
你的代码里有个明显的逻辑错误:发件人和收件人的邮箱赋值搞反了,同时PasswordAuthentication里的邮箱必须和发件人邮箱一致,修正后代码片段如下:
// 收件人邮箱 String to = "receiver@gmail.com"; // 发件人邮箱 String from = "sender@gmail.com"; // ... protected PasswordAuthentication getPasswordAuthentication() { // 这里要填发件人邮箱和对应的应用专用密码 return new PasswordAuthentication("sender@gmail.com", "生成的16位应用专用密码"); }
3. 简化SMTP配置(可选优化)
JavaMail的新版本已经不需要手动指定mail.smtp.socketFactory.class,可以把这行配置删掉,简化后的配置如下:
properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", "465"); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.auth", "true");
内容的提问来源于stack exchange,提问作者Suchith Kumar
相关产品推荐
相关产品推荐

