Send text file attachment with an email

徘徊边缘 提交于 2020-01-04 04:31:09

问题


I am trying to attach a text file in order to send it with an email but whenever i open the Email app it says that the file does not exist Help Please

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"musabyahya1005@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse(directory+"/data.txt"));

try {
     startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
     Toast.makeText(getBaseContext(), "An Error Happened ", Toast.LENGTH_SHORT).show();
}

回答1:


If you are trying to send files with email, make sure that they are on a public accessible location and that you tell it that it is a file you want to send. Use this code as an example.

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("message/rfc822").putExtra(Intent.EXTRA_EMAIL, new String[]{"fake@example.com"}).putExtra(android.content.Intent.EXTRA_SUBJECT, "Mail subject").putExtra(android.content.Intent.EXTRA_TEXT, "lalalala");
String targetFilePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "tmp" + File.separator + "test.txt";
Uri attachmentUri = Uri.parse(targetFilePath);
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file://" + attachmentUri)); 


来源:https://stackoverflow.com/questions/30016888/send-text-file-attachment-with-an-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!