1

I'm working on a Xamarin Forms app and am trying to open the the default mail client directly to the Inbox.

I'm able to open and pass data through to compose a message using XF Essentials

Email.ComposeAsync(message);

But I would like the app to open the default mail app's Inbox on a button press. Is this possible in Xamarin Forms?

  • Check [this answer](https://stackoverflow.com/a/31594815/7432494), you would have to implement it on your android project and then using it via [dependency injection](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/dependency-injection) – FabriBertani May 10 '19 at 14:20
  • Thanks a lot. I saw both answers when I cam into work this morning. Thanks for taking the time to reply. – James Cheevers May 13 '19 at 12:22

1 Answers1

3

I think Dependency Service is what you need. Create an interface in your Forms project:

public interface IOpenManager
{
    void openMail();
}

Then implement it on each platform, for iOS:

[assembly: Dependency(typeof(OpenImplementation))]
namespace Demo.iOS
{
    public class OpenImplementation : IOpenManager
    {
        public void openMail()
        {
            NSUrl mailUrl = new NSUrl("message://");
            if (UIApplication.SharedApplication.CanOpenUrl(mailUrl))
            {
                UIApplication.SharedApplication.OpenUrl(mailUrl);
            }            
        }
    }
}

For Android:

[assembly: Dependency(typeof(OpenImplementation))]
namespace Demo.Droid
{
    public class OpenImplementation : IOpenManager
    {
        public void openMail()
        {
            Intent intent = new Intent(Intent.ActionMain);
            intent.AddCategory(Intent.CategoryAppEmail);
            Android.App.Application.Context.StartActivity(intent);
        }
    }
}

At last, call this dependcy via: DependencyService.Get<IOpenManager>().openMail();

Ax1le
  • 6,563
  • 2
  • 14
  • 61