我正在尝试使用Galaxy Note 3平板电脑发送带有附件的邮件。我发送邮件的代码如下:
public static void sendMail(Context context, String rc, String subject, String message, File file, String type) {
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { rc });
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
Uri uri = Uri.fromFile(file);
email.putExtra("android.intent.extra.MIME_TYPES", type);
// email.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
email.setClassName("com.android.email", "com.android.email.activity.MessageCompose");
email.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(email);
}
文件保存到外部SD卡。如果我使用Gmail应用程序,附件已附加,我可以发送邮件。如果我使用来自android的股票电子邮件应用程序,附件没有附加,我不知道为什么。
有人知道为什么会这样吗?我发现了很多问题,如何将文件附加到电子邮件中,他们几乎都使用这种方式附加文件。那么为什么它只适用于gmail应用程序?
答案 0 :(得分:3)
之前我遇到过类似的问题,但是使用此代码,我可以发送一封带有csv文件作为附件的电子邮件,使用“myMail”作为客户端。
Uri exportFile = null;
exportFile = Uri.fromFile(file);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "testSubject");
emailIntent.putExtra(Intent.EXTRA_STREAM, exportFile);
String aEmailList[] = { "test@test.org" };
emailIntent.putExtra(Intent.EXTRA_EMAIL, aEmailList);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(
Intent.EXTRA_TEXT,
emailText
);
startActivityForResult(emailIntent, REQUESTCODE_SENTEMAIL);
答案 1 :(得分:0)
我只是想通了......你必须使用电子邮件Intent的setType()
方法来设置邮件的类型。然后,股票邮件应用程序识别附件。