2

I am trying to get the gmail inbox using javamail with this code:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import java.util.Properties;

public class HomeActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button emailRead=(Button)findViewById(R.id.button);
        emailRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Runnable r=new Runnable() {
                    @Override
                    public void run() {
                        Properties props = new Properties();
                        props.setProperty("mail.store.protocol", "imaps");

                        Session session = Session.getDefaultInstance(props, null);
                        IMAPStore imapStore = null;

                        try {
                            imapStore = (IMAPStore) session.getStore("imaps");
                            imapStore.connect("imap.gmail.com", "myemail@gmail.com", "mypassword");
                            final IMAPFolder folder = (IMAPFolder) imapStore.getFolder("Inbox");
                            folder.open(IMAPFolder.READ_WRITE);
                            Message m[]=folder.getMessages();
                            for(Message n:m){
                                System.out.println(n.getSubject());
                            }
                        } catch (MessagingException e) {
                            e.printStackTrace();
                        }

                    }
                };
                Thread t=new Thread(r);
                t.start();
            }
        });
    }
}

It works in emulator, but when run on a real device(with working internet connection) I constantly fail and get the exception:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: imap.gmail.com, 993; timeout -1;
  nested exception is:
    java.net.SocketTimeoutException: failed to connect to imap.gmail.com/2607:f8b0:400e:c02::6c (port 993) after 90000ms

Why am I receiving an MailConnectException, and how can I fix it?


EDIT: Here are the links I have tried, but with the same result:

1)Reading Gmail mails using android SDK

2)Are there any good short code examples that simply read a new gmail message?

3)Reading all new messages from my gmail using javamail

Its just the subset of what I have tried before posting. I request someone to please share a live working code, which works on a real device here.

EDIT2:

I have tested this on three real devices. One was using wifi internet.Two others were using Gprs for net connectivity. So it appears that gmail setting for javamail have changed with android. The javamail code is working for me on desktop java.But seems something strange with android real devices.

Community
  • 1
  • 1
rahulserver
  • 10,411
  • 24
  • 90
  • 164
  • You're asking for *working* code - but you're not sharing *your* code. You should show what *doesn't work*, so we know what to avoid. – Phil Feb 05 '14 at 02:03
  • @Phil see the link in question. – rahulserver Feb 05 '14 at 02:05
  • @Phil any reason for the downvote? It think its a genuine problem because javamail is the most popular and i think the only api in a java to send/read email. The code used to work with android's earlier versions.It seems that there are a few changes in gmail settings or the api has gone obsolete. – rahulserver Feb 05 '14 at 02:12
  • I believe that because there is no code in the question, where clearly you are asking for code in the answer, this question is poorly written. From the [FAQ](http://stackoverflow.com/help/how-to-ask): "Not all questions benefit from including code. But if your problem is with code you've written, you should include some." – Phil Feb 05 '14 at 02:32
  • @Phil see the code.I have attached – rahulserver Feb 05 '14 at 02:42
  • Working Gmail code is in the [JavaMail FAQ](http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail), if there's any question. But if you're trying to connect to the right host and port, as it appears you are, and you're getting socket timeout exceptions, there's some lower level networking problem preventing you from connecting, perhaps a firewall? Is the real device using ethernet or cellular? Is it connecting through your company network? – Bill Shannon Feb 05 '14 at 03:06
  • @rahulserver this is an excellent question! Changing my downvote to an upvote. – Phil Feb 05 '14 at 03:25
  • @BillShannon i have tested this on three real devices. One was using wifi internet.Two others were using Gprs for net connectivity. So it appears that gmail setting for javamail have changed via android. The javamail code is workin for me on desktop java.But seems something strange with android. – rahulserver Feb 05 '14 at 04:20
  • rahulserver - sorry about removing some of the commentary. I thought it was a good question, so I wanted to try and get the downvoters off your back. – jww Feb 05 '14 at 05:38
  • @noloader Thanks. I dont care about the downvoters.Only that it reduces the hits of the question, hence it probability to be answered. I dont know what to do about this issue. I am surprised that how do modern emailing apps are able to read emails if javamail is defunct with android! – rahulserver Feb 05 '14 at 05:44
  • Others have gotten it to work, so there must be something different about your environment. Try making a plain java.net.Socket connection to the Gmail server and see if that times out. That will eliminate JavaMail as the source of the problem. – Bill Shannon Feb 05 '14 at 08:08
  • "Others have gotten it to work". The last question on SO in which someone claimed to have it work was over a year ago. Thats why I doubt that fact.Infact, if its no too much to ask, can u try to run that code snippet in your android device? its very simple and can be setup quickly. – rahulserver Feb 05 '14 at 08:16

1 Answers1

0

U are not allowed to use networking in main UI thread from android 3.0 and up.

So setup aSyncTask to solve your problem

  • Lol see my code carefully. I am accessing the inbox in a different thread. In android 3.0 up, if you try to access network, your app would simply crash. – rahulserver Mar 24 '14 at 15:58