Search code examples
androidemailoutlookattachmentprovider

android studio sent pdf via email Outlook


I have a big problem. I hope you can help me. when i send pdf via outlook ,an error occurs -> unable to add Attach

when i send pdf via Samsung-Email, it works. but i need to send pdf via Outlook.

Manifest:

 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

file_paths:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="external" path="." />
    <external-files-path name="my_images" path="." />
    <external-files-path name="external_files" path="." />
    <external-files-path name="cache" path="." />
    <external-files-path name="files" path="." />
    <external-files-path name="root" path="." />
</paths>

MY CODE:

 private String Speichern_Path="/ABO/Dokumente/Reparaturbericht/";

            String emailText ="";
            String emailBetreff="";

            //String directory_path=Environment.getExternalStorageDirectory()+Speichern_Path+Pdfname;

            emailBetreff ="Reparaturbericht";
            emailText = "Hallo";

            Toasty.info(context,"Email wird geöffnet",Toast.LENGTH_SHORT).show();

            Intent i = new Intent(Intent.ACTION_SEND);
            //i.setType("*/*");
            i.setType("application/pdf");

            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{Reparatur_Email});
            i.putExtra(Intent.EXTRA_SUBJECT, emailBetreff);
            i.putExtra(Intent.EXTRA_TEXT   , emailText);


            File root = Environment.getExternalStorageDirectory();
            String pathToMyAttachedFile = Speichern_Path+ Pdfname;
            File file = new File(root, pathToMyAttachedFile );
            if (!file.exists() || !file.canRead()) {
                return;
            }
           Uri uri = Uri.fromFile(file);
           //Uri uri = FileProvider.getUriForFile(context, "com.example.android.fileprovider",file);
            i.putExtra(Intent.EXTRA_STREAM, uri);



            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            try {
               
                startActivity(Intent.createChooser(i, "Email senden"));
                
            }
            catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
            }


I changed the code but it doesn't work too

   String emailText ="";
            String emailBetreff="";

            String directory_path = Environment.getExternalStorageDirectory() + Speichern_Path+Pdfname;

            emailBetreff ="Reparaturbericht";
            emailText = "Hallo,"
                    +"\n\nanbei schicke ich euch den Reparaturbericht."
                    +"\n\nViele Grüße\n"
                    +Reparatur_User
                    +"\n\nDiese Email wurde automatisch erstellt.\n\n"
                    +App_name;

            Toasty.info(context,"Email wird geöffnet",Toast.LENGTH_SHORT).show();

            Intent i = new Intent(Intent.ACTION_SEND);
            //i.setType("*/*");
            i.setType("application/pdf");
           

            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{Reparatur_Email});
            i.putExtra(Intent.EXTRA_SUBJECT, emailBetreff);
            i.putExtra(Intent.EXTRA_TEXT   , emailText);


            File root = Environment.getExternalStorageDirectory();
            String pathToMyAttachedFile = Speichern_Path+ Pdfname;
            File file = new File(root, pathToMyAttachedFile);
            if (!file.exists() || !file.canRead()) {
                return;
            }
            //Uri uri = Uri.fromFile(file);
           Uri uri = FileProvider.getUriForFile(context, "com.example.android.fileprovider",file);
            i.putExtra(Intent.EXTRA_STREAM, uri);



            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            try {

                startActivity(Intent.createChooser(i, "Email senden"));

            }
            catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
            }

when i send pdf via Samsung-Email, it works. but i need to send pdf via Outlook.


Solution

  • I found the answer for sending email with Outlook:

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("*/*");
    
    i.putExtra(Intent.EXTRA_EMAIL, new String[]{Reparatur_Email});
    i.putExtra(Intent.EXTRA_SUBJECT, emailBetreff);
    i.putExtra(Intent.EXTRA_TEXT, emailText);
    
    File root = Environment.getExternalStorageDirectory();
    String pathToMyAttachedFile = Speichern_Path + Pdfname;
    File file = new File(root, pathToMyAttachedFile);
    
    if (!file.exists() || !file.canRead()) {
        return;
    }
    
    //Uri uri = Uri.fromFile(file);
    Uri uri = FileProvider.getUriForFile(context, "com.example.android.fileprovider",file);
    i.putExtra(Intent.EXTRA_STREAM, uri);
    
    context.grantUriPermission(getActivity().getPackageName(),uri,Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    context.revokeUriPermission(uri,Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    
    List<ResolveInfo> resolveInfos_List = context.getPackageManager().queryIntentActivities(i,PackageManager.MATCH_DEFAULT_ONLY);
    
    for (ResolveInfo resolveInfo:resolveInfos_List){
        String Package_Name = resolveInfo.activityInfo.packageName;
     
        context.grantUriPermission(Package_Name,uri,Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    }
    
    try {       
        startActivity(Intent.createChooser(i, "Email senden"));
    }
    catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
    }
    

    File_Path:

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path name="external" path="." />
        <external-files-path name="my_images" path="." />
        <external-files-path name="external_files" path="." />
        <cache-path name="cache" path="." />
        <external-cache-path
            name="external-cache"
            path="." />
        <external-files-path name="root" path="." />
        <files-path name="files" path="." />
    </paths>