jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.
http://www.mimekit.net
MIT License
6.14k stars 816 forks source link

MailKit not sending out mails for all newer android phones even though android emulator for affected version does... #1557

Closed aproko234 closed 1 year ago

aproko234 commented 1 year ago

Hello,

I am having an issue with using Mailkit in my Xamarin.Forms App.

Some newer Android Phones especially from Android 11 upwards (different manufacturers) are not sending out mails (I am checking my mail server email logs and there are no interractions), but in the relevant android emulator, the mails are being sent out and delivered.

But, for most Android 9 and below, the mails are going out and being delivered. However, a Huawei Android 9 Phone I just installed for, failed to send a mail.

This is the code snippet:

                        client.ServerCertificateValidationCallback = (s, c, h, e) => true; 

                        client.Connect("mail.2kks.com.ng", 587, SecureSocketOptions.StartTls); 

                        client.Capabilities &= ~SmtpCapabilities.Pipelining;

                        client.Authenticate(username, password);  

                        client.Send(message);

And then a Vivo Android 12 Phone I installed for, is working fine. But, an Infinix Android 12 Phone is not sending out mails.

I have even updated my Mailkit library (and MimeKit Library) to version 4.00.

So, why is this issue happening for the same Android Version and different phone makers, even though the android emulator for each version works?

jstedfast commented 1 year ago

It'll be impossible for me to debug this without the proper hardware. Would you be willing to send me a phone or the money to buy a phone to test this on?

aproko234 commented 1 year ago

It'll be impossible for me to debug this without the proper hardware. Would you be willing to send me a phone or the money to buy a phone to test this on?

That might be a little difficult for me to do right now. However, during my chat with the Bing AI, it alluded that since my android emulators are sending the mails, maybe the real devices not doing so, might be lacking the latest system update. I would like to confirm that and then revert ASAP. Thanks.

jstedfast commented 1 year ago

Yes, that sounds like it would be worth trying.

aproko234 commented 1 year ago

Hi.

I have updated some of the phones to their latest system update and the issue persists in several of them. However, I then discovered that the Android 12 emulator sends out SMTP mails (using MailKit) when it is running as a debugged version but if it is a release version, the issue is there (that is, once it gets to client.Connect("mail.2kks.com.ng", 587, SecureSocketOptions.StartTls), it doesnt continue from there and my mail server does not receive any sign of a connection attempt).

Can you please confirm this last information (Android 12 emulator debug v release) at your end?

I am suspecting the issue might have to do with the Nuget (Xamarin.AndroidX.AppCompat.AppCompatResources Library by Microsoft). It is presently in version 1.6.1 but if I upgrade to it from 1.5.1.1, it will not successfully build a release version and it would complain of a linker issue. But, if I revert back to version 1.5.1.1, it compiles the release build successfully.

I am using VS 2022 and my Project is based on Xamarin.forms.

Thanks.

jstedfast commented 1 year ago

Can you create a test project? I tried to reproduce this yesterday but had no success (but maybe because my project didn't actually use Xamarin.AndroidX.AppCompat.AppCompatResources? I have no idea)

From your description, it sounds more like a Xamarin.Forms/Android bug than a MailKit bug.

Also, FWIW, Microsoft is phasing out Xamarin.Forms in favor of MAUI.

aproko234 commented 1 year ago

Ok.

aproko234 commented 1 year ago

It is all well and good that Microsoft is phasing out Xamarin.forms in favour of Maui, but trying to upgrade my app to Maui is causing me so much pain right now that I feel resolving my issue would be best served under xamarin.forms for now. Why would different (manufacturer) models of the same android version (11, 12 7 13) behave differently with regards to sending emails using mailkit meanwhile all those devices send and receive emails using the in-built gmail app?

jstedfast commented 1 year ago

The built-in GMail app doesn't use the IMAP or SMTP protocols, it uses Google's HTTP REST API for GMail.

Anyway, I'm willing to take a look, but I need a sample project.

aproko234 commented 1 year ago

I am noticing an issue that Xamarin Essentials observed with Android 11. In this link https://learn.microsoft.com/en-us/xamarin/essentials/email?tabs=android, this is an excerpt: "If your project's Target Android version is set to Android 11 (R API 30) you must update your Android Manifest with queries that are used with the new package visibility requirements."

Did MailKit take this into consideration because this is the message I am currently seeing on my mail server logs when an android 11 or higher device (including the emulator in release mode) tries to send a mail as per my initial code above: Apr 26 20:14:39 aproko2kks postfix/smtpd[961477]: connect from unknown[141.98.11.83] Apr 26 20:14:42 aproko2kks postfix/smtpd[961477]: warning: unknown[141.98.11.83]: SASL PLAIN authentication failed: Apr 26 20:14:42 aproko2kks postfix/smtpd[961477]: disconnect from unknown[141.98.11.83] ehlo=1 auth=0/1 quit=1 commands=2/3

Thanks.

jstedfast commented 1 year ago

MailKit doesn't have any Android-specific code.

I don't know much about Android development, but it doesn't look like your link is at all related to whatever issue you are experiencing.

The link you pasted is talking about an Android API that allows you to open the "Email Composer" of the default Mail app on the Android phone.

MailKit doesn't do that.

The log you posted shows MailKit successfully connected but is failing to authenticate with SASL PLAIN authentication. Probably a bad password or something.

jstedfast commented 1 year ago

It just dawned on me that most likely the issue you are hitting is with text encodings for your password.

Are you calling this snippet of code at program startup?

System.Text.Encoding.RegisterProvider (System.Text.CodePagesEncodingProvider.Instance);
aproko234 commented 1 year ago

No.

jstedfast commented 1 year ago

Try adding that snippet and see if that solves your issue. If not, there might be an issue in newer versions of Xamarin.Android (or your build config?) where the I18N assemblies aren't being included in your apk.

It's been years since I have worked on anything Android, but at one time, the Xamarin.Android build configuration required you to opt-in to the inclusion of non-unicode text encodings in the Project Options in one of the Android-specific build properties pages. You might need to make sure you are doing that as well.

jstedfast commented 1 year ago

Also worth mentioning is that Microsoft will be including a tool in Visual Studio 17.7 (I believe?) that can upgrade Xamarin.Forms apps to MAUI (well, more realistically it will do a lot of the common stuff, but it won't be able to magically do everything that might be needed).

There's also an older tool available for the command-line that can do the upgrade here: https://github.com/dotnet/upgrade-assistant/

I don't think that updating to MAUI will necessarily solve this particular issue, I'm just trying to offer solutions that might help you in your migration when you decide to update.

The newer tool that will be built into Visual Studio will be able to do more with fixing up XAML than the command-line tool, so it may be worth waiting depending on how complicated your app is.

aproko234 commented 1 year ago

Ok. I will add the snippet to my MainActivity.cs and test again. Thanks.

jstedfast commented 1 year ago

Have you managed to solve this issue? Is there anything you still need from me?