maui icon indicating copy to clipboard operation
maui copied to clipboard

iOS Email composing crashes App

Open LennoxP90 opened this issue 2 years ago • 8 comments

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

LennoxP90 avatar Dec 10 '22 00:12 LennoxP90

More information I figured out by looking through the source code for iOS email implementation

Task PlatformComposeAsync(EmailMessage message)
		{
#if !(MACCATALYST || MACOS)
			if (MFMailComposeViewController.CanSendMail)
				return ComposeWithMailCompose(message);
			else
				return ComposeWithUrl(message);
#else
			return Task.CompletedTask;
#endif
		}

in my iOS device i have not setup the Mail client thus the implementation MFMailComposeViewController.CanSendMail is returning false, but the ComposeWithUrl throws an exception when it tries to use the Launcher Api

I ended up creating a platform implementation to check if MFMailComposeViewController.CanSendMail returns false and doesn't try to send the email, which also looking at how ComposeWithUrl is implemented it doesn't allow attachments which i require anyways.

LennoxP90 avatar Dec 10 '22 03:12 LennoxP90

+1

samirgcofficial avatar Dec 10 '22 14:12 samirgcofficial

Hi @LennoxP90. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Dec 12 '22 08:12 ghost

https://github.com/LennoxP90/EmailComposeIssue

you must test with a physical iPhone with the mail client not setup to reproduce the crash

LennoxP90 avatar Dec 12 '22 16:12 LennoxP90

Same issue here, Email compose action fails from maui app running on iOS 16.2. I physically loaded the app on iPhone device.

Update: What is odd is when I run the app in Debug mode in Visual Studio and select my physical iphone to run on, after the app installs, I click the button to compose email and works as expected. It's when I do a release build and download it through test flight the email action crashes the app.

MxD-js avatar Dec 19 '22 00:12 MxD-js

I've also tried using the share feature as a workaround but same thing the app crashes. It works in debug to the physical device but does not work when I build a release.

@LennoxP90 How did you get that error? I'm looking through the crash logs that are submitted through test flight but it's a thread dump instead of meaningful info.

https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/data/share?view=net-maui-7.0&tabs=ios

MxD-js avatar Dec 19 '22 14:12 MxD-js

Any workarounds for this? I can't publish the app with this critical function not working.

MxD-js avatar Dec 22 '22 01:12 MxD-js

I've also tried using the share feature as a workaround but same thing the app crashes. It works in debug to the physical device but does not work when I build a release.

@LennoxP90 How did you get that error? I'm looking through the crash logs that are submitted through test flight but it's a thread dump instead of meaningful info.

https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/data/share?view=net-maui-7.0&tabs=ios

I just run the app on a physical device WITHOUT the mail client setup in debug crashes everytime.

Read my previous investigation on why it is crashing.

LennoxP90 avatar Dec 22 '22 07:12 LennoxP90

I'm stuck on the same thing. So are we saying that as of now its not possible to create an email compose template with an attachment on iOS unless there's the apple Mail client setup on the device?

sundeep22 avatar Jan 03 '23 07:01 sundeep22

Yes

LennoxP90 avatar Jan 03 '23 10:01 LennoxP90

That’s a shame. Defeats the purpose of setting “Default Mail App” for third party mail apps on iOS. Anyway it is what it is.

sundeep22 avatar Jan 03 '23 11:01 sundeep22

I'm having the same issue: clean physical iOS device (6s) with email app installed but no email account set up.

Is it an idea to change the IsComposeSupported bool to check for this so that it returns false in this case? Or else just not crash the app but do nothing and let the ComposeAsync return false so that we know it didn't work and we can handle it within the app.

Hamata6 avatar Feb 07 '23 09:02 Hamata6

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

ritesh-burn avatar Feb 16 '23 02:02 ritesh-burn

@samhouts it must be backported to .NET 7

ederbond avatar Apr 04 '23 15:04 ederbond

Hello lovely human, thank you for your comment on this issue. Because this issue has been closed for a period of time, please strongly consider opening a new issue linking to this issue instead to ensure better visibility of your comment. Thank you!

ghost avatar Apr 04 '23 15:04 ghost