通过比较数据库中的电子邮件发送带有用户名的电 [英] Sending email with username by comparing email in database
问题描述
我有一个用户登录的页面,我也忘记了用户名链接,通过电子邮件向用户发送用户名。我正在使用Outlook帐户来测试哪个是开放的。我在我的数据库表中检查用户的电子邮件地址,如果db表中存在该地址,则向用户发送电子邮件。我收到以下错误'
i have a page where users login , i also have forgot username link which sends users their username via email. I am using an outlook account to test which is open. i check the email address against one in my database table for users and send email to user if the address is present in db table. i am getting the following error '
在App_Web_2woqyecp.dll中发生了'System.Net.Mail.SmtpFailedRecipientException'类型的异常但是没有用户代码处理
An exception of type 'System.Net.Mail.SmtpFailedRecipientException' occurred in App_Web_2woqyecp.dll but was not handled in user code
'
其他信息:邮箱不可用。服务器响应是:5.7.1无法中继
Additional information: Mailbox unavailable. The server response was: 5.7.1 Unable to relay
如果我使用另一个电子邮件地址而不是Outlook,则不会发送电子邮件但我没有收到错误
我尝试过:
and if i user another email address not from the outlook the email is not sent but i dont get an error
What I have tried:
public void forgot()
{
string username = "";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand(spgetuser, con);
cmd.Parameters.AddWithValue("@email", txtEmailP.Text);
con.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
username= dr["username"].ToString();
}
}
con.Close();
if (!string.IsNullOrEmpty(username))
{
try
{
string emailFrom = "me@example.com";
string body = "<div style='border: medium solid grey; width: 500px; height: 266px;font-family: arial,sans-serif; font-size: 17px;'>";
body += "<h3 margin-top:0px;'>Username Recovery</h3>";
body += "<br />";
body += ("Your Username is: " + username);
body += "<br />";
body += "<br />";
body += "Kinds Regards";
body += "<br />";
body += "</div>";
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
//message.To.Add(emailTo);
//message.Subject = subject;
message.From = new System.Net.Mail.MailAddress(emailFrom);
message.IsBodyHtml = true;
message.Body = body;
message.Priority = System.Net.Mail.MailPriority.High;
message.Subject = "username Recovery";
message.From = new System.Net.Mail.MailAddress(emailFrom);
SmtpClient SmtpMail = new SmtpClient();
SmtpMail.EnableSsl = true;
SmtpMail.Port = 25;
SmtpMail.Host = "myhost";
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
// compare session username to username in database
SqlCommand command = new SqlCommand(spcheck,con)
command.Parameters.AddWithValue("@email", txtEmailP.Text);
using (SqlConnection connection =
new SqlConnection(strConnString))
{
connection.Open();
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())
{
var to = new MailAddress(reader["Email"].ToString());
message.To.Add(to);
}
// Passing values to smtp object
SmtpMail.Send(message);
ScriptManager.RegisterStartupScript(this, typeof(string), "Msg","alert('request has been sent');", true);
Response.Redirect("~/lgn.aspx");
// Call Close when done reading.
reader.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
this.forgot();
}
推荐答案
不计算。
Does not compute.
//message.To.Add(emailTo);
//message.Subject = subject;
这篇关于通过比较数据库中的电子邮件发送带有用户名的电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!