Error sending email

I had a decision to get the user password when the user forgot the password. I did the code well, but the error appears with the exception of SMTP (sending error). 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;
    }
}
+3
source share
1 answer

The documentation forSmtpClass indicates the following (primary focus):

The connection established by the current instance of the SmtpClient class for the SMTP server can be reused if the application wants to send multiple messages to the same SMTP server. This is especially useful when authentication or encryption is used, establish a connection to the SMTP server.

(client.EnableSsl = true;), SmtpClient .

, SmtpClient SendMail, SMTP-; , , , , , , SMTP - - , , DDoS- .

, SmtpClient.

, SmtpClient , QUIT. ​​ Microsoft.NET Framework 4.

+1

Source: https://habr.com/ru/post/1758158/


All Articles