blue dream
blue dream

Reputation: 101

Android email intent not attaching files as attachment

For my company i am trying to send email from my android app using email intent. I'm using emulator to test my app. But the problem is when i'm trying to Add and Attachment (eg. pdf, image) it won't attaching yet. here is my code:

      private String SendEmail(String oid, final String img, final String party_code, final String order_by, final Bitmap attachimg, final String note) {
        try {
       String filename="DOAttachment.jpeg";
            String subject ="Order "+oid+" has been sent successfully";
            String body="\nDear Sir, \n"+"Please find the attached file herewith.\nThe D.O for the customer(party) "+party_code+" has been successfully done with the order number: "+oid+"\n\n\n"+"With regards \n \n Employee code/ID: "+order_by+"\n\nN.B:"+note;

 File root = Environment.getExternalStorageDirectory();
            String pathToMyAttachedFile = "DirName/"+filename;
            File file = new File(root, pathToMyAttachedFile);
            file.setReadable(true,false);
            Log.e("File path"," "+file);    
            //using outlook    
            Intent intent = new Intent(Intent.ACTION_VIEW).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP);
            intent.setType("image/*");
            Uri data = Uri.parse("mailto:?subject=" + subject + "&body=" + body+ "&stream="+Uri.parse("file:///"+Environment.getExternalStorageDirectory().getAbsolutePath())+"/DirName/"+filename);
            intent.setData(data);
            intent .putExtra(Intent.EXTRA_EMAIL, toemail);
            if (!file.exists() || !file.canRead()||!file.canWrite()) {
                Log.e(" FILE ERROR ","File Not found");
                Toast.makeText(getApplicationContext(),"File Not found",Toast.LENGTH_LONG).show();
            }
            else {
                file.setReadable(true);
                Log.e(" FILE OK ","File was found");    
                Uri uri = Uri.fromFile(file);    
                intent.putExtra(Intent.EXTRA_STREAM, uri);
            }
 ));
 if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }    
     return "TRUE";    

        } catch (Exception e) {
            Log.e("SendMail", e.getMessage(), e);
            return "MAIL NOT SENT";
        }
    }    

The result is email with empty attachment find screenshot : https://1drv.ms/i/s!AruisQQIx8MTgatuhJFmSaoArg_6Xw

Upvotes: 3

Views: 3263

Answers (1)

Tomin B Azhakathu
Tomin B Azhakathu

Reputation: 2686

Check whether you had provided READ_EXTERNAL_STORAGE permission for your application both in manifest and run-time.

Then Call the below method send mail assuming the full file path is saved in filePath.

  File root = Environment.getExternalStorageDirectory();
  String pathToMyAttachedFile = "DirName/"+filename;
  File filePath = new File(root, pathToMyAttachedFile)
  sendEmailAlert(filePath,subject,text);

CALLED METHOD

 private void sendEmailAlert(File fileName,String subject,String text) {

    final File file=fileName;

                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("application/octet-stream"); /* or use intent.setType("message/rfc822); */
                    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
                    intent.putExtra(Intent.EXTRA_TEXT, text);
                    if (!file.exists() || !file.canRead()) {
                        Toast.makeText(getContext(), "Attachment Error", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Uri uri = Uri.fromFile(file);
                    intent.putExtra(Intent.EXTRA_STREAM, uri);
                    startActivity(Intent.createChooser(intent, "Send email..."));

}

Upvotes: 3

Related Questions