Reputation: 147
I have login page.login is working fine but when trying to create new user, I'm getting
Error:The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
here is code which I am using to send mail
public void SendConfirmationAfterRegister(String EmailID, String UserName)
{
MailMessage mailMsg = new MailMessage();
String BodyMsg = UserName + ", \r\n\nWe have recieved your request to become a user of our site. Upon review, we will send you verification for site access.\r\n\n" +
"Thank you, \r\n\nMuda Admin";
try
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential(ConfigurationManager.AppSettings["AdminEmail"].ToString(), ConfigurationManager.AppSettings["Pwd"].ToString()),
EnableSsl = true
};
mailMsg.IsBodyHtml = true;
client.Send(ConfigurationManager.AppSettings["AdminEmail"].ToString(), EmailID, "Received Your Request", BodyMsg);
client.Send(ConfigurationManager.AppSettings["AdminEmail"].ToString(), "mymailId", "User Needs Access", EmailID + " looking for access the xyz Network Site.");
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
can any one what is wrong
Upvotes: 0
Views: 356
Reputation: 7107
Try like this...
using System.Net.Mail;
String BodyMsg = UserName + ", \r\n\nWe have recieved your request to become a user of our site. Upon review, we will send you verification for site access.\r\n\n" +
"Thank you, \r\n\nMuda Admin";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(ConfigurationManager.AppSettings["AdminEmail"].ToString());
mail.To.Add("[email protected]");
mail.Subject = "subject";
mail.Body = BodyMsg ;
SmtpServer.UseDefaultCredentials=true
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["AdminEmail"].ToString(), ConfigurationManager.AppSettings["Pwd"].ToString());
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Upvotes: 0
Reputation: 38538
Try this:
public void SendConfirmationAfterRegister(String EmailID, String UserName)
{
MailMessage mailMsg = new MailMessage();
String BodyMsg = UserName + ", \r\n\nWe have received your request to become a user of our site. Upon review, we will send you verification for site access.\r\n\n" +
"Thank you, \r\n\nMuda Admin";
try
{
using (var client = new SmtpClient("smtp.gmail.com", 587))
{
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["AdminEmail"].ToString(), ConfigurationManager.AppSettings["Pwd"].ToString());
client.EnableSsl = true;
mailMsg.IsBodyHtml = true;
client.Send(ConfigurationManager.AppSettings["AdminEmail"].ToString(), EmailID, "Received Your Request", BodyMsg);
client.Send(ConfigurationManager.AppSettings["AdminEmail"].ToString(), "mymailId", "User Needs Access", EmailID + " looking for access the xyz Network Site.");
}
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
You need to set UseDefaultCredentials
to false
.
Note: You MUST set UseDefaultCredentials
to false
before setting the Credentials
property.
Upvotes: 1