Android发送邮件 分类: Android开发 2014-05-30 10:57 63人阅读 评论(0) 收藏
原文地址:Android发送邮件作者:rain
一:最基本的发送邮件
intent.putExtra("subject", file.getName());
//
intent.putExtra("body", "android123 - email
sender"); //正文
intent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(file)); //添加附件,附件为file对象
if
(file.getName().endsWith(".gz")) {
intent.setType("application/x-gzip"); //如果是gz使用gzip的mime
} else if
(file.getName().endsWith(".txt")) {
intent.setType("text/plain");
//纯文本则用text/plain的mime
} else
{
intent.setType("application/octet-stream");
//其他的均使用流当做二进制数据来发送
}
startActivity(intent);
//调用系统的mail客户端进行发送
Intent data= new Intent(Intent.ACTION_SENDTO);
data.setData(Uri.parse("mailto:1213244340@qq.com"));
data.putExtra(Intent.EXTRA_SUBJECT, "这是标题");
data.putExtra(Intent.EXTRA_TEXT, "这是内容");
startActivity(data);
二:发送给多人以及抄送和密送都很简单,查找Intent类中的Extra常量,发送有这么三个常量:Intent.EXTRA_EMAIL,Intent.EXTRA_CC,Intent.EXTRA_BCC。这三个分别用于传递“接受人地址列表”、“抄送人地址列表”和“密送人地址列表”,传递的都是String[]类型的Email地址,如果数组中有多个地址,就可以发送给多人
Intent data=new Intent(Intent.ACTION_SENDTO);
data.setData(Uri.parse("mailto:1213244340@qq.com"));
data.putExtra(Intent.EXTRA_EMAIL, new String[]{ "222@qq.com","333@qq.com"});
data.putExtra(Intent.EXTRA_CC, new String[]{ "111@qq.com"});
data.putExtra(Intent.EXTRA_BCC, new String[]{ "111@qq.com"});
data.putExtra(Intent.EXTRA_SUBJECT, "这是标题");
data.putExtra(Intent.EXTRA_TEXT, "这是内容");
startActivity(data);
三:发送带附件的邮件
//只启动邮件发送方式
Uri uri =
Uri.parse("mailto:");
Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra(Intent.EXTRA_SUBJECT, "邮件标题");
intent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file:///"+Environment.getExternalStorageDirectory()+
File.separator + "附件目录"));
startActivity(intent);
//启动多种发送方式(可以选择邮件方式)
File file = new File("sdcardandroid123.cwj"); //附件文件地址
Intent intent = new Intent(Intent.ACTION_SEND);