I have a Xamarin forms app targeting .NET Standard 2.0 and running on a physical Android 10 device.
I’m trying to send emails through the app using the following code:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.office365.com");
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Message Subject";
mail.Body = "Message Body";
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("****", "******");
SmtpServer.EnableSsl = true;
SmtpServer.TargetName = "STARTTLS/smtp.office365.com";
SmtpServer.Send(mail);
The email fails to send and I receive the following error message
“System.IO.IOException: Authentication failed because the remote party has closed the transport stream”
Android 10 should be using TLS 1.2 already, but to be sure I tried specifying in code but results are the same
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
As a test I tried using a gmail server and the email was delivered without issue, but I cannot get office365 to work.