代码执行,但不在收件箱中接收邮件

时间:2015-06-20 19:16:30

标签: c# asp.net asp.net-mvc visual-studio asp.net-mvc-3

我已经编写了发送电子邮件的代码,并且代码执行成功后,收件箱中没有收到邮件。请帮助我并更正代码;我的错误在哪里,或者为什么收件箱中没有收到邮件?

这是我的代码,请告诉我这是什么问题。

我正在处理忘记密码和用户名发送给输入电子邮件ID的用户的项目。

protected void Button2_Click(object sender, EventArgs e)
{
        string email = TextBox1.Text;
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=regester;Integrated Security=True");
        string command = "select id,password,email from reg ";

        SqlCommand sqlcmd = new SqlCommand(command, con);
        //sqlcmd.Parameters["@Email"].Value = email;
        //sqlcmd.Parameters.Add("@Email", email);
        con.Open();

        if (con.State == ConnectionState.Open)
        {
            SqlDataReader dtr = sqlcmd.ExecuteReader();

            while (dtr.Read())
            {
                if (dtr[2].ToString().Equals(TextBox1.Text))
                {
                    MailMessage mail = new MailMessage();
                    mail.To.Add(dtr[2].ToString());
                    mail.From = new MailAddress("mian722@hotmail.com");
                    mail.Subject = "Your userId and Password";
                    mail.Body = "Your<br/> UserId:<b>" + dtr[0].ToString() + "</b><br/>" + "Password:<b>" + dtr[1].ToString() + "</b>";
                    mail.IsBodyHtml = true;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;

                    smtp.Credentials = new System.Net.NetworkCredential("your id", "your password");
                    smtp.EnableSsl = true;
                    //smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    //smtp.EnableSsl = true;
                    //smtp.UseDefaultCredentials = true;
                    smtp.EnableSsl = true; //Gmail works on Server Secured Layer
                    try
                    {
                        smtp.Send(mail);
                    }
                    catch (Exception ex)
                    {

                    }  
                    //smtp.Send(mail);

                    Label1.Text = "check your mailbox for user iD and Password";

                    string javaScript = "<script language=JavaScript>\n" + "alert('User Id and password send to Your mail box');\n" + "</script>";
                    RegisterStartupScript("xyz", javaScript);
                    break;
                }
                else
                {
                    Label1.Text = "Email Id not valid";
                }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

此行会丢弃Send引发的所有异常。删除try和catch,进行某种日志记录,甚至只是在其上放置一个断点。然后,希望你可以告诉我们例外是什么。

try
{
    smtp.Send(mail);
}
catch (Exception ex)
{
}

答案 1 :(得分:0)

您可能希望在错误处理方面更具体,至少在找到问题之前

protected void Button2_Click(object sender, EventArgs e)
{
  try
  {
    string email = TextBox1.Text;
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=regester;Integrated Security=True");
    string command = "select id,password,email from reg ";

    SqlCommand sqlcmd = new SqlCommand(command, con);
    //sqlcmd.Parameters["@Email"].Value = email;
    //sqlcmd.Parameters.Add("@Email", email);
    con.Open();

    if (con.State == ConnectionState.Open)
    {
        SqlDataReader dtr = sqlcmd.ExecuteReader();

        while (dtr.Read())
        {
            if (dtr[2].ToString().Equals(TextBox1.Text))
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(dtr[2].ToString());
                mail.From = new MailAddress("mian722@hotmail.com");
                mail.Subject = "Your userId and Password";
                mail.Body = "Your<br/> UserId:<b>" + dtr[0].ToString() + "</b><br/>" + "Password:<b>" + dtr[1].ToString() + "</b>";
                mail.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;

                smtp.Credentials = new System.Net.NetworkCredential("your id", "your password");
                smtp.EnableSsl = true;
                //smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                //smtp.EnableSsl = true;
                //smtp.UseDefaultCredentials = true;
                smtp.EnableSsl = true; //Gmail works on Server Secured Layer
                try
                {
                    smtp.Send(mail);
                }
                catch (SmtpException ex)
                {
                    string javaScript = "<script language=JavaScript>\n" + "alert('SMTP mail exception: " + Microsoft.Security.Application.Encoder.HTMLEncode(ex.Message) + "');\n" + "</script>";
                    RegisterStartupScript("xyz", javaScript);
                    // add logging here
                    break;
                }  
                catch (Exception ex)
                {
                    string javaScript = "<script language=JavaScript>\n" + "alert('A general exception sending mail: " + Microsoft.Security.Application.Encoder.HTMLEncode(ex.Message) + "');\n" + "</script>";
                    RegisterStartupScript("xyz", javaScript);
                    // add logging here
                    break;
                }  

                Label1.Text = "check your mailbox for user iD and Password";

                string javaScript = "<script language=JavaScript>\n" + "alert('User Id and password send to Your mail box');\n" + "</script>";
                RegisterStartupScript("xyz", javaScript);
                break;
            }
            else
            {
                Label1.Text = "Email Id not valid";
            }
        }
      }
    }
  } catch(SqlException sqlexcept)
    {
         string javaScript = "<script language=JavaScript>\n" + "alert('SQL exception: '" + Microsoft.Security.Application.Encoder.HTMLEncode(sqlexcept.Message) ");\n" + "</script>";
          RegisterStartupScript("xyz", javaScript);
          // add logging here
    }
  } catch(Exception except)
    {
         string javaScript = "<script language=JavaScript>\n" + "alert('General mail creation exception: '" + Microsoft.Security.Application.Encoder.HTMLEncode(except.Message) ");\n" + "</script>";
          RegisterStartupScript("xyz", javaScript);
          // add logging here
    }
} 

邮件的web.config设置。

    <system.net> 
     <mailSettings>
      <smtp deliveryMethod="Network" from="myname@outlook.com">
        <network host="smtp-mail.outlook.com" userName="myname@outlook.com" password="<passwordhere>" port="587" enableSsl="true"/>
      </smtp>
     </mailSettings>
   </system.net>

答案 2 :(得分:-1)

string email = TextBox1.Text;    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [&#34; regesterConnectionString&#34;] .ConnectionString);             // SqlConnection con = new SqlConnection(&#34; Data Source = .; Initial Catalog = regester; Integrated Security = True&#34;);             string命令=&#34;从reg&#34;中选择uname,密码,电子邮件;             SqlCommand sqlcmd = new SqlCommand(command,con);             //sqlcmd.Parameters[" ;@Email"].Value = email;             //sqlcmd.Parameters.Add("@Email" ;, email);             con.Open();             if(con.State == ConnectionState.Open)             {                 SqlDataReader dtr = sqlcmd.ExecuteReader();

            while (dtr.Read())
            {
                if (dtr[2].ToString().Equals(TextBox1.Text))
                {



                    MailMessage mail = new MailMessage();
                    mail.To.Add(dtr[2].ToString());
                    mail.From = new MailAddress("youremail@hotmail.com");
                    mail.Subject = "Your userId and Password";
                    mail.Body = "Your<br/> UserId:<b>" + dtr[0].ToString() + "</b><br/>" + "Password:<b>" + dtr[1].ToString() + "</b>";

                    mail.IsBodyHtml = true;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.live.com";


                    smtp.Credentials = new System.Net.NetworkCredential("yyyyyyyy@hotmail.com", "xxxxxx");
                    // NetworkCredential.UserName = "";
                    smtp.EnableSsl = true;
                    smtp.Port = 25;

                    //TLS / SSL required = yes;
                    //smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    //smtp.EnableSsl = true;
                    //smtp.UseDefaultCredentials = true;
                    //smtp.EnableSsl = true; //Gmail works on Server Secured Layer
                    //try
                    //{
                    //    smtp.Send(mail);
                    //}
                    //catch (Exception ex)
                    //{}
                     smtp.Send(mail);









                    Label1.Text = "check your mailbox for user iD and Password";

                    string javaScript = "<script language=JavaScript>\n" + "alert('User Id and password send to Your mail box');\n" + "</script>";
                    RegisterStartupScript("xyz", javaScript);
                    break;
                }
                else
                {
                    Label1.Text = "Email Id not valid";
                }

            }