maui: iOS Email composing crashes App

Description

I am sending an email using the Email.Default.ComposeAsync() method

this works fine on Android but crashes on iOS with the exception below

2022-12-09 19:19:10.850 ClientUI[11511:2319672]    at System.Uri..ctor(String uriString, UriKind uriKind)
   at Foundation.NSUrl.op_Implicit(NSUrl url)
   at Microsoft.Maui.ApplicationModel.Communication.EmailImplementation.ComposeWithUrl(EmailMessage message)
2022-12-09 19:19:10.850 ClientUI[11511:2319672]    at Microsoft.Maui.ApplicationModel.Communication.EmailImplementation.PlatformComposeAsync(EmailMessage message)
   at Microsoft.Maui.ApplicationModel.Communication.EmailImplementation.ComposeAsync(EmailMessage message)

here is the code that send the email

if( Email.Default.IsComposeSupported )
        {
          string username = name;
          if( string.IsNullOrEmpty( username ) )
          {
            username = "Unknown";
          }

          string subject = $"Wi-Fi Messenger Logs for client <{username}>";
          string body = $"Attached is the zipped up log files for client <{username}>";
          string targetName = Path.Combine( TARGET_PATH,
                                            MakeValidFileName( $"{username}_{DateTime.UtcNow:yyyy-MM-dd_hh-mm-ss}.zip" ) );

          using FileStream targetFile = File.OpenWrite( targetName );
          //Emails require the zip file be in an actual file
          outputMemStream.WriteTo( targetFile );
          targetFile.Close();

          EmailMessage message = new()
          {
            Subject = subject,
            Body = body,
            BodyFormat = EmailBodyFormat.PlainText,
            Attachments = new List<EmailAttachment>() { new EmailAttachment( targetName ) },
            To = new List<string>() { recipient.Trim() }
          };

          await Email.Default.ComposeAsync( message );

I have added this to my info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>mailto</string>
</array>

Steps to Reproduce

  1. Create a maui App
  2. try to send an Email using the Email Api on a physical iOS device
  3. Observe the crash

Link to public reproduction project repository

N/A

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 16.1.2

Did you find any workaround?

No response

Relevant log output

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

It looks like the relevant code that crashes is this line: https://github.com/dotnet/maui/blob/main/src/Essentials/src/Email/Email.shared.cs#L81 It sets the url to something like mailto:email%40domain.com instead of mailto:email@domain.com and that causes the https://github.com/dotnet/maui/blob/main/src/Essentials/src/Email/Email.ios.cs#L90 line to throw an exception as its not a valid url with mailto: protocol.

@samhouts it must be backported to .NET 7