You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin应用发送邮件:UWP正常,Android报‘指定方法不支持’错误

Android平台调用Xamarin.Essentials Email.ComposeAsync报“指定方法不支持”错误(UWP正常)

以下代码在UWP平台可正常运行,但在Android平台执行时,Email.ComposeAsync(message)会触发Try/Catch块捕获到“指定方法不支持”错误。

相关代码

XAML部分

<?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>

C#代码部分

using System;
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 = { "user@outlook.com"};
            
            var message = new EmailMessage("Bowlers Scheduled for Week", body, to);
            
            try
            {
                await Email.ComposeAsync(message);
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
        }
    }
}

解决方案

针对Android平台的报错,可按以下步骤排查修复:

  1. 检查AndroidManifest.xml配置
    确保在Android项目的AndroidManifest.xml中添加邮件相关的intent-filter,同时确认Xamarin.Essentials初始化代码已配置:

    <!-- 添加邮件跳转的intent-filter -->
    <intent-filter>
      <action android:name="android.intent.action.SENDTO" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:scheme="mailto" />
    </intent-filter>
    

    在Android项目的MainActivity.cs的OnCreate方法中添加初始化代码:

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    
  2. 确认设备存在可用邮件客户端
    Android系统无内置邮件应用,需确保测试设备或模拟器上已安装并配置至少一个邮件客户端(如Gmail、Outlook),否则Email.ComposeAsync会因找不到可处理的应用而报错。

  3. 添加支持性前置检查
    在调用ComposeAsync前,先判断当前平台是否支持邮件撰写,避免直接触发异常:

    private async void SendEmail_Clicked(object sender, EventArgs e)
    {
        string body = "Test bowling message";
        string[] to = { "user@outlook.com"};
        
        var message = new EmailMessage("Bowlers Scheduled for Week", body, to);
        
        try
        {
            if (await Email.IsComposeSupportedAsync())
            {
                await Email.ComposeAsync(message);
            }
            else
            {
                await DisplayAlert("提示", "当前设备不支持邮件撰写功能", "确定");
            }
        }
        catch (Exception ex)
        {
            await DisplayAlert("错误", ex.Message, "确定");
        }
    }
    
  4. 检查版本兼容性
    确保Xamarin.Essentials版本与当前Xamarin.Forms版本兼容,建议更新至最新稳定版,避免因版本不匹配导致的API支持问题。

内容的提问来源于stack exchange,提问作者Shades49

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 18:19:55