11

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", "aravelliramesh35@gmail.com", "pswd");
    }
  //}catch (NoSuchProviderException e) {
  //   e.printStackTrace();
  //   System.exit(1);
  //} 
    catch (MessagingException e) {
        e.printStackTrace();
        System.exit(2);
    }
}
B770
  • 1,272
  • 3
  • 17
  • 34
Aravelli Ramesh
  • 131
  • 1
  • 2
  • 5

1 Answers1

18

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();
Laxman Rana
  • 2,904
  • 3
  • 25
  • 37
  • if my answer helps u to solve u r problem..then mark it as answer so others can refer it... – Laxman Rana Jun 08 '12 at 17:41
  • @ Lucky Boy i tried to implement it .. downloaded 4 java files but it contains error? do i missed any jar file?Error is in EmailMessenger.jav – vnshetty May 31 '13 at 06:28
  • 1
    I am always getting socket timeout. why is it so? – dinesh sharma Sep 24 '13 at 13:54
  • @LuckyBoy downloaded the source code but i'am getting connection refused exception. I'm connected through a proxied network – Sayed Jalil Hassan Dec 16 '13 at 12:17
  • @LuckyBoy i got many error regarding this code where is Store Class where is mail.store.protocol ??? – Bhanu Sharma Oct 06 '15 at 05:14
  • can we get from and to id ? – Dakshesh Khatri Oct 29 '15 at 10:46
  • 1
    It is necessary to download the javax.mail library ir order to use the code given above. You can do this by adding `compile 'javax.mail:mail:1.4.7'` to your dependencies in gradle. Hope this helps – blastervla Nov 23 '16 at 15:02
  • Is there somewhere an example how to use the "getMails()" Method from an other class? Or could someone explain please?! I have a button and an MultiLine Textbox. I want to run the getMails method in the onClick Method of the button. But it cant resolve this method. – Sardar Agabejli Oct 04 '17 at 23:19
  • There is no way to get inbox without asking password to user for registered gmail account? – Yucel Bayram Nov 09 '17 at 11:38