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

WinUI桌面应用(.NET6)如何发送带附件邮件?正确API是什么?

WinUI 3桌面应用中发送邮件的正确API方案

项目环境

基于.NET 6的WinUI桌面应用,项目配置如下:

<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<UseWinUI>true</UseWinUI>

问题现象

原本尝试使用UWP的EmailManagerAPI(支持邮件附件),但调用时抛出以下异常:

System.Runtime.InteropServices.COMException (0x80070032): The request is not supported. (0x80070032)
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.Windows.ApplicationModel.Email.IEmailManagerStaticsMethods.ShowComposeNewEmailAsync(IObjectReference _obj, EmailMessage message)
   at Windows.ApplicationModel.Email.EmailManager.ShowComposeNewEmailAsync(EmailMessage message)
   at WinUIApp.MainWindow.ComposeEmail(String subject, String messageBody)

测试代码基于默认WinUI模板编写,仅在MainWindow.xaml.cs中添加以下逻辑:

private void myButton_Click(object sender, RoutedEventArgs e)
{
    myButton.Content = "Clicked";

    ComposeEmail("Bob", "Hello Bob.");
}

private async Task ComposeEmail(string subject, string messageBody)
{
    try
    {
        var emailMessage = new Windows.ApplicationModel.Email.EmailMessage();
        await Windows.ApplicationModel.Email.EmailManager.ShowComposeNewEmailAsync(emailMessage);
    }
    catch (Exception e)
    {
        Debug.WriteLine(e);
    }
}

上述代码在UWP应用中可正常启动邮件客户端,但WinUI桌面应用中无法运行。

核心问题

WinUI桌面应用中发送邮件应使用什么正确的API?


解决方案

UWP的EmailManagerAPI仅适用于UWP应用,WinUI桌面应用(非MSIX打包的纯桌面应用)调用会触发"不支持请求"的异常。以下是两种可行替代方案:

1. 使用mailto URI协议

这是轻量且跨平台的方案,直接唤起系统默认邮件应用,支持主题、正文、收件人参数,部分邮件客户端支持附件(兼容性依赖客户端实现)。

示例代码:

private async Task ComposeEmail(string subject, string messageBody)
{
    try
    {
        // 编码参数避免特殊字符问题
        var encodedSubject = Uri.EscapeDataString(subject);
        var encodedBody = Uri.EscapeDataString(messageBody);
        var mailtoUri = new Uri($"mailto:?subject={encodedSubject}&body={encodedBody}");
        
        // 启动默认邮件应用
        await Windows.System.Launcher.LaunchUriAsync(mailtoUri);
    }
    catch (Exception e)
    {
        Debug.WriteLine(e);
    }
}

若需添加附件,可尝试在URI中加入attachment参数(注意仅部分客户端支持):

var attachmentPath = @"C:\example\file.pdf";
var encodedAttachment = Uri.EscapeDataString(attachmentPath);
var mailtoUri = new Uri($"mailto:?subject=Test&body=Hello&attachment={encodedAttachment}");

2. 使用DataTransferManager共享API(可靠支持附件)

如果需要稳定的附件发送功能,可使用系统共享API,让用户选择邮件应用完成发送,支持多附件和完整的内容共享。

示例代码:

using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using System.Collections.Generic;

private void ComposeEmailWithAttachment(string subject, string messageBody, string attachmentPath)
{
    try
    {
        var dataTransferManager = DataTransferManager.GetForCurrentView();
        dataTransferManager.DataRequested += (sender, args) =>
        {
            var request = args.Request;
            // 设置共享内容的标题和描述
            request.Data.Properties.Title = subject;
            request.Data.Properties.Description = messageBody;
            
            // 添加文本正文
            request.Data.SetText(messageBody);
            
            // 添加附件文件
            var attachmentFile = StorageFile.GetFileFromPathAsync(attachmentPath).GetAwaiter().GetResult();
            request.Data.SetStorageItems(new List<IStorageItem> { attachmentFile });
            
            // 建议系统优先展示邮件类应用
            request.Data.Properties.SuggestedApplicationCategory = SuggestedApplicationCategory.Mail;
        };
        
        // 唤起系统共享UI
        DataTransferManager.ShowShareUI();
    }
    catch (Exception e)
    {
        Debug.WriteLine(e);
    }
}

方案对比

方案优点缺点适用场景
mailto协议实现简单、无需权限附件支持兼容性差仅需发送文本内容的场景
DataTransferManager可靠支持附件、功能完整需要用户手动选择应用需要发送附件的场景

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 17:31:00