Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send mail via System.Net.Mail.SmtpClient isn't working on .NET MAUI Android, but on .NET MAUI Windows and Xamarin.Forms Android #74576

Closed
tsjdev-apps opened this issue Aug 25, 2022 · 16 comments

Comments

@tsjdev-apps
Copy link

Description

I've created a small sample trying to send an email using the System.Net.Mail.SmtpClient. The sample is working on Windows, but not on Android. An Interop+AndroidCrypto+SslException is thrown. The same code is working on Xamarin.Forms for both platforms.

Reproduction Steps

  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

Expected behavior

Mail was sent on Android

Actual behavior

Interop+AndroidCrypto+SslException is thrown

Regression?

The same code is working on Xamarin.Forms for Android and iOS.

Known Workarounds

No response

Configuration

Which version of .NET is the code running on?
.NET MAUI Version: 6.0.486 (current)

What OS and version, and what distro if applicable?
Android

What is the architecture (x64, x86, ARM, ARM64)?
ARM, ARM64

Other information

{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}
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 25, 2022
@ghost
Copy link

ghost commented Aug 25, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I've created a small sample trying to send an email using the System.Net.Mail.SmtpClient. The sample is working on Windows, but not on Android. An Interop+AndroidCrypto+SslException is thrown. The same code is working on Xamarin.Forms for both platforms.

Reproduction Steps

  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

Expected behavior

Mail was sent on Android

Actual behavior

Interop+AndroidCrypto+SslException is thrown

Regression?

The same code is working on Xamarin.Forms for Android and iOS.

Known Workarounds

No response

Configuration

Which version of .NET is the code running on?
.NET MAUI Version: 6.0.486 (current)

What OS and version, and what distro if applicable?
Android

What is the architecture (x64, x86, ARM, ARM64)?
ARM, ARM64

Other information

{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}
Author: tsjdev-apps
Assignees: -
Labels:

area-System.Net

Milestone: -

@tsjdev-apps
Copy link
Author

I've also create an issue in the .NET MAUI repository:
dotnet/maui#9587

Maybe also related to:
dotnet/android#7277

@ghost
Copy link

ghost commented Aug 25, 2022

Tagging subscribers to 'arch-android': @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I've created a small sample trying to send an email using the System.Net.Mail.SmtpClient. The sample is working on Windows, but not on Android. An Interop+AndroidCrypto+SslException is thrown. The same code is working on Xamarin.Forms for both platforms.

Reproduction Steps

  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

Expected behavior

Mail was sent on Android

Actual behavior

Interop+AndroidCrypto+SslException is thrown

Regression?

The same code is working on Xamarin.Forms for Android and iOS.

Known Workarounds

No response

Configuration

Which version of .NET is the code running on?
.NET MAUI Version: 6.0.486 (current)

What OS and version, and what distro if applicable?
Android

What is the architecture (x64, x86, ARM, ARM64)?
ARM, ARM64

Other information

{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}
Author: tsjdev-apps
Assignees: simonrozsival
Labels:

area-System.Net, os-android, untriaged

Milestone: -

@simonrozsival simonrozsival added this to the 8.0.0 milestone Aug 25, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Aug 25, 2022
@steveisok
Copy link
Member

@tsjdev-apps What version of Android / api level are you using?

@simonrozsival
Copy link
Member

I tried to reproduce this problem on my device and the message was successfully sent (Samsung Galaxy S10 Lite, Android 12)

@ComptonAlvaro
Copy link

I have debugging the the MAUI application in a android device (a phone) and it works perfectly.
photo_2022-08-25_17-01-11

I am using ionos as priveder, instead of outlook, but I guess the problem is with the data that you use to ahutenticate.

@ComptonAlvaro
Copy link

ComptonAlvaro commented Aug 25, 2022

I have with outlook.com account (really a hotmail account) and it works too, using this data:

var smtpClient = new SmtpClient("smtp-mail.outlook.com", 587)
            {
                Port = 587,
                Credentials = new NetworkCredential("xxx@hotmail.com", "YYYY"),
                EnableSsl = true,
            };

EDIT: I forget to delete the port in the set, because I passed it as paramter in the constructor, but I have tried to comment this line and it still work.

@janseris
Copy link

Android 5 should be tested as it is the lowest supported Android version by MAUI

@tsjdev-apps
Copy link
Author

I've tried now on several real devices and it is working.
Before I was testing on different emulators and on the emulators I recieve the exception.

@wfurt
Copy link
Member

wfurt commented Aug 26, 2022

firewall setting....?

@simonrozsival
Copy link
Member

I've done some further testing on emulators with various API levels and encrypted SMTP consistently worked on API levels 28 and newer and failed on 26 and lower. For some reason emulator with API 27 didn't work on my PC today so I couldn't test it.

I'm not sure what exactly the problem is, but it seems that the problem is support on lower API levels and not emulator vs. device or firewall settings.

@janseris
Copy link

janseris commented Aug 26, 2022

@simonrozsival
Your result exactly matches API levels with issues with SSL when connecting to SQL Server. It works for Android 9+ which is API 28+ and fails for all lower versions.
dotnet/SqlClient#1656

@janseris
Copy link

I've tried now on several real devices and it is working. Before I was testing on different emulators and on the emulators I recieve the exception.

What Android version?

@tsjdev-apps
Copy link
Author

I've tried now on several real devices and it is working. Before I was testing on different emulators and on the emulators I recieve the exception.

What Android version?

I've tested with Android 11 and Android 12.

@janseris
Copy link

I've tried now on several real devices and it is working. Before I was testing on different emulators and on the emulators I recieve the exception.

What Android version?

I've tested with Android 11 and Android 12.

Could you test Android 8.1 or lower?

@tsjdev-apps
Copy link
Author

I've tried now on several real devices and it is working. Before I was testing on different emulators and on the emulators I recieve the exception.

What Android version?

I've tested with Android 11 and Android 12.

Could you test Android 8.1 or lower?

Unfortunately I don't have access to an Android device running Android 8.1 or lower.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants