尝试通过Xamarin应用程序发送电子邮件.与UWP正常工作,但与Andoid失败-不支持指定的方法,

deikduxw  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(163)

代码在UWP中工作正常,但在Android中失败。在我的Try... Catch中收到错误“Specified method is not supported.”。根据@JamesMontemagno,此代码应该可以工作。有关应用程序的更多细节,请参阅https://www.youtube.com/watch?v=YZ-Yw9CRxAY。代码如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SendEmail.MainPage">

    <StackLayout>
        <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
            <Label Text="Email Sample" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
        </Frame>
        <Button x:Name="SendEmail" Text="Send" Clicked="SendEmail_Clicked"></Button>
    </StackLayout>

</ContentPage>

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace SendEmail
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private async void SendEmail_Clicked(object sender, EventArgs e)
        {
            string body = "Test bowling message";
            string[] to = { "[email protected]"};
            //EmailAttachment attachment = new EmailAttachment(attach); 
            
            var message = new EmailMessage("Bowlers Scheduled for Week", body, to);
            //message.Attachments.Add(attachment);
            try
            {
                await Email.ComposeAsync(message);
            }
            catch (Exception ex)
            {
                //bool result = await DisplayAlert(ex.ToString(), "Send Email", "Yes", "No");
                string msg = ex.Message;
            }
        }
    }
}

字符串

7qhs6swi

7qhs6swi1#

我测试了代码,它在我这边运行得很好。下面是你可能遇到的问题。
首先,如果项目的Target Android版本设置为Android 11(R API 30),则必须使用新package visibility requirements使用的查询更新Android Manifest。
请在manifest节点内添加以下内容:

<queries>
  <intent>
    <action android:name="android.intent.action.SENDTO" />
    <data android:scheme="mailto" />
  </intent>
</queries>

字符串
第二,电子邮件。如果设备上没有电子邮件客户端应用程序可用于发送电子邮件,ComposeAsync将返回FeatureNotSupportedException: 'Specified method is not supported.'。您可以在手机上安装Gmail应用程序,当您单击按钮时,它将打开Gmail并发送电子邮件。

相关问题