-1
public partial class ForgotPassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnPass_Click(object sender, EventArgs e)
    {
        //Create Connection String And SQL Statement
        string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string strSelect = "SELECT UserName,Password FROM Users WHERE Email = @Email";

        SqlConnection connection = new SqlConnection(strConnection);
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.Text;
        command.CommandText = strSelect;

        SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar, 50);
        email.Value = txtEmail.Text.Trim().ToString();
        command.Parameters.Add(email);

        //Create Dataset to store results and DataAdapter to fill Dataset 
        DataSet dsPwd = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter(command);
        connection.Open();
        dAdapter.Fill(dsPwd);
        connection.Close();
        if(dsPwd.Tables[0].Rows.Count > 0 )
        {

            MailMessage loginInfo = new MailMessage();
            loginInfo.To.Add(txtEmail.Text.ToString());
            loginInfo.From = new MailAddress("YourID@gmail.com");
            loginInfo.Subject = "Forgot Password Information";

            loginInfo.Body = "Username: " + dsPwd.Tables[0].Rows[0]["UserName"] + "<br /><br />Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "<br /><br />";
            loginInfo.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; 
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("YourGmailID@gmail.com", "YourGmailPassword");
            smtp.Send(loginInfo);
            lblMessage.Text = "Password is sent to you email id,you can now <a href='Login.aspx'>Login</a>";
        }
        else
        {
            lblMessage.Text = "Email Address Not Registered";
        }

    }
}

我遇到了错误 smtpException is unhandled by user code ...如何解决?

4

4 回答 4

2

很简单,写一些用户代码来处理它!

如果您不熟悉 Try..Catch 块,我建议您阅读它们,并使用它们(在适当的情况下)处理异常情况(即邮件服务器离线、网络连接问题、密码过期)。

导致您的异常的代码很可能是这一行,它应该给您一个开始的地方:

smtp.Send(loginInfo);
于 2012-09-27T11:14:39.623 回答
0

问题是您使用了错误的端口号。用于通过 SSL 的 Gmail。从文档中:

Outgoing Mail (SMTP) Server - requires TLS3 or SSL: smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465

由于您将EnableSsl选项设置为 true,因此您的端口号应设置为465.

于 2012-09-27T11:26:47.503 回答
0

您应该在用户代码中处理异常:

try
{
    smtp.Send(loginInfo); 
}
catch (Exception ex)
{
    //Handle your exception here
    lblMessage.Text = "Oeps, something when wrong when we tried to send the email";
    return;
}
于 2012-09-27T11:15:20.037 回答
-1

错误:邮件未发送 - 用户代码未处理 smtp 异常

if (ModelState.IsValid)
        {
            try{
            string from = "abcd@gmail.com";
            using (MailMessage mail = new MailMessage(from, obj.Idemail))
            {
                mail.Subject = "Admin";
                mail.Body = "Registration successful!";
                mail.IsBodyHtml = false;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential networkCredential = new NetworkCredential(from, "pswd");
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = networkCredential;
                smtp.Port = 587;
                smtp.Host = "localhost";
                smtp.Send(mail); 
                ViewBag.Message = "Password Sent through mail. Please chack password in your account and signup.";
            }
            }
            catch (Exception ex)
            {
                ViewBag.Message = "Error occur for sending data";
            }
        }

        else
        {
            return Json("Invalid Information!", JsonRequestBehavior.AllowGet);
        }
于 2017-09-08T09:32:48.083 回答