The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.
My project is uploaded to a remote server where it sends this particular error but it runs at local host for about sending mails.I also allowed less secure app on the gmail account but still not able to understand.
What I have tried:
public void SendMail(string strTO, string strSubject, string strBody, FileUpload objFileUpload = null)
{
try
{
tblSettings objSettings = new tblSettings();
objSettings.LoadAll();
if (objSettings.RowCount > 0)
{
string strFrom = objSettings.AppSiteEmail;
if (strTO == "")
{
strTO = objSettings.AppRecepientEmail;
}
MailMessage mail = new MailMessage(strFrom, strTO, strSubject, strBody);
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = objSettings.AppSMTP;
if (!string.IsNullOrEmpty(objSettings.s_AppPortNumber))
{
smtp.Port = Convert.ToInt32(objSettings.AppPortNumber.ToString());
}
smtp.UseDefaultCredentials = false;
smtp.Timeout = 60000;
clsEncryption objEncrypt = new clsEncryption();
smtp.Credentials = new System.Net.NetworkCredential(strFrom, objEncrypt.Decrypt(objSettings.AppEmailPassword, appFunctions.strKey));
if (objSettings.s_AppIsSSL != "")
{
smtp.EnableSsl = objSettings.AppIsSSL;
}
else
{
smtp.EnableSsl = false;
}
string exe = " MailTO: " + strTO + " ,MailFrom: " + strFrom + " ,Subject :" + strSubject + " ,Body: " + strBody + " ,Host: " + smtp.Host + " ,Port: " + smtp.Port + " ,SSL: " + smtp.EnableSsl;
LogError(exe);
smtp.Send(mail);
smtp = null;
mail = null;
}
}
catch (Exception ex)
{
string message = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
message += Environment.NewLine;
message += "-----------------------------------------------------------";
message += Environment.NewLine;
message += string.Format("Message: {0}", ex.Message);
message += Environment.NewLine;
message += string.Format("StackTrace: {0}", ex.StackTrace);
message += Environment.NewLine;
message += string.Format("Source: {0}", ex.Source);
message += Environment.NewLine;
message += string.Format("TargetSite: {0}", ex.TargetSite.ToString());
message += Environment.NewLine;
message += "-----------------------------------------------------------";
message += Environment.NewLine;
string path = HttpContext.Current.Server.MapPath("~/Error/ErrorLog1.txt");
if (!File.Exists(path))
{
File.Delete(path);
var myFile = File.Create(path);
myFile.Close();
System.IO.File.WriteAllText(path, message);
myFile.Close();
return;
}
if (File.Exists(path))
{
File.Delete(path);
var myFile = File.Create(path);
myFile.Close();
System.IO.File.WriteAllText(path, message);
myFile.Close();
return;
}
}
}