2010-04-29 277 views
30

我設計一個應用程序中,我需要在點擊一個按鈕打開一個電子郵件客戶端打開一個電子郵件客戶端。電子郵件客戶端應該打開一個預定義的主題和'到'地址。有沒有辦法達到這個目標?請爲我提供解決方案和代碼示例,如果可能的...上點擊一個按鈕

+0

的[如何通過意圖(但只有一個電子郵件程序)打開電子郵件程序]可能的複製(http://stackoverflow.com/questions/3312438/how-to-open-email-program-via-intents-but -only-AN-電子郵件程序) – mixel 2016-05-16 10:19:05

回答

62

是這樣的:

Intent intent = new Intent(Intent.ACTION_SEND); 
intent.setType("plain/text"); 
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" }); 
intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); 
intent.putExtra(Intent.EXTRA_TEXT, "mail body"); 
startActivity(Intent.createChooser(intent, "")); 

或者,你可以使用IntentFactory.getSendEmailIntent(String mailTo, String mailCC, String subject, CharSequence body, File attachment)

+1

當我點擊其顯示該NO APPLCATIONS可執行此操作按鈕....請告訴我溶液??? – 2010-04-29 07:01:34

+0

這隻適用於真實的設備。 – yanchenko 2010-04-29 07:24:42

+0

是否有任何其他方式在我的仿真器打開一個電子郵件客戶端.. ???? – 2010-04-29 08:21:33

0

您可以通過使用電子郵件inbuild與電子郵件配置您的電子郵件地址仿真器打開電子郵件客戶端。然後當打電話的意圖將打開併發送郵件。

9

要僅顯示電子郵件客戶端使用此代碼:

Intent intent = new Intent(Intent.ACTION_VIEW); 
Uri data = Uri.parse("mailto:[email protected]?subject=" + subject + "&body=" + body); 
intent.setData(data); 
startActivity(intent); 

如果您已經選擇了默認的電子郵件客戶端,然後它會啓動它。否則,它將顯示可用電子郵件客戶端的列表。

+0

這工作完美...我驚訝爲什麼它沒有權限的工作? – anshulkatta 2017-08-20 07:33:49

+0

@anshulkatta因爲它不執行任何需要權限的操作。它只是打開可以發送電子郵件的活動。 – mixel 2017-08-20 14:45:52