无法在Android中发送邮件

时间:2013-07-16 06:10:57

标签: android gmail

我正致力于向朋友发送邮件,但以下代码仅发布一次。在那之后,我再也无法发送邮件了。请指导我。

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[] { "Xnn123@gmail.com" });
                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) {

            }
        }
    }

1 个答案:

答案 0 :(得分:0)

你不需要启动AsyncTask来激活Intent,快速修复可以在preExecute或postExecute中粘贴doInBackground代码。

让我知道它是否有效:)