Jak wysłać wiadomość e-mail z załącznikiem do pliku w systemie Android

Chcę dołączyć .plik vcf z moją pocztą i wyślij pocztą. Ale Poczta jest odbierana na adres bez załącznika.Użyłem poniższego kodu, ale kod do tego i nie wiem, gdzie się mylę.

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);
}
Author: Phillip, 2012-04-02

4 answers

Użyj poniższego kodu, aby wysłać wiadomość

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..."));
 73
Author: Shankar Agarwal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-25 02:31:43

Folder_name to nazwa pliku w pamięci wewnętrznej telefonu. (WŁAŚCIWIE EXTERNAL_STORAGE). file_name to nazwa pliku, który chcesz wysłać.

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);
    }
}
 7
Author: Kshitij,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-03 19:40:18

Przykład na oficjalnej stronie Android zadziałał dla mnie. Wszystko, co jest potrzebne, aby dodać

startActivity(Intent.createChooser(emailIntent , "Send email..."));
Tak jak w odpowiedzi Agarwala]}
 4
Author: hamish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-18 08:09:09

SENDTO nie obsługuje załączników. Dodałem odpowiedź za pomocą Provider, aby odczytać informacje o pliku. Znajduje się w Kotlinie.

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()
        }

    }
}
 1
Author: Deep P,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-01 23:56:12