Can't receive mails with Pop in Android
Asked Answered
T

2

1

I can receive my mails with Imap with this code sample :

URLName server = new URLName("imaps://" + username + ":"+ password + "@imap.gmail.com/INBOX");
    Session session = Session.getDefaultInstance(new Properties(), null);
    Folder folder = session.getFolder(server);
if (folder == null) 
{
    System.exit(0);
}
   folder.open(Folder.READ_ONLY);
   Message[] messages = folder.getMessages();

But sometimes Imap doesn't give any service and at those times I want to use Pop but I couldn't use it with my code. It is different the other codes for using receive mail. But in Android only this code is working.

What should I change in this code to work with Pop?

Tophole answered 25/4, 2012 at 21:53 Comment(0)
M
1

First, there's a nice URLName constructor that takes all the component pieces as separate parameters, so you don't have to do string concatenation.

Switch from IMAP to POP3 requires changing the protocol name as well as the host name. See the JavaMail FAQ for examples. The protocol name is "pop3s" and the host name is "pop.gmail.com".

Finally, you should use Session.getInstance instead of Session.getDefaultInstance. Compare the javadocs for the two methods to understand why.

Marche answered 25/4, 2012 at 22:14 Comment(2)
Thanks for your reply, but it doesnt work. It give an exception "Connect failed"Subjoin
You did something wrong. Check the JavaMail FAQ for debugging tips and post the debugging output here so we can help you figure out what you did wrong.Marche
D
0

How about this one.Really worked for me!!(Source:here)

            String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

    Properties pop3Props = new Properties();

    pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
    pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
    pop3Props.setProperty("mail.pop3.port", "995");
    pop3Props.setProperty("mail.pop3.socketFactory.port", "995");

    URLName url = new URLName("pop3", "pop.gmail.com", 995, "","[email protected]",yourpassword);
    Session session = Session.getInstance(pop3Props, null);
    Store store = new POP3SSLStore(session, url);
    try {
        store.connect();
    } catch (MessagingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    Folder folder = null;
    try {
        folder = store.getDefaultFolder();

        folder = folder.getFolder("INBOX");

    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (folder == null) {
        System.exit(0);
    }
    try {
        folder.open(Folder.READ_ONLY);
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Try retreiving folder via store object.And also mention that the folder you wish to retreive is INBOX!Also note that in settings,port number is 995 form pop.(You may leave the first six lines as they are.)

Doone answered 26/4, 2012 at 5:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.