I have an asp.net web API that is used to send emails to another email (gmail), and another frontend project calls that API, and it works perfect it sends emails as expected when the backend & frontend projects are locally, but it doesn't work on live hosting (SmarterAsp).
Here's my conflagration and code:
//This method only sends email as per the data sent
[System.Web.Http.HttpPost]
public EmailResponseModel SendEmail()
{
NetworkCredential basicCredential =
new NetworkCredential("mysenderemail@hotmail.com", "mysenderemailpassword");
MailMessage ProviderMail = new MailMessage();
ProviderMail.From = new MailAddress("mysenderemail@hotmail.com");
ProviderMail.To.Add("myemail@gmail.com");
ProviderMail.Subject = "Title";
ProviderMail.IsBodyHtml = true;
ProviderMail.Body = "Body";
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Host = "smtp.office365.com";
smtp.Credentials = basicCredential;
smtp.EnableSsl = true;
try
{
smtp.Send(ProviderMail);
return Response;
}
catch (Exception ex)
{
return Response;
}
finally
{
smtp.Dispose();
}
}
And here the error that I got from the API:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP
I tried to do many things such as:
- Tried these hosts smtp.live.com, smtp.office365.com, smtp-mail.live.com and others
- Tried other ports such as 25
- Tried to make this UseDefaultCredentials as false and true
- Made sure that the User name and Pass are correct
- Made sure from the order of SMPT Config code
I spent hours on this, but I not fining any solution! I need any insight.