2

I have a .Net MAUI app. I have the following code to send emails (taken from https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/communication/email?view=net-maui-7.0&tabs=windows):

        try
        {
            if (Microsoft.Maui.ApplicationModel.Communication.Email.Default.IsComposeSupported)
            {
                var message = new EmailMessage
                {
                    Subject = string.Empty,
                    Body = string.Empty,
                    BodyFormat = EmailBodyFormat.PlainText,
                    To = new List<string>() { _emailTechService },
                };

                await Email.Default.ComposeAsync(message);
            }
            else
            {
                await Shell.Current.DisplayAlert("Email Failure", $"Sending emails feature is not supported on this device", "OK");
            }
        }
        catch (Exception ex)
        {
            App.HandleException(ex, true);
        }

On iOS and Android it works correctly, but when I run it on Windows Machine in Visual Studio, IsComposeSupported is true, but Email.Default.ComposeAsync(message) throws a System.Runtime.InteropServices.COMException "The request is not supported."

How can I check if the feature is supported correctly to avoid the exception?

David Shochet
  • 5,035
  • 11
  • 57
  • 105
  • 1
    I've upvoted the question, but IMHO this is a situation where handling the exception in `catch` seems like a fine solution to me. This isn't performance-critical code. If necessary for sane logic flow, wrap the `ComposeAsync` line itself in another try-catch. – ToolmakerSteve Nov 30 '22 at 19:30
  • @ToolmakerSteve I have thought about using try-catch, but the exception type is not specific enough, unless I check against the error message, which seems weird to me... – David Shochet Nov 30 '22 at 21:30

1 Answers1

0

This is an existed issue on the github. You can check the issue which is about Sending e-mail works in Android but not in Windows.

It used the same code as yours and met the same error. According to the comment in it, the cause is the issue for the WinUI. You can follow up it on the github.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • I have seen that thread. It refers to another one that claims issue to be fixed: https://github.com/dotnet/maui/pull/10063: "Fix Essentials Email on Windows. With the move from Xamarin.Forms to .NET MAUI has also moved from using UWP to WinUI for support on Windows where the APIs used to send email were not supported. To get our API working correctly (no exceptions), we move on to exposing and using the Win32 API (MAPI32.DLL). Issues Fixed". But I don't understand how it is fixed and what I should do to make it work. – David Shochet Dec 01 '22 at 11:21
  • According to `we move on to exposing and using the Win32 API (MAPI32.DLL)`, it seems the solution is using the MAPI32.DLL to send email instead of the Essential.Mail. You can refer to this [link shows how to use it to send mail](https://stackoverflow.com/questions/70833946/mapi-opening-default-mail-client-under-my-application). @DavidShochet – Liyun Zhang - MSFT Dec 02 '22 at 09:49