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

.NET MAUI Windows端IsComposeSupported判断异常,如何正确检测邮件功能?

解决.NET MAUI Windows平台邮件发送IsComposeSupported误判引发的COM异常

问题场景

使用.NET MAUI官方邮件发送代码在Windows平台运行时,IsComposeSupported返回true,但调用ComposeAsync却抛出System.Runtime.InteropServices.COMException(提示“请求不被支持”),iOS和Android平台运行正常。

原代码如下:

try
{
    if (Microsoft.Maui.ApplicationModel.Communication.Email.Default.IsComposeSupported)
    {
        var message = new EmailMessage
        {
            Subject = string.Empty,
            Body = string.Empty,
            BodyFormat = EmailBodyFormat.PlainText,
            To = new List<string>() { _emailTechService },
        };

        await Email.Default.ComposeAsync(message);
    }
    else
    {
        await Shell.Current.DisplayAlert("Email Failure", $"Sending emails feature is not supported on this device", "OK");
    }
}
catch (Exception ex)
{
    App.HandleException(ex, true);
}

问题原因

Windows平台的IsComposeSupported仅检查系统是否注册了mailto协议,不会验证是否有实际可用的邮件客户端。当系统仅注册协议但无客户端时,就会触发该异常。

解决方案

方案1:针对Windows平台补充客户端可用性检测

通过Windows原生API检查是否有可用的邮件客户端,结合IsComposeSupported做双重判断:

// 封装Windows平台专属检测方法
private bool IsEmailClientAvailableOnWindows()
{
    try
    {
        var uri = new Uri("mailto:");
        var options = new Windows.System.LauncherOptions();
        var supportStatus = Windows.System.Launcher.QueryUriSupportAsync(uri, Windows.System.LaunchQuerySupportType.Uri, options).GetAwaiter().GetResult();
        return supportStatus == Windows.System.LaunchQuerySupportStatus.Available;
    }
    catch
    {
        return false;
    }
}

修改原判断逻辑:

try
{
    bool isSupported = Email.Default.IsComposeSupported;

    // Windows平台补充检测
#if WINDOWS
    isSupported = isSupported && IsEmailClientAvailableOnWindows();
#endif

    if (isSupported)
    {
        var message = new EmailMessage
        {
            Subject = string.Empty,
            Body = string.Empty,
            BodyFormat = EmailBodyFormat.PlainText,
            To = new List<string>() { _emailTechService },
        };

        await Email.Default.ComposeAsync(message);
    }
    else
    {
        await Shell.Current.DisplayAlert("Email Failure", $"Sending emails feature is not supported on this device", "OK");
    }
}
catch (Exception ex)
{
    App.HandleException(ex, true);
}

方案2:直接捕获特定COM异常

如果不想额外做前置检测,可以直接捕获Windows平台抛出的特定异常,转为功能不支持提示:

try
{
    if (Microsoft.Maui.ApplicationModel.Communication.Email.Default.IsComposeSupported)
    {
        var message = new EmailMessage
        {
            Subject = string.Empty,
            Body = string.Empty,
            BodyFormat = EmailBodyFormat.PlainText,
            To = new List<string>() { _emailTechService },
        };

        await Email.Default.ComposeAsync(message);
    }
    else
    {
        await Shell.Current.DisplayAlert("Email Failure", $"Sending emails feature is not supported on this device", "OK");
    }
}
catch (System.Runtime.InteropServices.COMException ex) when (ex.Message.Contains("请求不被支持"))
{
    // 处理Windows平台特定异常
    await Shell.Current.DisplayAlert("Email Failure", $"Sending emails feature is not supported on this device", "OK");
}
catch (Exception ex)
{
    App.HandleException(ex, true);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 09:25:36