1

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

blue dream
  • 81
  • 1
  • 13
  • 5
    Possible duplicate of [Sending an email with attachments programmatically on Android](https://stackoverflow.com/questions/28809776/sending-an-email-with-attachments-programmatically-on-android) – ADM Nov 28 '17 at 05:23
  • @blue dream : Try the below code – Tomin B Azhakathu Nov 28 '17 at 05:57

1 Answers1

2

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..."));

}
Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28
  • Yes i'd provide full permission both in manifest and during Login (start Activity) and also using ActivityCompat.requestPermissions(LoginActivity.this, new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE }, 1); . Anyways Thanks for your Reply. – blue dream Nov 28 '17 at 08:58
  • Did you try my code? This Code is successfully implemented in my Project last week – Tomin B Azhakathu Nov 28 '17 at 09:00
  • I am also taking attachment image using camera intent So i think it's ok. Because I'm getting the image file in directory. Also i'm trying to use only selected email client for this so I ignore intent chooser – blue dream Nov 28 '17 at 09:02
  • Yes it's worked, But If i want to select only gmail or outlook then what should I do? – blue dream Nov 28 '17 at 09:14
  • I had added the change in the code. Check it. use `intent.setType()` – Tomin B Azhakathu Nov 28 '17 at 09:19
  • t change the type to email.setType("message/rfc822"); .. it will force to do your work. It supports mime type html,mhtml,mth,htm etc. – Tomin B Azhakathu Nov 28 '17 at 09:23
  • It's showing something like that https://1drv.ms/i/s!AruisQQIx8MTgatwgKtwa0NTnVn0Bg and let me choose those intents who also support same email type. But I need only Email intent like gmail or outlook or yahoo. – blue dream Nov 28 '17 at 09:40