2011-03-16 52 views
6

我試圖發送帶有多個附件的電子郵件。發送帶有多個附件的電子郵件

Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE); 
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]", "[email protected]"}); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "The Subject"); 
emailIntent.putExtra(Intent.EXTRA_TEXT, "The Text"); 
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 
emailIntent.setType("text/plain"); 
startActivity(Intent.createChooser(emailIntent, "Send Email Using: ")); 

這個偉大的工程,當我使用Gmail發送電子郵件,但它並沒有附加附件,如果我發送郵件使用電子郵件客戶端的電子郵件上的Nexus One。它包含所有的文本,主題等等,但沒有附件。我的電子郵件帳戶是一個交易帳戶,如果重要...

回答

12

試試這個工作很好。

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE); 
emailIntent.setType("plain/text"); 

ArrayList<Uri> uris = new ArrayList<Uri>(); 

String[] filePaths = new String[] {image1 Path,image2 path}; 
for (String file : filePaths) { 
    File fileIn = new File(file); 
    Uri u = Uri.fromFile(fileIn); 
    uris.add(u); 
} 

if (!(app_preferences.getString("email", "") == null || app_preferences.getString("email", "").equals(""))) { 
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {app_preferences.getString("email", "")});  
} 

emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject name"); 
emailIntent.putExtra(Intent.EXTRA_TEXT, "Please find the attachment."); 
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 

startActivity(Intent.createChooser(emailIntent, "Email:")); 
+6

+2 android.content.Intent.ACTION_SEND_MULTIPLE這工作正常... thanx –

+0

它的工作原理..謝謝 –

+1

如果你沒有傳遞一個新的String [] {}到Intent.EXTRA_EMAIL它只會離開收件人字段空... –

0

試過這一切一百萬次 - 得到它的工作,但有一個討厭的警告。發現它是一個Android錯誤。有一個修復&更多的信息在這裏:

https://code.google.com/p/android/issues/detail?id=38303 

Error: ClassCastException warning in log when opening e-mail app with a body and multiple file attachments. 

更新:找到解決方法。取而代之的

sendIntent.putExtra(Intent.EXTRA_TEXT, "See attached CSV files."); 

把文字作爲ArrayList

ArrayList<String> extra_text = new ArrayList<String>(); 
extra_text.add("See attached CSV files."); 
sendIntent.putStringArrayListExtra(Intent.EXTRA_TEXT, extra_text); 

瞧!沒有更多的例外,EXTRA_TEXT最終成爲電子郵件的主體。

編輯:我想簡單地註釋掉這條線擺脫了錯誤 - 但是然後你不會輸入任何信息的身體。在我的情況下,這很好,但因爲我只給電子郵件發送日誌文件。刪除此行以擺脫以下警告:'sendIntent.putExtra(Intent.EXTRA_TEXT,「請參閱附加的CSV文件。」);'

+0

如果emailIntent.setType(「text/html」),您的解決方案對HTML正文有效。 – Nizzy

相關問題