4
votes

i'm trying to connect my app to Gmail to check emails. I must use SSL for POP3.

This is my code:

    Properties props = new Properties();

    props.put("mail.host", "pop.gmail.com");
    props.put("mail.store.protocol", "pop3s");
    props.put("mail.pop3s.auth", "true");
    props.put("mail.pop3s.port", "993");

    Session session = Session.getDefaultInstance(props, null);
    Store store=session.getStore();
    store.connect("[email protected]","mypass");

And I get this error:

Exception in thread "main" javax.mail.MessagingException: Connect failed;
  nested exception is:
    java.io.IOException: Unexpected response: * OK Gimap ready for requests from x.x.x.x.x z50if25691877wef.13
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:210)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:196)

I think this has a good new: the gmail server answered, however... seems to answer in a bad way for javamail.

2
If found this on Protocol.java of javax.mail source: if (line.startsWith("+OK")) r.ok = true; And google answer "* OK"...Tobia
First, change Session.getDefaultInstance to Session.getInstance. Then, I think if you look at the session debugging output you'll see that you're actually connecting to imap.gmail.com. The correct host name for POP3 access is pop.gmail.com. Note also that you don't need to set the mail.pop3s.port property if you're using the default, and there is no mail.pop3s.auth property so there's no need to set it. Check the JavaMail FAQ for a complete example.Bill Shannon

2 Answers

0
votes

I think it will help you

 Properties properties = new Properties();  
 properties.put("mail.pop3.host", pop3Host);
 properties.put("mail.pop3.ssl.enable", true);
 properties.put("mail.pop3.ssl.trust", "*");
 properties.put("mail.pop3.port", 995);
 Session emailSession = Session.getDefaultInstance(properties); `enter code here`