簡體   English   中英

Android 僅使用電子郵件應用程序發送帶有附件的電子郵件

[英]Android send email with attachments using only email apps

官方文檔展示了如何發送帶有附件的電子郵件:

public void composeEmail(String[] addresses, String subject, Uri attachment) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("*/*");
    intent.putExtra(Intent.EXTRA_EMAIL, addresses);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_STREAM, attachment);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(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);
    }
}

但實際上,我想要上述的組合......即發送帶有附件的電子郵件僅使用電子郵件應用程序。

但是當使用intent.setData(Uri.parse("mailto:"))Intent.ACTION_SENDIntent.ACTION_SEND_MULTIPLE結合使用時,什么也沒有發生......根本沒有打開電子郵件應用程序(或應用程序選擇器)。

那么如何發送帶有附件(或多個附件)的電子郵件,同時將應用程序限制為電子郵件應用程序?

mailto: URI 字符串在沒有收件人的情況下顯得無效; 嘗試類似的方法:

intent.setData(Uri.parse("mailto:" + String.join(",", addresses)));

請參閱RFC 6068 :“mailto”URI 方案。

刪除if (intent.resolveActivity(getPackageManager()) != null)檢查,它將打開意圖。 通常,無論電子郵件處理應用程序是否存在,操作系統都會返回 null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM