1. 程式人生 > >Android 呼叫系統郵箱傳送郵件

Android 呼叫系統郵箱傳送郵件

做需求遇到了需要調起系統郵箱並且顯示特定標題 因此去搜索了 N 多資料查詢怎麼實現

val uri = Uri.parse(activity.getString(R.string.tip_email)) val email = arrayOf(activity.getString(R.string.tip_email)) // 需要注意,email必須以陣列形式傳入 val intent = Intent(Intent.ACTION_SENDTO, uri) intent.type = “message/rfc822” // 設定郵件格式 intent.putExtra(Intent.EXTRA_EMAIL, email) // 接收人 intent.putExtra(Intent.EXTRA_SUBJECT, activity.getString(R.string.tip_email_title)) // 主題 activity.startActivity(Intent.createChooser(intent, “Please choose the email”)) 這個是晚上最多的一種解決方案,使用 Intent.ACTION_SENDTO 實現調起郵箱,實現方式是隻會調起來 郵箱類應用,如果你使用Intent.ACTION_SEND 則 會調起許多無關應用。

使用上述程式碼之後,發現一直無法調起系統郵箱(小米測試機+Gmail 郵箱+Mail 郵箱+163郵箱+QQ 郵箱),都掉不起來。。。。。。。。。。。。。。。。。 內心是絕望的 、

如果您想確保 Intent 只由電子郵件應用(而非其他簡訊或社交應用)進行處理,則需使用 ACTION_SENDTO 操作並加入 “mailto:” 資料架構

public void composeEmail(String[] addresses, String subject) { Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse(“mailto:”)); // only email apps should handle this intent.putExtra(Intent.EXTRA_EMAIL, addresses); intent.putExtra(Intent.EXTRA_SUBJECT, subject); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }

以此記錄,希望後續不要繼續採坑了。。。