user590849
user590849

Reputation: 11765

email attachment not successful in android

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

Upvotes: 1

Views: 3859

Answers (5)

Deepak
Deepak

Reputation: 2009

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

Upvotes: 1

user590849
user590849

Reputation: 11765

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.

Upvotes: 3

Andy
Andy

Reputation: 5379

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[]{"[email protected]"});  

    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...

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33248

try this

emailintent.setType("text/calendar");

Upvotes: 0

DeRagan
DeRagan

Reputation: 22920

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

Upvotes: 1

Related Questions