如何在Android系統中發送帶附件的電子郵件呢? 其實通過Intent可以很友善的發送Email,隻需要短短10行代碼就可以處理,這裡Android開發網就以在sdcard上的 android123.cwj檔案為例,通過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用戶端進行發送