2013-07-16 28 views
1

我正在將郵件發送給朋友,但下面的代碼只發布一次。之後,我無法再發送郵件。請指導我。無法在Android中發送郵件

package com.mkyong.android; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.Toast; 

    public class SendEmailActivity extends Activity { 

     Button buttonSend; 

     ConnectionDetector cd; 
     Boolean isInternetPresent = false; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      buttonSend = (Button) findViewById(R.id.buttonSend); 

      buttonSend.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        cd = new ConnectionDetector(getApplicationContext()); 

        isInternetPresent = cd.isConnectingToInternet(); 
        if (isInternetPresent) { 
         new MyTask().execute(); 
        } else { 
         Toast.makeText(getApplicationContext(), "Not Enabled", 
           Toast.LENGTH_LONG).show(); 
        } 
       } 
      }); 
     } 

     class MyTask extends AsyncTask<String, String, String> { 

      @Override 
      protected String doInBackground(String... params) { 
       Intent i = new Intent(Intent.ACTION_SEND); 

       i.putExtra(android.content.Intent.EXTRA_EMAIL, 
         new String[] { "[email protected]" }); 
       i.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject of email"); 
       i.putExtra(android.content.Intent.EXTRA_TEXT, "body of email"); 
       i.setType("message/rfc822"); 
       try { 
        startActivity(Intent.createChooser(i, "Send mail...")); 
       } catch (android.content.ActivityNotFoundException ex) { 
        Toast.makeText(getApplicationContext(), 
          "There are no email clients installed.", 
          Toast.LENGTH_SHORT).show(); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(String result) { 

      } 
     } 
    } 
+0

PLS顯示有什麼錯誤代碼 – Riandy

+0

07-16 11:47:17.890:W/IInputConnectionWrapper(4384):getExtractedText非活動InputConnection 07-16 11:47: 17.895:W/IInputConnectionWrapper(4384):getTextBeforeCursor on inactive InputConnection 07-16 11:47:17.895:W/IInputConnectionWrapper(4384):getTextAfterCursor on inactive InputConnection 07-16 11:47:17.895:W/IInputConnectionWrapper(4384) :getExtractedText on inactive InputConnection 07-16 11:47:17.895:W/IInputConnectionWrapper(4384):beginBatchEdit on inactive InputConnection 07-16 11:47:17.900:W/IInputConnection包裝(4384):endBatchEdit on inactive InputConnection – Giridharan

+0

我的問題,我發送郵件三次在我的朋友的帳戶,但之後,我無法發送郵件.. – Giridharan

回答

0

你不需要啓動的AsyncTask火的意圖,速戰速決,可能是粘貼您在preExecute或postExecute doInBackground代碼。

讓我知道,如果它的工作原理:)

+0

它沒有工作shekhar – Giridharan

+0

它顯示任何錯誤日誌?任何異常?因爲這個片段應該在任何情況下工作 –

+0

該代碼現在工作..但問題是,當我從android 2.2版本發送時,我正在收到郵件,當我用更高的版本,如4.0.3 ..獲取延遲的郵件..不要問題在哪裏? – Giridharan