Xamarin - SMPT OFFICE365: sending emails has stopped working for me
I developed an application with xamarin, a few months ago, to send emails, here a piece of my code:
public Email(string To, string CC, string Subject, string Body)
Server = new SmtpClient
{
Host = "smtp.office365.com",
Port = 587,
EnableSsl = true,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("no-reply@contoso.com", "pass")
};
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
From = "no-reply@contoso.com";
this.To = To;
this.CC = CC;
this.Subject = Subject;
this.Body = Body;
}
public void SendEmail()
{
try
{
var correo = new MailMessage();
correo.From = new MailAddress(From);
correo.To.Add(To);
if (!string.IsNullOrEmpty(CC))
correo.CC.Add(CC);
correo.Subject = Subject;
correo.IsBodyHtml = true;
correo.Body = Body;
//Enable suport TLS1.2
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Server.EnableSsl = true;
Server.SendCompleted += (s, e) => Server.Dispose();
Server.Send(correo);
}
catch (Exception ex)
{
Device.BeginInvokeOnMainThread(async () => { await Application.Current.MainPage.DisplayAlert("Error al enviar correu", $"Message: {ex.Message}\n\n Inner Exception: {ex.StackTrace}", "Ok"); });
}
}
This app has worked correctly so far. Now I'm getting and error when I'm trying to send an email.
Here a trace from the error:
"System.Net.Mail.SmtpException: Message could not be sent. ---> System.IO.IOException: Authentication failed because the remote party has closed the transport stream.\n at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation......."
I was looking through several forums to get with the response, but I was unable to find it. A lot of answer were related with use TLS12, but I'm currently using it.
Any help?
Thanks in advance