在Android开发中无法发送电子邮件

时间:2018-12-12 08:32:11

标签: android email-integration

我正尝试在android开发中发送电子邮件,当程序启动时,没有传入的电子邮件

这是我的代码

public void NoticationEmail() {

    SenderEmail = "xxx@gmail.com";
    Subject = "Announcement";
    Message = "Test Email";

    Intent email = new Intent(Intent.ACTION_SEND);
    email.putExtra(Intent.EXTRA_EMAIL, new String[]{SenderEmail});
    email.putExtra(Intent.EXTRA_SUBJECT, Subject);
    email.putExtra(Intent.EXTRA_TEXT, Message);
    email.setType("message/rfc822");
    startActivity(Intent.createChooser(email, "Choose Email client :"));
    Intent pindah = new Intent(verification.this, AdminActivity.class);
    startActivity(pindah);
}

请帮助我

2 个答案:

答案 0 :(得分:0)

只需在电子邮件中添加“ email” |即可。单击按钮即可使用此代码。然后您会找到解决方法

Uri uri = Uri.parse("mailto:" + email);
Intent myActivity2 = new Intent(Intent.ACTION_SENDTO, uri);
myActivity2.putExtra(Intent.EXTRA_SUBJECT, "");
startActivity(myActivity2);

答案 1 :(得分:0)

尝试使用以下方法通过邮件应用发送电子邮件

public void composeEmail(String[] addresses, String subject) {
    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:")); // only email apps should handle this
    intent.putExtra(Intent.EXTRA_EMAIL, addresses);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}