2012-04-02 10 views
36

Desidero allegare file .vcf con la mia posta e inviare tramite posta. Ma la posta viene ricevuta sull'indirizzo senza l'allegato. Ho usato il codice qui sotto ma il codice per questo e non so dove sbaglio.Come inviare un messaggio di posta elettronica con un file allegato in 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); 
} 

risposta

69

utilizzare il codice qui sotto per inviare una e-mail

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

uno sguardo alla mia domanda ... http: //stackoverflow.com/questions/12798001/android-how-to-send-multiple-contacts-are-attached-in-single-vcf-file- e-send – NagarjunaReddy

+3

Non si dovrebbero usare percorsi "hardcoded" perché potrebbero cambiare a seconda del dispositivo. Ti suggerisco di cambiare la definizione di filelocation in: Filelocation file = new File (Environment.getExternalStorageDirectory(). GetAbsolutePath(), filename); Quindi definire: Uri path = Uri.fromFile (filelocation); e inseritelo nell'intenzione: emailIntent .putExtra (Intent.EXTRA_STREAM, path); –

+1

emailIntent.putExtra (Intent.EXTRA_STREAM, filelocation) non mi ha collegato il file, ma utilizzando emailIntent.putExtra (Intent.EXTRA_STREAM, Uri.parse ("file: //" + filelocation)), così come ha funzionato Phillip. L'allegato – andytrombone

4

L'esempio sul sito ufficiale Android site ha lavorato per me. Tutto ciò che è necessario per aggiungere il

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

come fatto in risposta di Agarwal

+0

Nel mio caso, è destinato al client di posta elettronica ma senza allegato. brindisi visualizzato è "non posso inviare file vuoto". il mio file è memorizzato in '/ data/data/com.example.app/files/temp.txt' e lo sto passando usando' emailIntent.putExtra (Intent.EXTRA_STREAM, Uri.parse ("content: /" + filePath)); // filePath è /data/com.example.app/files/temp.txt ' – kAmol

+0

Non è possibile inviare file perché si trova nella directory della cache dell'app e solo l'app può leggere da tale directory. Dovresti usare un'altra directory, come Environment.getExternalStorageDirectory(). – Borzh

+0

Used Environment.getExternalStorageDirectory(), verificato che il percorso era valido e che il file aveva dati buoni .... ma otteneva comunque lo stesso messaggio di errore (?). – CESDewar

4

Nome_cartella è il nome del file nel Storage interno del telefono. (ATTUALMENTE EXTERNAL_STORAGE). nome_file è il nome del file che si desidera inviare.

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 non supporta l'allegato. Ho aggiunto la mia risposta usando il provider per leggere le informazioni del file. È a 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() 
     } 

    } 
}