2017-06-07 13 views
-3

爲什麼每當我試圖運行它時,崩潰我的android應用程序/將電子郵件數據導入android應用程序的最佳方式是什麼?如何在Android Studio中獲取電子郵件數據?

該應用程序本身只包含一個按鈕點擊 - 我已經得到了下面的代碼在eclipse中工作,我試圖運行它在手機上,而不是一個模擬器,所以我不再收到網絡錯誤。

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

     } 
    } 
} 

Here is the logcat

+1

添加logcat的。 – Jens

+0

確實會添加LogCat的輸出,這會顯示出錯。我不知道這是否由javax.mail包處理,但我的第一個猜測是,您嘗試在主線程上執行與網絡相關的事情,這在Android上是不允許的。 嘗試將其包裝到一個AsyncTask中,看看是否有效。 – JaapM

+0

已添加logcat –

回答

1

你的一個Log.i電話都是空的消息。 看看你的代碼,這可能會在catch塊之一。

嘗試登錄錯誤是這樣的:

Log.i("MainActivity1", "Error: " + e.getMessage()); 
相關問題