如何在Android中发送带有文件附件的电子邮件 [英] How to send an email with a file attachment in Android

查看:399
本文介绍了如何在Android中发送带有文件附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

使用以下代码发送一封邮件

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

这篇关于如何在Android中发送带有文件附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆