I had a solution to retrieve the user password when the user forgot the password. I did the code well, but the error appear with an SMTP exception (error sending). How can I fix this problem?
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection con = Connection.GetConnection())
{
string sql = "Select Password From Registeration Where UserName=@UserName And Email=@Email";
SqlCommand com = new SqlCommand(sql, con);
com.CommandType = CommandType.Text;
com.Parameters.Add("@UserName", SqlDbType.NVarChar, 50).Value = TxtUserName.Text;
com.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = TxtEmail.Text;
SqlDataReader dr = com.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.CloseConnection);
while (dr.Read())
{
SendMail("karim-gamal@elarabygroup.com", "xxxx", TxtEmail.Text, " Hi", "Hi" + dr["Password"].ToString());
}
Response.Redirect("");
}
}
public static bool SendMail(string elarabyAccount, string password, string to, string subject, string message)
{
try
{
NetworkCredential loginInfo = new NetworkCredential(elarabyAccount, password);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(elarabyAccount);
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.elarabygroup.com", 8080);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
return true;
}
catch (Exception)
{
return false;
}
}
The documentation for
SmtpClassstates the following (emphasis mine):Since you’re using encryption (
client.EnableSsl = true;) you should try to use a singleSmtpClientinstance to send your mails.Also, by creating a new instance of
SmtpClienton every call ofSendMail, you’re creating a new connection to the SMTP server; depending on the number of records you have, and thus the number of emails you want to send, it’s possible that the SMTP server refuses to accept any connections from you because of the high quantity, since it may think that you’re either issuing a DDoS attack or you’re trying to do other malicious things.As already mentioned above, just try to use a single
SmtpClientinstance.Another possible issue could be that the
SmtpClientis not quitting the connection correctly by not issuing aQUITcommand. This is a known bug, and is fixed in the Microsoft .NET Framework 4.