简体   繁体   English

由于javax.mail.AuthenticationFailedException,因此无法使用JavaMail发送邮件

[英]Can't send mail using JavaMail due to javax.mail.AuthenticationFailedException

I'm trying to configure my app to send mail, using gmail. 我正在尝试将我的应用程序配置为使用gmail发送邮件。 I looked up the gmail javamail documentation and I've been able to write that piece of code: 我查阅了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();
}

but I keep getting the following error: 但我不断收到以下错误:

javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.c....

What should I try to change ? 我应该尝试改变什么?

It may be that Gmail account is not configured to use less secure authentication. 可能是Gmail帐户未配置为使用安全性较低的身份验证。 You can change that by logging into the account you are trying to send email from and then going to this page . 您可以通过登录尝试发送电子邮件的帐户,然后转到此页面来更改此设置。 On that page you can turn on access by less secure authentication. 在该页面上,您可以通过不太安全的身份验证来打开访问权限。 This should solve the problem 这应该解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM