0

I came across this error when Trying to send an email with attachment. any help ? Thank you

****Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?****

this is my code :

[assembly: Dependency(typeof(sendEmail))]
namespace myapp.Droid
{
    public class sendEmail : IEmailTask
    {
        public sendEmail()
        {

        }

        public void SendEmail () 
        {
            var sqlliteFilname = "test.3gpp";
            string documentsPath = System.Environment.GetFolderPath(
            Environment.SpecialFolder.Personal);
            var stringPath = Path.Combine(documentsPath, sqlliteFilname);

            var path = Android.Net.Uri.FromFile(new 
           Java.IO.File(stringPath));

            Intent emailIntent = new Intent(Intent.ActionSend);
            // set the type to 'email'
            emailIntent.SetData(Android.Net.Uri.Parse("mailto:"));

            String[] to = { "youremail@mail.com" };

            emailIntent.PutExtra(Intent.ExtraEmail, to);
            // the attachment
            emailIntent.PutExtra(Intent.ExtraStream, path);
            // the mail subject
            emailIntent.PutExtra(Intent.ExtraSubject, "Subject");

           Android.App.Application.Context.StartActivity(Intent.CreateChooser(emailIntent, "Send email..."));


        }
    }
}

code on the page is :

 void btnSendingHandle_Clicked(object sender, System.EventArgs e)
        {
            var getEmail = DependencyService.Get<IEmailTask>();

            getEmail.SendEmail();
        }
Pxaml
  • 651
  • 2
  • 12
  • 38
  • Since this is a Forms app I'd try changing your Context to `Forms.Context.StartActivity (Intent.CreateChooser(emailIntent, "Send email..."));` – Nick Peppers Dec 07 '17 at 21:12

2 Answers2

0

Like Nick had said

  • you could use Forms.Context to start activity.

Or

  • you could add emailIntent.SetFlags(ActivityFlags.NewTask); in your emailIntent.

In Android, every Activity should have its own task stack which you can use taskAffinity to define it, the package's name is the default value. But if you use Application.Context to start activity, there is not task stack for your activity, so it suggests you to use FLAG_ACTIVITY_NEW_TASK flag, this flag will create a task stack for you activity.

Robbit
  • 4,300
  • 1
  • 13
  • 29
  • Thank you for reply. I tried both solutions , but I am still getting the same error . I dont get the option to add Forms.context , Android.App.Application.Context.StartActivity(Intent.CreateChooser(emailIntent, "Send email...")); – Pxaml Dec 09 '17 at 19:55
  • where exactly should I call this task ? emailIntent.SetFlags(ActivityFlags.NewTask); – Pxaml Dec 09 '17 at 19:57
  • Not `Forms.context`, it is `Forms.Context`, make sure it is `Xamarin.Forms.Forms.Context`, and [this is the document about task stack in Android](https://developer.android.com/guide/components/activities/tasks-and-back-stack.html), [this is about the error in Android](https://stackoverflow.com/questions/3918517/calling-startactivity-from-outside-of-an-activity-context) – Robbit Dec 11 '17 at 02:28
0

check if you have an attachment to send. *To be replace for the if block & change this

Intent emailIntent = new Intent(Intent.ActionSend);
//change it to 


Intent emailIntent = new Intent(Intent.ActionSendto);

Android.App.Application.Context.StartActivity(Intent.CreateChooser(emailIntent, "Send email..."));

if (emailIntent.ResolveActivity(Android.App.Application.Context.PackageManager) != null)
            {
                Android.App.Application.Context.StartActivity(emailIntent);
            }
            else
            {

                    string tag = "MY-EMAIL";
                    Log.Info(tag, "no attachment found");
            }

my issue relies on the attachment itself.

Pxaml
  • 651
  • 2
  • 12
  • 38