简体   繁体   English

如何在 WinUI 应用程序中发送带附件的 email?

[英]How to send email with attachments in WinUI app?

My application is a WinUI desktop application using.Net 6.我的应用程序是一个使用 .Net 6 的 WinUI 桌面应用程序。

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

I thought I could use the EmailManager from UWP (which supports email attachments) but I get the following error when I do.我以为我可以使用UWP中的 EmailManager(它支持 email 附件),但是当我这样做时出现以下错误。

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)

What is the correct API to use to send emails from a WinUI application?用于从 WinUI 应用程序发送电子邮件的正确 API 是什么?

Code代码

I made the application using the default WinUI template and simply added the following code in MainWindows.xaml.cs to test.我使用默认的 WinUI 模板创建了应用程序,并在MainWindows.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);
    }
}

Note that the previous code works fine in a UWP app.请注意,前面的代码在 UWP 应用程序中运行良好。 It launches the Mail app.它启动邮件应用程序。 I was expecting it to do the same in my WinUI app, but it doesn't.我期待它在我的 WinUI 应用程序中做同样的事情,但事实并非如此。

The EmailManager API is only supported with UWP apps, because it depends on Windows.UI.Core.CoreWindow Class 's GetForCurrentThread method which always get backs null for non-UWP apps . EmailManager API 仅受 UWP 应用程序支持,因为它依赖于Windows.UI.Core.CoreWindow ClassGetForCurrentThread 方法对于非 UWP 应用程序,该方法始终返回 null

You can check this dependency if you disassemble the ShowComposeNewEmailAsync function, as shown in this screenshot here:如果您反汇编ShowComposeNewEmailAsync function,则可以检查此依赖项,如此处的屏幕截图所示:

在此处输入图像描述

Some of these WinRT classes have been modified to support the IInitializeWindow trick to support classical HWNDs, but it seems it's not the case here.其中一些 WinRT 类已被修改以支持IInitializeWindow 技巧以支持经典 HWND,但这里似乎并非如此。 Maybe you can report that to Microsoft so they can improve it.也许您可以将其报告给 Microsoft,以便他们改进它。

So you'll have to find other non-WinRT/UWP ways to send email.因此,您必须找到其他非 WinRT/UWP 方式来发送 email。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM