2012-04-02 72 views
36

我想將.vcf文件與我的郵件附加在一起並通過郵件發送。但郵件收到的地址沒有附件。我已經使用了下面的代碼,但代碼爲此,我不知道我在哪裏錯了。如何用Android中的文件附件發送電子郵件

try {  
    String filelocation="/mnt/sdcard/contacts_sid.vcf";  
    Intent intent = new Intent(Intent.ACTION_SENDTO);  
    intent.setType("text/plain");  
    intent.putExtra(Intent.EXTRA_SUBJECT, "");  
    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filelocation));  
    intent.putExtra(Intent.EXTRA_TEXT, message);   
    intent.setData(Uri.parse("mailto:"));   
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    activity.startActivity(intent); 
    activity.finish(); 
    } catch(Exception e) { 
    System.out.println("is exception raises during sending mail"+e); 
} 

回答

69

使用下面的代碼來發送郵件

String filename="contacts_sid.vcf"; 
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename); 
Uri path = Uri.fromFile(filelocation); 
Intent emailIntent = new Intent(Intent.ACTION_SEND); 
// set the type to 'email' 
emailIntent .setType("vnd.android.cursor.dir/email"); 
String to[] = {"[email protected]"}; 
emailIntent .putExtra(Intent.EXTRA_EMAIL, to); 
// the attachment 
emailIntent .putExtra(Intent.EXTRA_STREAM, path); 
// the mail subject 
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
startActivity(Intent.createChooser(emailIntent , "Send email...")); 
+0

一看我的問題... HTTP://stackoverflow.com/questions/12798001/android-how-to-send-multiple-contacts-are-attached-in-single-vcf-file-併發送 – NagarjunaReddy 2012-10-11 06:37:32

+3

您不應使用「硬編碼」路徑,因爲它們可能會因設備而異。我建議你改變文件位置的定義爲: File filelocation = new File(Environment.getExternalStorageDirectory()。getAbsolutePath(),filename); 然後定義: Uri path = Uri.fromFile(filelocation);並把它放在你的意圖中: emailIntent .putExtra(Intent.EXTRA_STREAM,path); – 2015-11-24 07:52:44

+1

phillip工作正常,emailIntent.putExtra(Intent.EXTRA_STREAM,filelocation)不會爲我附加文件,但使用emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(「file://」+ filelocation))。 – andytrombone 2016-01-10 01:18:28

4

官方Android site爲我工作的例子。 所有什麼需要添加

startActivity(Intent.createChooser(emailIntent , "Send email...")); 

在阿加瓦爾的答案

+0

在我的情況下,它的郵件客戶端,但沒有附件。顯示的吐司是「無法發送空文件」。我的文件存儲在'/ data/data/com.example.app/files/temp.txt'中,我使用'emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(「content:/」+ filePath )); // filePath是/data/com.example.app/files/temp.txt ' – kAmol 2015-09-13 18:00:07

+0

您無法發送文件,因爲它位於應用的緩存目錄中,只有您的應用可以從該目錄讀取。您應該使用另一個目錄,例如Environment.getExternalStorageDirectory()。 – Borzh 2015-09-28 18:17:46

+0

使用Environment.getExternalStorageDirectory(),驗證路徑是有效的,該文件具有良好的數據....但仍然得到相同的錯誤消息(?)。 – CESDewar 2016-03-16 19:14:10

4

文件夾名所做的就是在手機的內部存儲文件的名稱。 (ACTUALLY EXTERNAL_STORAGE)。 file_name是您要發送的文件的名稱。

private void ShareViaEmail(String folder_name, String file_name) { 
    try { 
     File Root= Environment.getExternalStorageDirectory(); 
     String filelocation=Root.getAbsolutePath() + folder_name + "/" + file_name; 
     Intent intent = new Intent(Intent.ACTION_SENDTO); 
     intent.setType("text/plain"); 
     String message="File to be shared is " + file_name + "."; 
     intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
     intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filelocation)); 
     intent.putExtra(Intent.EXTRA_TEXT, message); 
     intent.setData(Uri.parse("mailto:[email protected]")); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

     startActivity(intent); 
    } catch(Exception e) { 
     System.out.println("is exception raises during sending mail"+e); 
    } 
} 
1

SENDTO不支持附件。我已經使用Provider添加了我的答案以讀取文件信息。它在Kotlin。

fun shareFile(context: Context, filePath: File?, fileShareInfo: FileShareInfo) { 

    val intentFileShare = Intent(Intent.ACTION_SEND) 

    if (filePath!!.exists()) { 
     intentFileShare.type = fileShareInfo.fileType 
     val uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", filePath) 
     intentFileShare.putExtra(Intent.EXTRA_STREAM, uri) 
     fileShareInfo.recipients?.let { 
      intentFileShare.putExtra(Intent.EXTRA_EMAIL, fileShareInfo.recipients) 
     } 
     intentFileShare.putExtra(Intent.EXTRA_SUBJECT, fileShareInfo.shareSubjectText) 
     fileShareInfo.shareExtraText?.let { 
      intentFileShare.putExtra(Intent.EXTRA_TEXT, AppViewUtil.fromHtml(fileShareInfo.shareExtraText!!)) 
     } 
     try { 
      ContextCompat.startActivity(context, Intent.createChooser(intentFileShare, fileShareInfo.shareTitle), null) 
     } catch (e: ActivityNotFoundException) { 
      Toast.makeText(context, context.getString(R.string.sharing_no_app_found), Toast.LENGTH_LONG).show() 
     } 

    } 
}