如何使用电子邮件客户端发送电子邮件?

时间:2017-03-01 14:05:19

标签: android

我想 -

  1. 获取我自己的主电子邮件

  2. 将主电子邮件设置为收件人地址,以便我发送电子邮件     对我自己

  3. 这就是我收到主电子邮件的方式 -

    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
                Account[] accounts = AccountManager.get(getBaseContext())
                        .getAccounts();
                for (Account account : accounts) {
                    if (emailPattern.matcher(account.name).matches()) {
                        String possibleEmail = account.name;
                        Toast.makeText(this, possibleEmail, Toast.LENGTH_LONG)
                                .show();
                    }
                }
    

    这就是我发送电子邮件的方式 -

    Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("message/rfc822");
                i.putExtra(Intent.EXTRA_EMAIL,
                        new String[] { "recipient@example.com" });
                i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
                i.putExtra(Intent.EXTRA_TEXT, "body of email");
                try {
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(Main.this,
                            "There are no email clients installed.",
                            Toast.LENGTH_SHORT).show();
                }
    

    如何提供EXTRA_EMAIL我的主电子邮件?

1 个答案:

答案 0 :(得分:0)

解决方案:

    String possibleEmail = null;
    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
    Account[] accounts = AccountManager.get(getBaseContext())
            .getAccounts();
    for (Account account : accounts) {
        if (emailPattern.matcher(account.name).matches()) {
            possibleEmail = account.name;
            Toast.makeText(this, possibleEmail, Toast.LENGTH_LONG)
                    .show();
        }
    }

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