title: how do you get email data in Android Studio?
body: <p>Why does this crash my android app whenever I attempt to run it / what is the best way to get email data into an android app? </p>
  
  <p>The app itself only consists of a button to click - I have gotten the below code to work in eclipse and I am attempting to run it on a mobile phone and not an emulator so I am no longer getting a network error.</p>
  
  <pre><code>package com.example.marc.emailtest;
  
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  
  import java.util.Properties;
  
  import javax.mail.Address;
  import javax.mail.Folder;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.NoSuchProviderException;
  import javax.mail.Session;
  import javax.mail.Store;
  
  import android.util.Log;
  
  public class MainActivity extends AppCompatActivity {
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      public void buttonOnClick(View v){
  
  
  
          String host = "pop.gmail.com";// change accordingly
          String StoreType = "pop3";
          String user = "******@gmail.com";// I purposefully hid these
          String password = "********";// I purposefully hid these
  
          try {
              //create properties field
              Properties properties = new Properties();
  
              properties.put("mail.pop3.host", host);
              properties.put("mail.pop3.port", "995");
              properties.put("mail.pop3.starttls.enable", "true");
              Session emailSession = Session.getDefaultInstance(properties);
  
              //create the POP3 store object and connect with the pop server
              Store store = emailSession.getStore("pop3s");
  
              store.connect(host, user, password);
  
              //create the folder object and open it
              Folder emailFolder = store.getFolder("INBOX");
              emailFolder.open(Folder.READ_ONLY);
  
              // retrieve the messages from the folder in an array and print it
              Message[] messages = emailFolder.getMessages();
              System.out.println("messages.length---" + messages.length);
  
              for (int i = 0, n = messages.length; i &lt; n; i++) {
                  Message message = messages[i];
                  Log.i("MainActivity1", "---------------------------------");
                  Log.i("MainActivity1", "Email Number " + (i + 1));
                  Log.i("MainActivity1", "Subject: " + message.getSubject());
                  Log.i("MainActivity1", "From: " + message.getFrom()[0]);
                  Log.i("MainActivity1", "Text: " + message.getContent().toString());
  
              }
  
              //close the store and folder objects
              emailFolder.close(false);
              store.close();
  
          } catch (NoSuchProviderException e) {
              e.printStackTrace();
              Log.i("MainActivity1", e.getMessage());
          } catch (MessagingException e) {
              e.printStackTrace();
              Log.i("MainActivity1", e.getMessage());
  
          } catch (Exception e) {
              e.printStackTrace();
              Log.i("MainActivity1", e.getMessage());
  
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/oW1Ks.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oW1Ks.png" alt="Here is the logcat"></a></p>