Reputation: 21
I want to send users who press a button in my Uno Platform (Xamarin) Android mobile app to a pre-populated email with the body and subject passed as a parameter, so as to automate the process of sending a report.
The app uses the MVVM pattern, the button in the View is bound to a command in the ViewModel which puts together some strings and tries to send them along with the user to their email app of choice. Nothing overtly complex.
Since Android 13 the following piece of code causes the app to consistently crash (which it didn't do earlier)
var message = new Xamarin.Essentials.EmailMessage
{
Subject = Report.SUBJECT,
Body = Report.DATA
To = new List<string> { Report.RECIPIENT }, // "[email protected]"
};
await Xamarin.Essentials.Email.ComposeAsync(message);
Stack trace yields the following
ApplicationActivity+<>c__DisplayClass29_0.<SetContentView>b__0 (System.Object s, Android.Views.View+ViewAttachedToWindowEventArgs e)
System.NullReferenceException: Object reference not set to an instance of an object
which is weird. The message is not null (seen in the debugger). Also, despite the app crashing, the message itself is correctly generated in the chosen email app (if I navigate there manually after my app crashed, that is)
Minimum Android version for the app is Android 9 (API level 28), target is 11 (API 30). The newest .NET Core SDK is 6.0.101, and .NET Core Runtime is 6.0.1, as seen in the SDK manager. I'm working in Visual Studio 2019 for Mac community edition version 8.10.22 (build 11)
How do I deal with this?
What I tried so far:
Replacing ComposeAsync with Launcher.OpenAsync results in the same failure.
var uri = $"mailto:{Report.RECIPIENT}?subject={Report.SUBJECT}&body={Report.DATA}";
await Xamarin.Essentials.Launcher.OpenAsync(uri);
In fact, even just going to some example website turns out to crash in the same way
var uri = "https://example.com";
await Xamarin.Essentials.Launcher.OpenAsync(uri);
Wrapping the whole method (regardless of launcher or email) in a try-catch statement fails to catch anything and app crashes nonetheless
private async void SendReport()
{
try
{
// Do the thing
}
catch (Exception ex)
{
// Some exception occurred
}
}
Elsewhere in the app, using a hyperlink button in the View (instead of a ViewModel method) surprisingly does work normally
<HyperlinkButton
NavigateUri="mailto:[email protected]">
<TextBlock Text="Contact via e-mail"/>
</HyperlinkButton>
which suggests that it is Xamarin.Essentials that isn't working for me, not just emails in Android.
I would much prefer solving the issue in the SendReport method in the ViewModel instead of resorting to that one weird trick that programmers hate (of switching to a hyperlink button in UI) to make it work.
Documentation about Android 13 behaviour changes to all apps doesn't seem to contain any changes of obvious relevance to this issue.
iOS in contrast works fine this time
This desperate attempt didn't work out of the box.
System.Diagnostics.Process.Start("http://www.c-sharpcorner.com/Default.aspx");
My environment is quite fragile which is why I'm highly hesitant to try updating anything as a rule
Upvotes: 2
Views: 1228
Reputation: 11
I had the same issue you have described (limited to Android 13) with only one difference: using the try-catch statement I trap an error:
{Xamarin.Essentials.FeatureNotSupportedException: Specified method is not supported. at Xamarin.Essentials.Email.ComposeAsync Xamarin.Essentials.EmailMessage message) [0x00007] in D:\a_work\1\s\Xamarin.Essentials\Email\Email.shared.cs:19 at .... }
I did solve it by adding into the AndroidManifest.xml the following section:
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
</queries>
Upvotes: 1