0

My application is a WinUI desktop application using .Net 6.

<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.

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?

Code

I made the application using the default WinUI template and simply added the following code in MainWindows.xaml.cs to test.

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. It launches the Mail app. I was expecting it to do the same in my WinUI app, but it doesn't.

Batesias
  • 1,914
  • 1
  • 12
  • 22
  • According to this doc [link](https://learn.microsoft.com/en-us/uwp/api/windows.applicationmodel.email.emailmessage.attachments?view=winrt-22621) emailMessage has an Attachment property that you can interact with. – CGA Jan 27 '23 at 19:23
  • You did not copy the complete example from learn.microsoft.com. You are getting errors because you left out some important parts..... – Luuk Jan 27 '23 at 19:24
  • 1
    @Luuk I don't think so. This exact same code works fine in a UWP app. It correctly launches the Mail app. @CGA I know. The problem is that `EmailManager` doesn't work at all (with or without attachments). – Batesias Jan 27 '23 at 19:36
  • Then it's probably: https://social.msdn.microsoft.com/Forums/en-US/ab5341e6-27fb-4eb2-97b0-57257a579bed/systemruntimeinteropservicescomexception-0x80070032?forum=xamarinvisualstudio – Luuk Jan 27 '23 at 19:42

1 Answers1

2

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.

You can check this dependency if you disassemble the ShowComposeNewEmailAsync function, as shown in this screenshot here:

enter image description here

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. Maybe you can report that to Microsoft so they can improve it.

So you'll have to find other non-WinRT/UWP ways to send email.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298