无法连接到Gmail存储Javax邮件api

时间:2019-07-12 04:47:58

标签: java api javamail ui-automation

几天以来,我一直在尝试检索使用javax邮件api发送的电子邮件。使用的依赖- <artifactId>javax.mail</artifactId>

我收到错误消息,例如尝试连接到存储时无法连接到存储或连接超时。这是我的代码

public static void main(String[] args) throws IOException {
    String userName = "mncjhc@gmail.com";
    String password = "ckjbc";
    String subjectKeyword = "New  Registration – Successful";
    String fromEmail = "djhgdd@gmail.com";
    String bodySearchText = "You have successfully register to our services";
    searchEmail(userName, password, subjectKeyword, fromEmail, bodySearchText);
}

public static void searchEmail(String userName, String password, final String subjectKeyword, final String fromEmail, final String bodySearchText) throws IOException {
    Properties properties = new Properties();
    boolean val = false;
    // server setting
    properties.put("mail.store.protocol", "imap");
    properties.put("mail.imap.host", "imap.gmail.com");
    properties.put("mail.imap.port", 143);

    // SSL setting
    properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    properties.setProperty("mail.imap.ssl.enable", "true");
    properties.setProperty("mail.imap.socketFactory.fallback", "false");
    properties.setProperty("mail.imap.socketFactory.port", String.valueOf(143));
    properties.put("mail.imaps.ssl.checkserveridentity", "false");
    properties.put("mail.imaps.ssl.trust", "*");
    properties.put("mail.imap.fetchsize", "519200");
    properties.put("mail.imaps.connectiontimeout", 1000);
    properties.put("mail.imaps.connectionpooltimeout", 1000);
    properties.put("mail.imaps.onTimeout", 1000);

    Session session = Session.getDefaultInstance(properties,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(
                            userName, password);// Specify the Username and the PassWord
                }
            });

    try {
        // connects to the message store
        Store store = session.getStore("imap");
        store.connect("imap.gmail.com", Integer.parseInt("143"), userName, password);
        System.out.println("Connected to Email server….");
        // opens the inbox folder
        Folder folderInbox = store.getFolder("INBOX");
        folderInbox.open(Folder.READ_ONLY);
        folderInbox.close(false);
        store.close();
    }
}

1 个答案:

答案 0 :(得分:0)

仅使用以下设置对我有用;

public static void searchEmail(String userName, String password) {
    Properties props = new Properties();
    props.setProperty("mail.imap.ssl.enable", "true");

    try {
        Session session = Session.getDefaultInstance(props, null);
        // connects to the message store
        Store store = session.getStore("imap");
        store.connect("imap.gmail.com", Integer.parseInt("993"), userName, password);
        System.out.println("Connected to Email server….");
        // opens the inbox folder
        Folder folderInbox = store.getFolder("INBOX");
        folderInbox.open(Folder.READ_ONLY);
        folderInbox.close(false);
        store.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

也不要忘记阅读Google的this。您需要从目标电子邮件帐户启用IMAP。请按照第1步:检查IMAP是否已打开

中的步骤进行操作。