I have the following action method to send emails using gmail smtp:-
[HttpPost]
public ActionResult Contact(Contact c)
{
//
if (ModelState.IsValid)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("*****");
mail.To.Add(new MailAddress("******"));
mail.Subject = "New Feedback from Edama Web Site";
mail.IsBodyHtml = true;
System.Text.StringBuilder mailBody = new System.Text.StringBuilder();
mailBody.AppendLine("<b>Dear Sirs, </b><br/><br/>");
mail.Body = mailBody.ToString();
// Create a new Smpt Client using Google's servers
var mailclient = new SmtpClient();
mailclient.Host = "smtp.gmail.com";
mailclient.Port = 587;
// This is the critical part, you must enable SSL
mailclient.EnableSsl = true;
mailclient.Credentials = new System.Net.NetworkCredential
("***", "***"); /
//Or your Smtp Email ID and Password
//smtp.EnableSsl = true;
mailclient.Send(mail);
TempData["message"] = string.Format("Your Feedback has been submitted. Thanks");
return RedirectToAction("Contact");
}
return View(c);
}
now when i am running my web application under VS 2013 everything will work well , where i will receive emails correctly. but now i deploy my web application under a hosting server , and when i call the above action method i got the following error:-
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
so what is causing this error ?
second question. i am a bit confused , as inside my action method i define the following mailclient.EnableSsl = true;
does this mean that my web application should be running under https
which is not my case, as my web application is a public web site so it is not under https .. and as i mentioned before when i call my action method inside VS 2013 on for example localhost:1234
the smtp send email correctly even i am not running my localhost under https ...