首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理"avax.mail.AuthenticationFailedException: 535-5.7.8用户名和密码不被接受“时,不太安全的应用程序功能

如何处理"avax.mail.AuthenticationFailedException: 535-5.7.8用户名和密码不被接受“时,不太安全的应用程序功能
EN

Stack Overflow用户
提问于 2022-08-08 17:38:23
回答 1查看 121关注 0票数 0

我试图用java发送一封电子邮件。这是我的密码。我所有的凭据都是正确的,但我还是得到了这个错误。我见过许多解决方案,其中说打开不那么安全的应用程序,但是现在这个功能被谷歌禁用了。那我该怎么解决呢。

代码语言:javascript
复制
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();
        }

    }

}

这就是我要犯的错误。我在vs代码中的错误

EN

回答 1

Stack Overflow用户

发布于 2022-08-09 11:03:20

由于less secure app特性已被删除,我们必须按照以下步骤通过third party softwareApp password使用Gmail

步骤1:设置2步验证:- Google -> 2-步骤验证->输入密码,询问->打开(您可以使用SMS获取Gmail代码以激活2步骤验证)

步骤2:生成应用程序机密:-谷歌帐户->安全->应用密码->输入密码问->选择应用程序和设备.->例如其他(Java应用程序) ->输入应用程序名称,例如MyApp ->生成

步骤3:使用生成的应用程序机密:-复制16字符密码,在应用程序中使用16字符密码(而不是实际密码)和Gmail用户名。

这应该可以节省你的时间

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73281984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档