android 发送带附件的邮件

来源:互联网 发布:大米营销软件 编辑:程序博客网 时间:2024/05/22 01:40

    // 附件文件地址
    String fileName = items.get(arg2).get(NAME);
    File file = new File(Environment.getExternalStorageDirectory(), fileName); 
    // 启动系统邮件
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra("subject", file.getName());
    // 正文
    intent.putExtra("body", fileName); 
    // 添加附件,附件为file对象
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
    // 如果是gz使用gzip的mime
    if (file.getName().endsWith(".gz")) {
    intent.setType("application/x-gzip"); 

    // 纯文本则用text/plain的mime
    else if (file.getName().endsWith(".txt")) {
    intent.setType("text/plain"); 
    }
    // 其他的均使用流当做二进制数据来发送
    else {
    intent.setType("application/octet-stream"); 
    }
    // 调用系统的mail客户端进行发送。
    startActivity(intent); 
原创粉丝点击