如何在android中以编程方式获取gmail邮件

时间:2012-06-07 04:09:02

标签: android

我希望将所有不同邮件服务的收件箱邮件(如gmail,hotmail,rediffmail等)收录到我的应用程序中。最初我尝试使用Imap获取gmail邮件。但我得到一个套接字例外。这是我试过的代码。请帮帮我。

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);
    }
}

1 个答案:

答案 0 :(得分:18)

试试这个......

 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();