2015-06-23 33 views
0

我最近開始了我的大學第一個應用程序,作爲我的應用程序的一部分,我需要做一個自定義的電子郵件客戶端。這意味着有一個登錄活動,用戶可以在其中輸入他/她的Gmail地址和密碼以連接到該帳戶。 (安全風險在這裏並不重要,這個應用程序不會上傳到商店,只有「內部」使用)。在android中製作自定義電子郵件客戶端

連接完成後,我們將進入下一個活動,用戶可以選擇發送新電子郵件,獲取收件箱消息或獲取發送的消息(後續3個活動)。所有這些功能都提供有關如何執行這些操作的指南(在stackoverflow,tutorialspoint等,雖然其中很多人不是專門針對android或不使用AsyncTask),我知道我檢查了很多,但所有這些都假設我們需要這些來自活動的地址和密碼。我的第一個目標是隻連接到用戶的帳戶,而我似乎無法實現它。我做了這個至今:

Email_LoginActivity.java

public boolean isOnline() { 
    ConnectivityManager cm = 
      (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    return netInfo != null && netInfo.isConnected(); 
} 


public void connectToTheServer() { 
    if (isOnline()) { 
     if (usernameInput.getText().toString().isEmpty() || passwordInput.getText().toString().isEmpty()) 
      Toast.makeText(this, "Enter both username and password!", Toast.LENGTH_LONG).show(); 
     else { 

      class LoginToEmailAccount extends AsyncTask<String, Integer, String> { 
//I made this class here so I can get a dialogbox to the activity until the user waits. 
       ProgressDialog progress; 
       private Folder inbox; 
       private Folder sent; 
       public static final String INBOX_STRING = "INBOX"; 
       public static final String SENT_ITEMS_STRING = "MMS_Sent"; 

       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        progress = ProgressDialog.show(Email_LoginActivity.this, null, "Login in progress, please wait.", true); 
       } 

       @Override 
       protected String doInBackground(String... args) { 

        final String username = args[0]; 
        final String password = args[1]; 

        String host = "pop.gmail.com"; 

        Properties properties = new Properties(); 
        properties.put("mail.pop3s.host", host); 
        properties.put("mail.pop3s.port", "995"); 
        properties.put("mail.pop3s.starttls.enable", "true"); 

        // Setup authentication, get session 
        Session emailSession = Session.getInstance(properties, 
          new javax.mail.Authenticator() { 
           protected PasswordAuthentication getPasswordAuthentication() { 
            return new PasswordAuthentication(
              username, password); 
           } 
          }); 

        try { 

         // create the POP3 store object and connect with the pop server 
         Store store = emailSession.getStore("pop3s"); 
         store.connect(); 

         //should i create the folders here too? 
         inbox = store.getFolder(INBOX_STRING); 
         sent = store.getFolder(SENT_ITEMS_STRING); 

         //create 
         inbox.create(Folder.HOLDS_MESSAGES); 
         sent.create(Folder.HOLDS_MESSAGES); 


         // open folders 
         inbox.open(Folder.READ_WRITE); 
         sent.open(Folder.READ_WRITE); 

         if(store.isConnected()){ 
          Log.i("mytag", "connected"); 
         } 
         else{ 
          Log.i("mytag", "not connected)"); 
         } 

        } catch (NoSuchProviderException e) { 
         e.printStackTrace(); 
        } catch (MessagingException e) { 
         e.printStackTrace(); 
         Log.i("mytag", "sad times"); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 


        return "Login complete"; 

       } 

       @Override 
       protected void onPostExecute(String result) { 
        super.onPostExecute(result); 
        progress.dismiss(); 
       } 


      } 
      new LoginToEmailAccount().execute(usernameInput.getText().toString(), passwordInput.getText().toString()); 
     } 

    } else { 
     Toast.makeText(this, "Please make sure you have internet connection!", Toast.LENGTH_LONG).show(); 
    } 
} 

public void startEmailMainActivity(View view) { 
    connectToTheServer(); //here we need a condition to check if the connection was successful before calling the next activity 
    Intent intent = new Intent(this, Email_MainActivity.class); //probably gonna need some extra intent info too 
    startActivity(intent); 
} 

(usernameInput和passwordInput是2的EditText圖,其中用戶發出信息給我們,在申報的onCreate)

AndroidManifest.xml有這些

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

我檢查過在調試模式下,我得到一個MessagingException後sent.open(Folder.READ_WRITE);線。我的主要想法是按照this guide只是連接到該帳戶。順便說一句,我測試了我的this guide在AsyncTask的幫助下工作,它只是有同樣的問題,我想要發送電子郵件部分在不同的活動,我不必再次提供用戶名和密碼。

我應該做些什麼不同?對不起,如果問題太廣泛,任何幫助,不勝感激。

+0

'他/她的Gmail地址'。你的意思是'電子郵件地址'? – greenapps

+0

@greenapps是的。 –

回答

0

請確保在帳戶設置中啓用了POP3訪問。 登錄到您的帳戶並啓用POP3。 您還需要在Gmail設置中啓用「安全性較低的應用程序」(第三方應用程序)。

+0

默認情況下啓用了POP3,當我在此之前一週測試另一個應用時,我啓用了安全性較低的應用。我雖然更新了我的帖子,但在sent.open(Folder.READ_WRITE)後得到異常; –

+0

發佈您的例外日誌 – Emil

+0

我現在有這個: } catch(MessagingException e){ e.printStackTrace(); e.getMessage();在調試中,即使應用程序在e.getMessage()處凍結,「悲傷時間」日誌也會顯示出來(我在我之後也得到了「連接」日誌在store.connect行之後移動它) –

相關問題