代码执行,但收件箱中收不到邮件
Code executes, but not receiving mail in inbox
我已经编写了发送电子邮件的代码,虽然代码成功执行,但收件箱中没有收到邮件。请帮助我并更正代码;我的错误在哪里,或者为什么收件箱没有收到邮件?
这是我的代码,请告诉我问题是什么。
我正在做一个项目,在这个项目中忘记密码和用户名会发送给输入电子邮件 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 和 catch,放置某种日志记录,甚至只是在其上放置一个断点。然后,希望您能告诉我们异常是什么。
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>
字符串电子邮件=TextBox1.Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["regesterConnectionString"].ConnectionString);
//SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=regester;Integrated Security=True");
字符串命令 = "select uname,password,email from reg ";
SqlCommand sqlcmd = new SqlCommand(command, con);
//sqlcmd.Parameters["@Email"].Value = email;
//sqlcmd.Parameters.Add("@Email", email);
con.Open();
如果(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";
}
}
我已经编写了发送电子邮件的代码,虽然代码成功执行,但收件箱中没有收到邮件。请帮助我并更正代码;我的错误在哪里,或者为什么收件箱没有收到邮件?
这是我的代码,请告诉我问题是什么。
我正在做一个项目,在这个项目中忘记密码和用户名会发送给输入电子邮件 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 和 catch,放置某种日志记录,甚至只是在其上放置一个断点。然后,希望您能告诉我们异常是什么。
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>
字符串电子邮件=TextBox1.Text; SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["regesterConnectionString"].ConnectionString); //SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=regester;Integrated Security=True"); 字符串命令 = "select uname,password,email from reg "; SqlCommand sqlcmd = new SqlCommand(command, con); //sqlcmd.Parameters["@Email"].Value = email; //sqlcmd.Parameters.Add("@Email", email); con.Open(); 如果(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";
}
}