How to send an email with a file attachment in And

2019-01-03 09:55发布

I want to attach .vcf file with my mail and send through the mail. But the mail is received on the address without the attachment.I have used the below code but the code for this and i don't know where i am wrong.

try {      
  String filelocation="/mnt/sdcard/contacts_sid.vcf";      
  Intent intent = new Intent(Intent.ACTION_SENDTO);    
  intent.setType("text/plain");      
  intent.putExtra(Intent.EXTRA_SUBJECT, "");      
  intent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+filelocation));      
  intent.putExtra(Intent.EXTRA_TEXT, message);         
  intent.setData(Uri.parse("mailto:"));         
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

  activity.startActivity(intent);
  activity.finish();
  } catch(Exception e)  {
     System.out.println("is exception raises during sending mail"+e);
}

4条回答
不美不萌又怎样
2楼-- · 2019-01-03 10:00

Use the below code to sent a mail

String filename="contacts_sid.vcf"; 
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation); 
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd@gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
查看更多
放我归山
3楼-- · 2019-01-03 10:04

Folder_name is the name of file in your Internal Storage of your phone. (ACTUALLY EXTERNAL_STORAGE). file_name is the name of the file you want to send.

private void ShareViaEmail(String folder_name, String file_name) {
    try {
        File Root= Environment.getExternalStorageDirectory();
        String filelocation=Root.getAbsolutePath() + folder_name + "/" + file_name;
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setType("text/plain");
        String message="File to be shared is " + file_name + ".";
        intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+filelocation));
        intent.putExtra(Intent.EXTRA_TEXT, message);
        intent.setData(Uri.parse("mailto:xyz@gmail.com"));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        startActivity(intent);
    } catch(Exception e)  {
        System.out.println("is exception raises during sending mail"+e);
    }
}
查看更多
我命由我不由天
4楼-- · 2019-01-03 10:06

The example on the official Android site worked for me. All what is need it to add the

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

as done in Agarwal's answer

查看更多
男人必须洒脱
5楼-- · 2019-01-03 10:19

SENDTO doesnt support attachment. I have added my answer using Provider to read the file information. Its in Kotlin.

fun shareFile(context: Context, filePath: File?, fileShareInfo: FileShareInfo) {

    val intentFileShare = Intent(Intent.ACTION_SEND)

    if (filePath!!.exists()) {
        intentFileShare.type = fileShareInfo.fileType
        val uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", filePath)
        intentFileShare.putExtra(Intent.EXTRA_STREAM, uri)
        fileShareInfo.recipients?.let {
            intentFileShare.putExtra(Intent.EXTRA_EMAIL, fileShareInfo.recipients)
        }
        intentFileShare.putExtra(Intent.EXTRA_SUBJECT, fileShareInfo.shareSubjectText)
        fileShareInfo.shareExtraText?.let {
            intentFileShare.putExtra(Intent.EXTRA_TEXT, AppViewUtil.fromHtml(fileShareInfo.shareExtraText!!))
        }
        try {
            ContextCompat.startActivity(context, Intent.createChooser(intentFileShare, fileShareInfo.shareTitle), null)
        } catch (e: ActivityNotFoundException) {
            Toast.makeText(context, context.getString(R.string.sharing_no_app_found), Toast.LENGTH_LONG).show()
        }

    }
}
查看更多
登录 后发表回答