Xamarin.Forms如何在设备上打开默认电子邮件客户端?

时间:2017-08-29 14:53:59

标签: xamarin xamarin.ios xamarin.android xamarin.forms

在Xamarin.Forms中,如果你想通过点击带有链接的Label打开设备的默认浏览器,它很简单:

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

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

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

使用带有电子邮件地址的打开NewMessage打开设备的默认电子邮件客户端是否有类似的简单方法?

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

  // what goes here?
}

谢谢。

3 个答案:

答案 0 :(得分:11)

尝试:

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

希望这会有所帮助.-

答案 1 :(得分:1)

我实际上使用了依赖服务,这样我就可以更好地控制我可以发送给邮件客户端的内容。

首先,我创建了一个名为IEmailService的依赖服务使用的接口。

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

我的Android依赖关系服务如下所示:

[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);

        }
    }
}

对于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);

        }


    }
}

然后我可以在我的代码中调用它:

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

这将打开每个平台上的邮件客户端,并可选择填充to,subject和body字段。

我希望有所帮助。

答案 2 :(得分:0)

您可以使用Launcher.OpenAsync(uri)中存在的Xamarin.Essentials。自version 4.3.0. uri = $"mailto:{address}?subject={emailSubject}&body={body content}";

起,OpenUri已过时