tester
tester

Reputation: 263

Using JavaMail to read the mail

I am getting this error when tried to read mail using JavaMail. please let me know how to resolve this error. I have added activation.jar and mail.jar into eclipse.

DEBUG POP3: server doesn't support TOP, disabling it
javax.mail.AuthenticationFailedException: Command is not valid in this state.
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:174)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at library.VerifyEmail.main(VerifyEmail.java:40)

Below is the code I am trying:

package library;

import java.io.IOException;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import com.sun.mail.pop3.POP3Store;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.SubjectTerm;
import javax.activation.*;
import java.io.*;
public class VerifyEmail {
public static void main(String[] args) throws Exception {
     // SUBSTITUTE YOUR ISP's POP3 SERVER HERE!!!
    String host = "myhost";
    // SUBSTITUTE YOUR USERNAME AND PASSWORD TO ACCESS E-MAIL HERE!!!
    String user = "myuser";
    String password = "mypass";

 // Get a session.  Use a blank Properties object.
    Session session = Session.getInstance(new Properties());
    try {
        // Get a Store object
        Store store = session.getStore("pop3");
        store.connect(host, user, password);

        // Get "INBOX"
        Folder fldr = store.getFolder("INBOX");
        fldr.open(Folder.READ_WRITE);
        int count = fldr.getMessageCount();
        System.out.println(count  + " total messages");

        // Message numebers start at 1
        for(int i = 1; i <= count; i++) {
            // Get  a message by its sequence number
            Message m = fldr.getMessage(i);
         // Get some headers
            Date date = m.getSentDate();
            Address [] from = m.getFrom();
            String subj = m.getSubject();
            String mimeType = m.getContentType();
            System.out.println(date + "\t" + from[0] + "\t" +
                                subj + "\t" + mimeType);
        }

    }catch (MessagingException  ioex) {
        ioex.printStackTrace();
    }
}
}

Upvotes: 0

Views: 7825

Answers (3)

sakkie6yster
sakkie6yster

Reputation: 31

When you're getting the javax.mail.AuthenticationException it means that your application is unable to authenticate to the mail server.

One possible reason for this might be that an SSL certificate of the mail server is missing from the client keystore.

Upvotes: 3

Sanjay
Sanjay

Reputation: 11

DEBUG POP3: server doesn't support TOP, disabling it this message can be disable by updating the java mail jar version to 1.4.4

Upvotes: 0

Oded Peer
Oded Peer

Reputation: 2427

According to microsoft: For exchange 2010, by default, the server would need the client use ssl for pop3. Without ssl the server responds with "ERR command is not valid in this state."

Here's how to use javamail with ssl - Javamail and Gmail Pop3 SSL

Upvotes: 0

Related Questions