I am having this error when sending an email from the android App in .Net MAUI Version 6.0, before my code was working perfectly but now it is giving the error said:Error sending email: The AMTP server requires a secure connection or client was not authenticated. the server response was: 5.7.0 Authentication Required But when i try to run the project in windows the code is working perfectly but in android it's failing.
public void Mail(string filePath)
{
try
{
var message = new MailMessage();
message.To.Add("example@gmail.com");// replace with the recipient email address
message.From = new MailAddress("example@gmail.com");// replace with the sender email address
message.Subject = "Black Soldier Fly Farming Repport";
message.Body = "My Body";
// create a new attachment from a file
var attachment = new Attachment(filePath);
// add the attachment to the email message
message.Attachments.Add(attachment);
using (var client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587))
{
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("example@gmail.com", "examplePass");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Timeout = 5000;
client.Send(message);
DisplayAlert($"Uploading Repport:", "Repport Submitted!", "Okay");
}
}
catch (Exception ex)
{
// handle any errors that occur
DisplayAlert($"Error sending email:", ex.Message, "Okay");
}
}