本文介绍了如何在ASP.NET中的电子邮件中向用户发送密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表单,我有一个文本框供用户输入他们的电子邮件地址,以便他们可以获取他们的密码,如果他们忘记了,发送到用户的电子邮件。我有代码,但它似乎不起作用。我有一个电子邮件服务器。邮件服务器是mail.fastfix.com。我的代码有什么问题,电子邮件无法发送?
I have a form that I have a textbox for the user to enter their email address so that they can get their password, if they forget it, sent to the user email. I have the code but it doesn't seem to work. I have a mail server for the emails. Mail server is mail.fastfix.com. What is wrong in my code the reason why the email won't send?
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Net.Mail;
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["HotConnectionString"].ConnectionString;
string strSelect = "SELECT EmailAddress, Password FROM Tablepass WHERE EmailAddress ='" + TextBoxEA.Text + "'";
SqlConnection connection = new SqlConnection(strConnection);
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = strSelect;
SqlParameter email = new SqlParameter("@EmailAddress", SqlDbType.VarChar, 50);
email.Value = TextBoxEA.Text.Trim().ToString();
command.Parameters.Add(email);
//Create Dataset to store results and DataAdapter to fill Dataset
DataSet dsPwd = new DataSet();
connection.Open();
SqlDataAdapter dAdapter = new SqlDataAdapter(command);
dAdapter.Fill(dsPwd);
connection.Close();
if (dsPwd.Tables[0].Rows.Count > 0)
{
MailMessage loginInfo = new MailMessage();
loginInfo.To.Add(TextBoxEA.Text.ToString());
loginInfo.From = new MailAddress("[email protected]");
loginInfo.Subject = "Forgot Password Information";
loginInfo.Body = "EmailAddress: " + dsPwd.Tables[0].Rows[0]["EmailAddress"] + "<br /><br />Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "<br /><br />";
loginInfo.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.mail.fastfix.com";
smtp.Port = 25;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "YourEmailPassword");
smtp.Send(loginInfo);
lblMessage.Text = "Password is sent to you email id,you can now <a href='Login.aspx'>Login</a>";
try
{
smtp.Send(loginInfo);
}
catch (Exception ex)
{
lblMessage.Text = "Oops, Something Went Wrong When We Tried to Send The Email";
return;
}
}
else
{
lblMessage.Text = "Email Address Not Registered";
}
}
}
推荐答案
这篇关于如何在ASP.NET中的电子邮件中向用户发送密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!