2

I'm trying to send a mail along with multiple files attached to it, however I can't get them to be added to the mail. I proceed like this:

private void SendMail (List<Data> ToSend)
    {
        var Attachments = new List<Android.Net.Uri>();
        Intent i = new Intent (Android.Content.Intent.ActionSendMultiple);
        i.SetType ("message/rfc822");
        i.PutExtra (Android.Content.Intent.ExtraEmail, new String[]{"try@mail.com"});
        i.PutExtra (Android.Content.Intent.ExtraSubject, "Test");
        i.PutExtra (Android.Content.Intent.ExtraText, "Test Test...");
        foreach (var content in ToSend) {
            Java.IO.File myFile = new Java.IO.File(content.attachmentloc);
            // attachmentloc is a string containing the absolute path to the file to attach.
            var uri = Android.Net.Uri.FromFile(myFile);
            Attachments.Add (uri);
        }
        i.PutParcelableArrayListExtra(Android.Content.Intent.ExtraStream, Attachments.ToArray());
        StartActivityForResult(Intent.CreateChooser(i, "Send mail..."), 0);
    }

I checked and the path in the string is good.. however the method .Exists (When used on the Java.IO.File in the foreach) returns false. might be the reason why ?

Thanks for the help.

EDIT:

When trying to add a single attachment, it works just fine. However whenever I call a function that imply there will be more than one attachment, it fails.

Aka:

Intent i = new Intent (Android.Content.Intent.ActionSend);
var uri = Android.Net.Uri.Parse (ex._FileLocation);
i.PutExtra(Intent.ExtraStream, uri);

Works just fine however replacing

    Intent i = new Intent (Android.Content.Intent.ActionSend);

By

    Intent i = new Intent (Android.Content.Intent.ActionSendMultiple);

Leads to the same fail and so does replacing:

var uri = Android.Net.Uri.Parse (ex._FileLocation);
i.PutExtra(Intent.ExtraStream, uri);

By

var Attachments = new List<Android.Net.Uri> ();
                foreach (var ex in ToSend) {
                var uri = Android.Net.Uri.Parse (ex._FileLocation);
                Attachments.Add (uri);
                //o
            }
            i.PutParcelableArrayListExtra (Android.Content.Intent.ExtraStream, Attachments.ToArray ());

... I'm using the default mail application (not gmail) I also tried setting the intent type to " * / * " (without spaces) as suggested somewhere else. Also tried AddFlags (ActivityFlags.GrantReadUriPermission);

As it works with a single attachment, I know the URIs are valid for sure...

I'd really need help.

MagiKruiser
  • 431
  • 1
  • 6
  • 19
  • http://stackoverflow.com/questions/2264622/android-multiple-email-attachments-using-intent – ChuKoNu Feb 14 '13 at 10:58
  • Unless I missed something, my code is most likely the same.. I obviously found that answer before... Yet it wouldn't work for me :/ I probably do something wrong but can't find what. – MagiKruiser Feb 14 '13 at 11:05

2 Answers2

2

I personally found no valid answer to this problem. Only answer I found is a workaround: compress all the file into a single .zip archive and send that archive as the single attachment.

EvilSakray
  • 40
  • 4
0

might be, the mail activity has not enough rights to read your file. Try to add myFile.setReadable(true, false) either when creating file, or here before adding to Attachments array.

thomas
  • 265
  • 2
  • 4