【问题标题】:How to access gmail with 2-step verification?如何通过两步验证访问 gmail?
【发布时间】:2013-05-12 04:06:13
【问题描述】:

最近我在我的 gmail 帐户中设置了两步验证,并尝试使用 Java Mail API 连接到我的 gmail 帐户,但它没有连接。

我的代码:

Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imaps");
    try {
        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("imaps");
        store.connect("imap.gmail.com", "my_account@gmail.com", "password");
        System.out.println(store);

        Folder inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        Message messages[] = inbox.getMessages();
        for (Message message : messages) {
            System.out.println(message);
        }
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (MessagingException e) {
        e.printStackTrace();
        System.exit(2);
    }

以及我从 logcat 中得到的:

javax.mail.AuthenticationFailedException: [ALERT] Application-specific password required: http://support.google.com/accounts/bin/answer.py?answer=185833 (Failure)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:660)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)

请问有什么方法可以解决这个问题。

提前致谢。

【问题讨论】:

    标签: java email imap


    【解决方案1】:

    当您为 gmail 帐户激活两步验证后,使用您的 gmail 凭据登录到以下链接并生成应用程序专用密码 (ASP)。您可以在代码中使用生成的密码。只需添加 ASP 来代替普通密码。它应该可以工作。

    https://accounts.google.com/IssuedAuthSubTokens?hide_authsub=1

    更新:2018 年 7 月 30 日

    当您打开此链接并手动使用 gmail id 和密码登录一次时,您将看到如下所示的此页面。在第一个下拉菜单中选择“邮件”选项

    在第二个下拉列表中,根据您的要求选择平台,选择“Windows 计算机”或“Mac”

    然后,单击生成按钮,您将在下一个屏幕中看到一个包含 16 个字母的代码。

    在你的java邮件程序中使用这个作为密码

    store.connect("imap.gmail.com", "gmail id used in the previous step", "16 alphabets code");

    【讨论】:

    • 有没有javamail api可以发送第二步验证码。
    • 请举个例子。
    猜你喜欢
    • 2013-07-08
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    相关资源
    最近更新 更多