dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.27k stars 1.76k forks source link

Send mail via System.Net.Mail.SmtpClient isn't working on Android, but on Windows #9587

Closed tsjdev-apps closed 2 years ago

tsjdev-apps commented 2 years ago

Description

I've created a small sample trying to send an email using theSystem.Net.Mail.SmtpClient. The sample is working on Windows, but not on Android. An Interop+AndroidCrypto+SslException is thrown.

Steps to Reproduce

  1. Create a File > New .NET MAUI App
  2. Create a System.Net.Mail.SmtpClient and calling the SendMailAsync() on Android
  3. An Interop+AndroidCrypto+SslException is thrown
  4. Run the same code on Windows and the email is sent

I've tried using an outlook.com mail address with the smtp-mail.outlook.com host.

See the following basic sample: MauiSendMailSample.zip

Version with bug

6.0.486 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android

Did you find any workaround?

No response

Relevant log output

{System.Net.Mail.SmtpException: Failure sending mail.
 ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
 ---> Interop+AndroidCrypto+SslException: Exception of type 'Interop+AndroidCrypto+SslException' was thrown.
   --- End of inner exception stack trace ---
   at System.Net.Security.SslStream.<ForceAuthenticationAsync>d__175`1[[System.Net.Security.AsyncReadWriteAdapter, System.Net.Security, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].MoveNext()
   at System.Threading.Tasks.TaskToApm.End(IAsyncResult asyncResult)
   at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
   at System.Net.TlsStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
   at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.TlsStreamAuthenticateCallback(IAsyncResult result)
--- End of stack trace from previous location ---
   at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result)
   at System.Net.Mail.SmtpConnection.EndGetConnection(IAsyncResult result)
   at System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult result)
   at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)
   --- End of inner exception stack trace ---
   at MauiSendMailSample.MainPage.OnMailButtonClicked(Object sender, EventArgs e) in MauiSendMailSample\MauiSendMailSample\MainPage.xaml.cs:line 36}
janseris commented 2 years ago

Seems there are SSL issues with Android in. NET in multiple technologies. Adding to my list, it is currently: gRPC, WebSocket, SQL Server and SMTP

https://github.com/xamarin/xamarin-android/issues/7277

jfversluis commented 2 years ago

As pointed out this seems to be at least a layer deeper on the Android bindings or maybe even on the plugin/.NET runtime in this case. Unfortunately this is not something that we can do something about in the .NET MAUI project.

janseris commented 2 years ago

@jfversluis can you give a tip how to solve that? Where to report etc.? Thank you

tsjdev-apps commented 2 years ago

I just tested the same sample on Xamarin.Forms and on Android/UWP it is working like expected. Only .NET MAUI Android isn't working as expected.

MohammdaSalah commented 2 years ago

So any workaround to this?

knocte commented 2 years ago

I just tested the same sample on Xamarin.Forms and on Android/UWP it is working like expected. Only .NET MAUI Android isn't working as expected.

If this is true then this issue needs to be reopened @jfversluis

tsjdev-apps commented 2 years ago

I just tested the same sample on Xamarin.Forms and on Android/UWP it is working like expected. Only .NET MAUI Android isn't working as expected.

If this is true then this issue needs to be reopened @jfversluis

For me this exception only got raised if I'm running the sample on an emulator for .NET MAUI. If I'm running the code on a real device (I've tested with Android 11 and Android 12) it is also running.

The Xamarin version is running on an emulator and on a real device.

dmsch commented 2 years ago

This issue also occurs with SignalR on Android 8.0. With Android 9.0 and above, SignalR works.

janseris commented 2 years ago

This issue also occurs with SignalR on Android 8.0. With Android 9.0 and above, SignalR works.

Could you create a new issue? I think @jfversluis doesn't plan to reopen this one I'll be happy to track it because this seems like a much broader problem with Android SSL providers