Xamarin.Forms how open default email client on device?

Published

In Xamarin.Forms if you want to open the device’s default browser by tapping a Label with a link, it’s simple as this:

private void WebUrl_TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
  var label = sender as Label;

  string url = "http://" + label.Text;

  Device.OpenUri(new Uri(url));
}

Is there a similarly simple way to open the device’s default email client with an open NewMessage with email address?

private void EmailClient_TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
  var label = sender as Label;

  // what goes here?
}

Thank you.

Source: Xamarin.ios Questions

Published
Categorised as Uncategorised Tagged , , ,

Answers

I actually use a Dependency Service so that I have more control over what I can send to mail client.

First I created an interface to be used by the dependency service called IEmailService.

public interface IEmailService
{
    void CreateEmail(List<string> emailAddresses, List<string> ccs, string subject, string body, string htmlBody);
}

My Dependency Service for Android looks like this:

[assembly: Xamarin.Forms.Dependency(typeof(EmailService))]
namespace Droid.Services
{
    public class EmailService : IEmailService
    {
         public void CreateEmail(List<string> emailAddresses, List<string> ccs, string subject, string body, string htmlBody)
        {
            var email = new Intent(Android.Content.Intent.ActionSend);

            if (emailAddresses?.Count > 0)
            {
                email.PutExtra(Android.Content.Intent.ExtraEmail, emailAddresses.ToArray());
            }

            if (ccs?.Count > 0)
            {
                email.PutExtra(Android.Content.Intent.ExtraCc, ccs.ToArray());
            }

            email.PutExtra (Android.Content.Intent.ExtraSubject, subject);

            email.PutExtra (Android.Content.Intent.ExtraText, body);

            email.PutExtra (Android.Content.Intent.ExtraHtmlText, htmlBody);



            email.SetType ("message/rfc822");

            MainActivity.SharedInstance.StartActivity(email);

        }
    }
}

For iOS:

[assembly: Xamarin.Forms.Dependency(typeof(EmailService))]
namespace iOS.Services
{
    public class EmailService : NSObject, IEmailService, IMFMailComposeViewControllerDelegate
    {


        public void CreateEmail(List<string> emailAddresses, List<string> ccs, string subject, string body, string htmlBody)
        {
            var vc = new MFMailComposeViewController();
            vc.MailComposeDelegate = this;

            if(emailAddresses?.Count > 0)
            {
                vc.SetToRecipients(emailAddresses.ToArray());
            }

            if(ccs?.Count > 0)
            {
                vc.SetCcRecipients(ccs.ToArray());
            }

            vc.SetSubject(subject);
            vc.SetMessageBody(htmlBody, true);
            vc.Finished += (sender, e) =>
            {
                vc.DismissModalViewController(true);
            };



           UIApplication.SharedApplication.Windows[0].
               RootViewController.PresentViewController(vc, true, null);

        }


    }
}

Then I can just call this in my code:

DependencyService.Get<IEmailService>().CreateEmail(recipients, ccs, subject, body, bodyHtml);

This will open the mail client on each platform with the to, subject and body fields optionally poplulated.

I hope that helps.


Omer Hirthe

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq