Xamarin.Forms how open default email client on device?

試著忘記壹切 提交于 2020-11-29 08:31:11

问题


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.


回答1:


Try with:

var address = "your.address@gmail.com";
Device.OpenUri(new Uri($"mailto:{address}"));

Hope this helps.-




回答2:


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.




回答3:


You can use Launcher.OpenAsync(uri) exists in Xamarin.Essentials. OpenUri is obsolete as of version 4.3.0. uri = $"mailto:{address}?subject={emailSubject}&body={body content}";



来源:https://stackoverflow.com/questions/45942291/xamarin-forms-how-open-default-email-client-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!