Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get gmail mails programmatically in android

Tags:

android

I want to get all inbox mails of different mail services like gmail, hotmail, rediffmail etc. into my application. Initially I tried to get gmail mails using Imap. But I am getting a socket exception. Here is the code I tried. Please help me.

public void getMails(View V){
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imap.port", "993");

props.setProperty("mail.imap.socketFactory.class", .ssl.SSLSocketFactory");
props.setProperty("mail.imap.socketFactory.fallback", "false");
    try {
      Session session = Session.getDefaultInstance(props, null);
      Store store = session.getStore("imaps");
      store.connect("imap.gmail.com", "[email protected]", "pswd");
    }
  //}catch (NoSuchProviderException e) {
  //   e.printStackTrace();
  //   System.exit(1);
  //} 
    catch (MessagingException e) {
        e.printStackTrace();
        System.exit(2);
    }
}
like image 773
Aravelli Ramesh Avatar asked Jun 07 '12 04:06

Aravelli Ramesh


1 Answers

Try this...

 Properties props = new Properties();
    //IMAPS protocol
    props.setProperty(“mail.store.protocol”, “imaps”);
    //Set host address
    props.setProperty(“mail.imaps.host”, imaps.gmail.com);
    //Set specified port
    props.setProperty(“mail.imaps.port”, “993″);
    //Using SSL
    props.setProperty(“mail.imaps.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”);
    props.setProperty(“mail.imaps.socketFactory.fallback”, “false”);
    //Setting IMAP session
    Session imapSession = Session.getInstance(props);

Store store = imapSession.getStore(“imaps”);
//Connect to server by sending username and password.
//Example mailServer = imap.gmail.com, username = abc, password = abc
store.connect(mailServer, account.username, account.password);
//Get all mails in Inbox Forlder
inbox = store.getFolder(“Inbox”);
inbox.open(Folder.READ_ONLY);
//Return result to array of message
Message[] result = inbox.getMessages();
like image 50
Laxman Rana Avatar answered Oct 11 '22 15:10

Laxman Rana