Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms how open default email client on device?

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.

like image 710
PaxForce Avatar asked Dec 01 '22 15:12

PaxForce


2 Answers

Try with:

var address = "[email protected]";
Device.OpenUri(new Uri($"mailto:{address}"));

Hope this helps.-

like image 154
pinedax Avatar answered Dec 04 '22 21:12

pinedax


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.

like image 35
Jacob Joz Avatar answered Dec 04 '22 20:12

Jacob Joz