Android发送邮件

来源:互联网 发布:linux终端粘贴快捷键 编辑:程序博客网 时间:2024/06/07 00:04

Android发送邮件

 (2013-04-08 09:37:05)
转载
 分类: Android
一:最基本的发送邮件
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("\sdcard\android123.cwj"); //附件文件地址
Intent intent = new Intent(Intent.ACTION_SEND);
 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客户端进行发送
0 0
原创粉丝点击