javaemailimap

Failed Connection to login to Mailserver: NoSuchProviderException. IMAP


fellows,

I want to build an app that can read emails from a mailbox. I’ve checked that the settings for SMTP and IMAP are activated.

Here is the method

public Message[] readMails() throws MessagingException {
        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imap");
        properties.put("mail.imap.host", "imap.web.de");
        properties.put("mail.imap.port", "993");
        properties.put("mail.imap.ssl.enable", "true");
        properties.put("mail.imap.auth", "true");
        Session session = Session.getInstance(properties);

        Store store = session.getStore("imap");
        store.connect("imap.web.de", 993, "user", "password");
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);

        // Fetch unseen messages from inbox folder
        Message[] messages = inbox.search(
                new FlagTerm(new Flags(Flags.Flag.SEEN), false));

        // Sort messages from recent to oldest
        Arrays.sort(messages, (m1, m2) -> {
            try {
                return m2.getSentDate().compareTo(m1.getSentDate());
            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
        });

        return messages;
    }

If I executed the method throws an exception here

Store store = session.getStore("imap"); //thorws NoSuchProviderException: imap

I've checked videos and other projects from github. no success.


Solution

  • I finaly solved my own problem.

    It seems like there was a conflict with the JavaMail API versions in my project. I've both javax.mail:javax.mail-api:1.6.2 and com.sun.mail:imap:2.0.1 in my pom.xml. These two dependencies were causing a conflict.

    I removed javax.mail.

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.6.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>imap</artifactId>
        <version>2.0.1</version>
    </dependency>