为什么不连接到客户端?Android

时间:2016-05-18 11:53:47

标签: java android email-client

我正在尝试创建一个发送电子邮件的活动。我正在使用Intent对象" Action send"启动电子邮件客户端。

但是没有检测到电子邮件客户端,这是我第一次这样做,请帮忙。我的代码出了什么问题?

public class email extends Activity {

    private Button send;
    DBHelper mydb1;
    private ListView obj;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mydb1 = new DBHelper(this);
        setContentView(R.layout.email_display);
        ArrayList array_list = mydb1.getAllCotacts();
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, array_list);

        obj = (ListView) findViewById(R.id.listView2);
        obj.setAdapter(arrayAdapter);


            send  =(Button) findViewById(R.id.send_button);

            send.setOnClickListener(new View.OnClickListener()

                                    {
                                        @Override
                                        public void onClick(View v) {
                                            try {
                                                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                                                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"myemail@gmail.com"});
                                                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                                                emailIntent.putExtra(Intent.EXTRA_TEXT, "HEY");
                                                emailIntent.putExtra(Intent.EXTRA_CC, "example@gmail.com");
                                                emailIntent.setType("message/rfc822");
                                                startActivity(emailIntent);
                                            } catch (ActivityNotFoundException anfe) {
                                                Toast toast = Toast.makeText(email.this, "Sorry, no email client found", Toast.LENGTH_LONG);
                                                toast.show();
                                            }
                                        }
                                    }

            );
        }
    }

2 个答案:

答案 0 :(得分:0)

  

点击按钮:

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail@yahoo.com"});          
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "message");
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Choose an Email client :"));

根据您的代码更改

startActivity(emailIntent);

startActivity(Intent.createChooser(emailIntent, "Choose an Email client :"));

OR

尝试,

而是使用 ACTION_SENDTO ,提供 mailto:Uri

  intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));

这可能对你有所帮助。

答案 1 :(得分:0)

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("text/plain");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"abc@gmail.com"});
    i.putExtra(Intent.EXTRA_SUBJECT, "subject");
    i.putExtra(Intent.EXTRA_TEXT   , "body");
    i.putExtra(Intent.EXTRA_CC, new String[] { "edf@gmail.com" });
    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }

修改

尝试使用:

 i.setType("text/plain");