C# 发送电子邮件时出错

C# 发送电子邮件时出错,c#,C#,我有一个解决方案,当用户忘记密码时检索用户密码。我的代码编写得很好,但是出现了一个SMTP异常(发送错误)。我如何解决这个问题 protected void Button1_Click(object sender, EventArgs e) { using (SqlConnection con = Connection.GetConnection()) { string sql = "Select Password From Registeration Where

我有一个解决方案,当用户忘记密码时检索用户密码。我的代码编写得很好,但是出现了一个SMTP异常(发送错误)。我如何解决这个问题

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;
    }
}
缔约国声明如下(强调我国):

由 SmtpClient的当前实例 SMTP服务器的类可能是 如果应用程序希望 向同一台计算机发送多条消息 SMTP服务器当使用身份验证或加密来建立到SMTP服务器的连接时,此功能特别有用。

由于您使用的是加密(
client.enablesssl=true;
),因此您应该尝试使用单个
SmtpClient
实例来发送邮件

另外,通过在每次调用
SendMail
时创建
SmtpClient
的新实例,您正在创建到SMTP服务器的新连接;根据您拥有的记录的数量,以及您想要发送的电子邮件的数量,SMTP服务器可能会因为数量太多而拒绝接受您的任何连接,因为它可能认为您正在发起DDoS攻击或尝试进行其他恶意操作

如上所述,只需尝试使用单个
SmtpClient
实例


另一个可能的问题可能是
SmtpClient
未通过发出
QUIT
命令而正确退出连接,在Microsoft.NET Framework 4中已修复。

发布错误/异常详细信息如何?顺便说一句,我无法解析
smtp.elarabigroup.com
。您用于测试的计算机是否可以使用此主机?它应该改为
mail.elarabigroup.com
吗?我在公司网络上工作,通过outlook或mail.elarabigroup.com登录我的电子邮件