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.