发送SMTP电子邮件时发生AuthenticationFailedException错误

我尝试在java中发送SMTP电子邮件,但我有这样的错误,我没有收到邮件。 我关闭所有防火墙和防病毒软件。

错误:

javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 pd8sm1306363pdb.93 - gsmtp 

这是我的代码:

 private void btn_mailActionPerformed(java.awt.event.ActionEvent evt) { String to = "receive.address@gmail.com"; String from = "send.address@gmail.com"; final String username = "send.address"; final String password = "sendpassword"; String host = "smtp.gmail.com"; Properties pro = new Properties(); pro.put("mail.smtp.host",host); pro.put("mail.smtp.socketFactory.port","465"); pro.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); pro.put("mail.smtp.auth","true"); pro.put("mail.smtp.port","465"); Session session = Session.getInstance(pro, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username,password); } } ); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject("test mail"); message.setText("Hello how are you?"); Transport.send(message); JOptionPane.showMessageDialog(null,"Send"); } catch (Exception e) { JOptionPane.showMessageDialog(null,e.toString()); System.out.println(e.toString()); } 

从浏览器登录电子邮件并转到此页面 。 你会看到这个;

在此处输入图像描述

确保单击“打开”并再次尝试您的代码。

我遇到过同样的问题。 在使用Gmail进行大量测试后,我发现问题是Gmail需要OAuth登录,而不仅仅是密码。 解决方案是使用Gmail API 。 但是,这是一个非常复杂的解决方案,我不会详细介绍。 如果您对此感兴趣,请在此处阅读第一个答案。

但是,如果你想要一个简单的解决方案,我所做的只是切换到雅虎帐户。 由于雅虎不使用相同的加密技术,因此效果非常好。 注意:不要忘记将SMTP服务器更改为“smtp.mail.yahoo.com”,将端口更改为“25”。

如果要从头开始设置,只需按照本教程下载JavaMail API和Java Activation Framework即可 。

然后你可以复制并粘贴我的代码,更改顶部变量,一切都应该工作! 如果我遗漏了什么,请告诉我! 谢谢!

 import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class Mailer { public static void main(String[] args) { final String username = "your-email@yahoo.com"; final String password = "your-password"; final String recipient = "email-recipient"; final String subject = "message-subject"; final String emailmessage = "message"; Properties props = new Properties(); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.mail.yahoo.com"); props.put("mail.smtp.port", "25"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient)); message.setSubject(subject); message.setText(emailmessage); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); } } }