如何在Android中发送带有文件附件的电子邮件

我想用我的邮件附加.vcf文件并通过邮件发送。 但邮件收到的地址没有附件。我已经使用了下面的代码,但代码为此,我不知道我在哪里错了。

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); } 

使用下面的代码发送邮件

 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...")); 

Android官方网站上的例子为我工作。 所有需要它添加的

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

正如Agarwal的回答所做的那样

Folder_name是手机内部存储器中文件的名称。 (实际的EXTERNAL_STORAGE)。 file_name是要发送的文件的名称。

 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); } }