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

除EmailManager外,UWP中是否有其他调用Windows Mail App发邮件的方式?

UWP中通过Windows Mail App发送邮件的替代方法

你在使用EmailManager发送HTML格式邮件时遇到API兼容问题,现有代码无法满足需求,以下是两种可行的替代方案:

方法一:使用共享合约(Share Contract)

共享合约是UWP与其他应用交互的标准机制,通过它可以将HTML内容和附件正确传递给Windows Mail App,且Mail App能正常解析HTML格式。

示例代码:

private async void SendViaShareContract(StorageFile file, string htmlContent)
{
    DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
    dataTransferManager.DataRequested += (sender, args) =>
    {
        DataRequest request = args.Request;
        request.Data.Properties.Title = "邮件标题";
        request.Data.Properties.Description = "通过共享合约发送的HTML邮件";
        
        // 封装HTML内容,确保Mail App能识别
        request.Data.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat(htmlContent));
        
        // 添加附件
        List<IStorageItem> attachments = new List<IStorageItem>();
        attachments.Add(file);
        request.Data.SetStorageItems(attachments);
    };
    
    // 唤起共享界面,选择Windows Mail App即可
    DataTransferManager.ShowShareUI();
}

方法二:使用mailto URI(仅适用于纯文本场景)

如果需求是快速唤起Mail App,且能接受HTML标签作为纯文本显示(mailto协议本身不支持原生HTML渲染),可以用LaunchUriAsync:

示例代码:

private async void SendViaMailtoUri(string toAddress, string mailSubject, string content, StorageFile file)
{
    // 转义URI参数,避免格式错误
    string encodedSubject = Uri.EscapeDataString(mailSubject);
    string encodedContent = Uri.EscapeDataString(content);
    Uri mailtoUri = new Uri($"mailto:{toAddress}?subject={encodedSubject}&body={encodedContent}");
    
    await Launcher.LaunchUriAsync(mailtoUri);
    
    // 注意:mailto协议无法直接添加附件,需要附件的话优先用共享合约
}

重要提示

  • 共享合约是目前让Windows Mail App正确接收HTML邮件的最可靠方式,它遵循系统共享规范,兼容性更好。
  • EmailManager的EmailMessageBodyKind.Html存在版本兼容问题,这也是你遇到问题的核心原因。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 05:05:31