簡體   English   中英

如何在 WinUI 應用程序中發送帶附件的 email?

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

我的應用程序是一個使用 .Net 6 的 WinUI 桌面應用程序。

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

我以為我可以使用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)

用於從 WinUI 應用程序發送電子郵件的正確 API 是什么?

代碼

我使用默認的 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);
    }
}

請注意,前面的代碼在 UWP 應用程序中運行良好。 它啟動郵件應用程序。 我期待它在我的 WinUI 應用程序中做同樣的事情,但事實並非如此。

EmailManager API 僅受 UWP 應用程序支持,因為它依賴於Windows.UI.Core.CoreWindow ClassGetForCurrentThread 方法對於非 UWP 應用程序,該方法始終返回 null

如果您反匯編ShowComposeNewEmailAsync function,則可以檢查此依賴項,如此處的屏幕截圖所示:

在此處輸入圖像描述

其中一些 WinRT 類已被修改以支持IInitializeWindow 技巧以支持經典 HWND,但這里似乎並非如此。 也許您可以將其報告給 Microsoft,以便他們改進它。

因此,您必須找到其他非 WinRT/UWP 方式來發送 email。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM