1

consider the code below:

String outFileName = "/data/data/com.packagename/attachment.ics";

emailintent.putExtra(Intent.EXTRA_STREAM, Uri.parse(outFileName));
        emailintent.setType("plain/text");
        startActivity(Intent.createChooser(emailintent, "Send mail..."));

The above code is starting the email client with the attachment shown when it starts. But when i send the email, the attachment is not received. The body is being received. what is going wrong here?

thank you in advance.

EDIT: Is there a specific mime type that i need to put for ics files? i even tried sending a txt file, but that too is not being sent. The attachment does show up when i am trying to send the email, but it does not appear when i receive the email

user590849
  • 11,655
  • 27
  • 84
  • 125

5 Answers5

3

i found the problem that was occurring. I was putting the file that i want to attach to the email into a private folder inside my application. The email client was not able to access..

All i had to do was put it in a public directory on the sdcard and voila.. the email client got access and i started receiving in the mails i sent from my application.

PS: Even for ics files the MIME type is plain/text.

thanks for all your help.

user590849
  • 11,655
  • 27
  • 84
  • 125
  • How to save files in External storage : http://developer.android.com/guide/topics/data/data-storage.html#filesExternal – GuruM Jan 09 '14 at 11:47
1

There are a lot of threads related to this topic.

Did you try adding this

i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + attachmentFilePath));?

How to send an attachment with the Email in android?

Android: How do I attach a temporary, generated image to an email?

problem sending an email with an attachment programmatically

Community
  • 1
  • 1
DeRagan
  • 22,827
  • 6
  • 41
  • 50
1

I am facing the same problem in sending email with attach SQLite database. I am Searching for the solution for this problem but i found nothing.

there is no way to send attached file via email from internal storage.

For sending file via email first save that file to external storage and then attach and send.

I am using this code for saving file to sdcard

public void copyFileToSdCard()
    {
        File f = new File(Environment.getExternalStorageDirectory() + "/File name");
        if(f.exists()) 
        {

        }
        else
        {
            try 
            {
                File sd = Environment.getExternalStorageDirectory();
                File data = Environment.getDataDirectory();

                if (sd.canWrite()) 
                {
                    String currentPath = "file path";
                    String backupFilePath = "file name ";
                    File currentFile = new File(data, currentPath);
                    File backupFile = new File(sd, backupFilePath);

                    if (currentFile.exists()) {
                        FileChannel src = new FileInputStream(currentFile).getChannel();
                        FileChannel dst = new FileOutputStream(backupFile).getChannel();
                        dst.transferFrom(src, 0, src.size());
                        src.close();
                        dst.close();
                    }
                }
            } 
            catch (Exception e) {
                Log.w("Backup", e);
            }
        }
    }

and this for attach and send

Intent i = new Intent(Intent.ACTION_SEND);
            i.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] {emailAddress}); 
            i.putExtra(Intent.EXTRA_SUBJECT, "Subject text");
            i.putExtra(Intent.EXTRA_TEXT, "Body text");
            Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "file name"));
            i.putExtra(Intent.EXTRA_STREAM, uri);
            i.setType("text/plain");
            startActivity(Intent.createChooser(i, "Send mail"));
Deepak
  • 1,989
  • 1
  • 18
  • 20
0

try this

emailintent.setType("text/calendar");
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • what has that got to do with the email attachment.. why is not showing on when i receive a mail...its showing when i send the mail – user590849 May 19 '11 at 12:13
  • or emailintent.setType("text/plain"); – jkhouw1 May 19 '11 at 12:14
  • first tell me what is attachment.ics? – Niranj Patel May 19 '11 at 12:18
  • it is a file that has some event details that i have put via code. This file can be opened on MAC systems. if we click on an ics file on a mac then the contents of the file will directly get added to the calendar present inside the mac OS.. ie iCal. – user590849 May 20 '11 at 07:28
  • just try to attach image and check image will coming in email or not..? I think this file will create issue.. – Niranj Patel May 20 '11 at 07:33
  • i really got to send the ics file. the file is being properly made.i have checked it. Is it something like that i cannot send the ics file from Android? – user590849 May 20 '11 at 09:41
  • so image was working fine and ics was not so, may be android doesn't support some file – Niranj Patel May 20 '11 at 09:48
  • ok. i tried doing the above for txt files also.. they too don't work... if you want to check out my code i can paste it... – user590849 May 20 '11 at 10:00
  • Android support ics file only you have to set emailIntent.setType("text/calendar") mime type... Just check one thing, are you getting file from your specified path??? – Andy May 20 '11 at 10:47
  • yes. i am pulling the file from the device and i can get the file. But when i URI parse the filepath, i guess that is when the problem occurs. – user590849 May 20 '11 at 11:09
0
public class SendingMail {

public static void SendingMail(Context context) {

    final Intent emailIntent = new Intent(
            android.content.Intent.ACTION_SEND);

    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc@wxy.com"});  

    emailIntent.setType("text/html");


    // Image file saved in sdcard 

        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+File.separator+"sdcard"
                + File.separator + "MyImage.png"));


    emailIntent.putExtra(Intent.EXTRA_TEXT, "My image");

    context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}

}

This will work...

Andy
  • 5,379
  • 7
  • 39
  • 53