简体   繁体   中英

Unable to connect email Server on JDK 8

I am not able to connect IMAP mail server to fetch the mails from jdk8 but it works fine with jdk7. Below is my code

private Properties getServerProperties(String protocol, String host, String port) {
    Properties properties = new Properties();
    properties.put(String.format("mail.%s.host", protocol), host);
    properties.put(String.format("mail.%s.port", protocol), port);
    properties.setProperty(String.format("mail.%s.socketFactory.class", protocol), "javax.net.ssl.SSLSocketFactory");
    properties.setProperty(String.format("mail.%s.socketFactory.fallback", protocol), "false");
    properties.setProperty(String.format("mail.%s.socketFactory.port", protocol), String.valueOf(port));
    return properties;
}

public void getNewEmails(String protocol, String host, String port, String userName, String password) {
    Properties properties = getServerProperties(protocol, host, port);
    Session session = Session.getDefaultInstance(properties);
    session.setDebug(true);

    try {
        Store store = session.getStore(protocol);
        store.connect(userName, password);

        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);

        int count = inbox.getMessageCount();
        Message[] messages = inbox.getMessages(1, count);
        for (Message message : messages) {

            Address[] fromAddresses = message.getFrom();
            System.out.println("...................");
            System.out.println("\t From: " + fromAddresses[0].toString());
            System.out.println("\t To: " + parseAddresses(message.getRecipients(RecipientType.TO)));
            System.out.println("\t CC: " + parseAddresses(message.getRecipients(RecipientType.CC)));
            System.out.println("\t Subject: " + message.getSubject());
            System.out.println("\t Sent Date:" + message.getSentDate().toString());
            System.out.println(message.getContent());
        }
        inbox.close(false);
        store.close();
    } catch (NoSuchProviderException ex) {
        System.out.println("No provider for protocol: " + protocol);
        ex.printStackTrace();
    } catch (MessagingException ex) {
        System.out.println("Could not connect to  the message store");
        ex.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private String parseAddresses(Address[] address) {

    String listOfAddress = "";
    if ((address == null) || (address.length < 1))
        return null;
    if (!(address[0] instanceof InternetAddress))
        return null;

    for (int i = 0; i < address.length; i++) {
        InternetAddress internetAddress = (InternetAddress) address[0];
        listOfAddress += internetAddress.getAddress() + ",";
    }
    return listOfAddress;
}

When I try to run the above code it hangs on store.connect(userName, password);

Below is the link from where I tried the above code http://www.developer.com/java/data/monitoring-email-accounts-imap-in-java.html . Here is a link which has no answer for the question https://community.oracle.com/message/13244272#13244272

Below is the logs while running with both JDK.

JDK 7 LOGS :

    DEBUG:setDebug:

JavaMail version 1.5.0-
b01
DEBUG:

getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: trying to connect to host "XXXXXXXXXXXXXXXXXXX", port 993, isSSL false
* OK server ready. Unauthorized Access Prohibited.
A0 CAPABILITY
* CAPABILITY IMAP4REV1 IDLE AUTH=PLAIN
A0 OK CAPABILITY completed
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: protocolConnect login, host=XXXXXXXXXXXXXXXXXXX, user=XXXXXXXXXXXXXXXXXXX, password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAP: AUTHENTICATE PLAIN command result: A1 OK AUTHENTICATE completed
A2 CAPABILITY
* CAPABILITY IMAP4REV1 IDLE AUTH=PLAIN
A2 OK CAPABILITY completed
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: connection available -- size: 1
A3 SELECT INBOX

JDK 8 LOGS :

    DEBUG:setDebug:

JavaMail version 1.5.0-
b01
DEBUG:

getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: trying to connect to host "XXXXXXXXXXXXXXXXXXX", port 993, isSSL false
* OK server ready. Unauthorized Access Prohibited.
A0 CAPABILITY
* CAPABILITY IMAP4REV1 IDLE AUTH=PLAIN
A0 OK CAPABILITY completed
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: protocolConnect login, host=XXXXXXXXXXXXXXXXXXX, user=XXXXXXXXXXXXXXXXXXX, password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed

After the above line it hangs and did not respond.

After Bill Shannon comment I tried below configuration and it start working with below configuration with JDK-8.

`<dependency>
 <groupId>com.sun.mail</groupId>
 <artifactId>javax.mail</artifactId>
 <version>1.5.5</version>
 </dependency>`  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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