Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error sending email

Tags:

c#

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("[email protected]", "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;
    }
}
like image 803
Myworld Avatar asked Mar 10 '26 18:03

Myworld


1 Answers

The documentation for SmtpClass states the following (emphasis mine):

The connection established by the current instance of the SmtpClient class to the SMTP server may be re-used if an application wishes to send multiple messages to the same SMTP server. This is particularly useful when authentication or encryption are used establish a connection to the SMTP server.

Since you're using encryption (client.EnableSsl = true;) you should try to use a single SmtpClient instance to send your mails.

Also, by creating a new instance of SmtpClient on every call of SendMail, 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 SmtpClient instance.

Another possible issue could be that the SmtpClient is not quitting the connection correctly by not issuing a QUIT command. This is a known bug, and is fixed in the Microsoft .NET Framework 4.

like image 108
Giuseppe Accaputo Avatar answered Mar 12 '26 08:03

Giuseppe Accaputo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!