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

vybvopom  于 2023-04-03  发布在  其他
关注(0)|答案(4)|浏览(155)

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

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

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

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

有没有类似的简单方法,可以打开设备的默认电子邮件客户端与一个开放的新消息与电子邮件地址?

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

  // what goes here?
}

谢谢大家。

92dk7w1h

92dk7w1h1#

尝试:

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

希望这个有用。-

0lvr5msh

0lvr5msh2#

我实际上使用了一个依赖服务,这样我就可以更好地控制我可以发送到邮件客户端的内容。
首先,我创建了一个由依赖项服务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);

这将在每个平台上打开邮件客户端,并可选择填充收件人、主题和正文字段。
希望能有所帮助。

piztneat

piztneat3#

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

axkjgtzd

axkjgtzd4#

设备.URI现在贬值-使用这个代替。
public String s($“String s”);
适用于iOS和Android

相关问题