Sending an email with an attachment from an application

怎甘沉沦 提交于 2019-12-02 09:09:42

问题


I'm creating an app that has an "export" feature which converts the user's data into a CSV file, and allows the user to send it as an attachment to somebody (presumably themselves).

The CSV file is created successfully, but when I try to send the email, I encounter a problem. The device looks like it is going to send the email with the appropriate attachment, but when the email is received... there is no attachment at all...

Here is the code I am using to send the email:

final Intent email = new Intent(android.content.Intent.ACTION_SEND);

  email.setType("text/html");
  email.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.email_subject));
  email.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(getString(R.string.email_1)));

  email.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///" + getString(R.string.csv_title)));


  startActivity(Intent.createChooser(email, "Send mail..."));

回答1:


i've done for send any file from SD card with mail attachment..

Intent sendEmail= new Intent(Intent.ACTION_SEND);
       sendEmail.setType("rar/image");
       sendEmail.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new        
         File("/mnt/sdcard/download/abc.rar")));
         startActivity(Intent.createChooser(sendEmail, "Email:"));



回答2:


need the correct path for your file, if on SD card then...

sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse 
("file://"+Environment.getExternalStorageDirectory()+getString(R.string.csv_title)"));

Look here for addition information on setting the appropriate file path

http://developer.android.com/guide/topics/data/data-storage.html#filesExternal




回答3:


GMail app accepts file:// Uris only if they are on the sdcard... and on an android 1.6 device I had even an issue with it accepting only file://sdcard/* Uris whereas the real external storage of this specific device is on another path.

Anyway, I have a real better behavior with attachments since I provide them through a ContentProvider.



来源:https://stackoverflow.com/questions/4149265/sending-an-email-with-an-attachment-from-an-application

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