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

本文关键字:收件箱 执行 代码 | 更新日期: 2023-09-27 17:57:12

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

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

我正在从事忘记密码和用户名发送给输入电子邮件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";
                }
            }
        }
    }
}

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

此行丢弃Send引发的任何异常。删除尝试和捕获,放置某种日志记录,甚至只是在其上放置一个断点。然后,希望您能告诉我们例外是什么。

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

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

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>

字符串电子邮件 = 文本框1.文本; SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["regesterConnectionString"].连接字符串); SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=regester;Integrated Security=True"); 字符串命令="选择uname,密码,来自reg的电子邮件"; SqlCommand sqlcmd = new SqlCommand(command, con); sqlcmd.参数["@Email"]。值 = 电子邮件; sqlcmd.Parameters.Add("@Email", email); 缺点。打开(); 如果(con.状态 == 连接状态.打开) { SqlDataReader dtr = sqlcmd.执行读取器();

            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";
                }
            }