0

I want to connect to our test email server and want to fetch the mail contents for any specific user.

In the code mentioned below I print the value of 'store' object and found that it printed the machine user name that i have logged in with. However the email for which i want to fetch the mail content is different from machine user.

  import javax.mail.*;
    import java.util.Properties;    
    public class VerifyEmails {
        public void check(String host, String user, String password) {
            try {
                Properties properties = new Properties();
                properties.put("mail.pop3.host", host);
                properties.put("mail.pop3.user", user);
                properties.put("mail.pop3.port", "143");
                properties.put("mail.pop3.starttls.enable", "true");
                Session emailSession = Session.getDefaultInstance(properties);
                Store store = emailSession.getStore("pop3s");
                System.out.println("test1 " + store);
                store.connect(host, user, password);
                Folder emailFolder = store.getFolder("INBOX");
                emailFolder.open(Folder.READ_ONLY);
                Message[] messages = emailFolder.getMessages();
                System.out.println("messages.length---" + messages.length);
                for (int i = 0, n = messages.length; i < n; i++) {
                    Message message = messages[i];
                    System.out.println("---------------------------------");
                    System.out.println("Email Number " + (i + 1));
                    System.out.println("Subject: " + message.getSubject());
                    System.out.println("From: " + message.getFrom()[0]);
                    System.out.println("Text: " + message.getContent().toString());
                }
                emailFolder.close(false);
                store.close();
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void main(String args[]) {
            VerifyEmails ch = new VerifyEmails();
            ch.check("testServer", "test1@anydomain.com", "hello");
        }
    }

This question has been asked many times and most of the answers shows that we should set, mail.pop3.starttls.enable"= "true" .

  • I have tried with same but did not succeed.

  • I am getting below exception when running the code.

    javax.mail.MessagingException: Connect failed; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209) at javax.mail.Service.connect(Service.java:345) at javax.mail.Service.connect(Service.java:226)

user207421
  • 305,947
  • 44
  • 307
  • 483
vish
  • 901
  • 1
  • 7
  • 20
  • Are you sure you should be connecting to port 143? that is the imap port, pop3 is 110, pop3s is 995. Have you tried telneting to that port and doing a starttls manually? – BevynQ Apr 21 '16 at 10:38
  • i did tried with 110 also but got same result. Using telnet i am able to connect to server with both 110 and 143. – vish Apr 21 '16 at 12:28
  • Using Telnet you aren't using SSL. The 'plaintext connection?' message strongly suggests that you shouldn't be using SSL for this connection either. – user207421 Apr 22 '16 at 01:31

0 Answers0