发送电子邮件到电子邮件地址不在数据库中的问题 [英] Send email to the email adresses not in the database problem

查看:114
本文介绍了发送电子邮件到电子邮件地址不在数据库中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用gmail作为smtp服务器发送用户帐户的密码..

问题是当我发送电子邮件到不在数据库中的电子邮件地址时.

它将提示电子邮件没有在数据库中退出(请参阅代码).但与此同时,它还会将电子邮件发送到电子邮件地址,并将数据读取器中存在的最后一个已恢复密码发送给我在文本框(即,不在数据库中)写的电子邮件地址(即不在数据库中的邮件). br/>
我想要支票,因此无法将电子邮件发送到不在我的数据库中的电子邮件地址

我在类名称中声明了此函数,我的类是

i am trying to send password of the user accounts using gmail as smtp server..

the problem is that when i sent email to the email address which is not in my database.

it will prompt that email does not exit in the database (see code). but at the same time.. it also sent the email to the email adress with the last recovered password present in datareader to email adrees i have writen in the textbox(i.e which is not in databse) .. which is not in database.

i want a check so no email can sent to the email adresse not in my database

i declared this function in class name my class

<pre>public void getUserPassword(string Email)
    {

        SqlConnection con = new SqlConnection();    //Create an SqlConnection object.

        //Pass the connection string to the SqlConnection object 

        con.ConnectionString = "Data Source=blaPC\\SQLEXPRESS; Initial Catalog=forgetpassword; Integrated Security=True";

        con.Open();  //Open the connection 

        string procedureText = "retriveUserPassword";

        SqlCommand cmd = new SqlCommand(procedureText, con); //Create an SqlCommand object.

        cmd.CommandType = CommandType.StoredProcedure; //Change the behaviour of command text to stored procedure.

        cmd.Parameters.Add(new SqlParameter("@Email", Email));  //Pass value to the parameter that you have created in procedure.

        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {

            
            body = "Your Password is " + dr[0].ToString();
            
        }

        else
        {

            HttpContext.Current.Response.Write("<script>alert('Email does not exist')</script>");
            
            

        }



这是发送电子邮件的方式.我认为问题在于



and here is the send email buttion. i think the problem is in that

protected void Button1_Click(object sender, EventArgs e)
    {
        myclass obj = new myclass();
        //obj.getUserPassword(TextBox1.Text);
        //Label1.Text = myclass.body;

        try
        {
            
            MailMessage mail = new MailMessage();
            obj.getUserPassword(TextBox1.Text);

            mail.To.Add(TextBox1.Text);
            
            mail.From = new MailAddress("farooqspecials@gmail.com");
            mail.Subject = "My Project Password Recovery System";

            string Body = myclass.body;
                          //"using Gmail in ASP.NET";
            mail.Body = Body;

            mail.IsBodyHtml = true;
                        SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential
            ("farooqspecials@gmail.com", "blablabla");
            smtp.EnableSsl = true;
            smtp.Send(mail);
            Label1.Text = "Dear User Your Password is Sent to your Email Address";
        }
        catch (Exception ex)
        {
            Label1.Text = "some Problem" + ex;
        }





存储过程是






store procedure is


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[retriveUserPassword] @Email varchar(50) 

as 

begin 

      select Password from login where Email=@Email 

end

推荐答案

更改
public bool getUserPassword(string Email)
{
   bool validEmail = false;
   //do db stuff
        if (dr.Read())
       {
 
            
            body = "Your Password is " + dr[0].ToString();
            validEmail = true;
        }
 
        else
        {
 
            HttpContext.Current.Response.Write("<script>alert('Email does not exist')</script>");
            
            
 
        }

   return validEmail
}

and then in Button click event check if the function returns a validEmail...

protected void Button1_Click(object sender, EventArgs e)
    {
        myclass obj = new myclass();
        //obj.getUserPassword(TextBox1.Text);
        //Label1.Text = myclass.body;

        try
        {
            
            MailMessage mail = new MailMessage();
            if(obj.getUserPassword(TextBox1.Text))
            {
 
            mail.To.Add(TextBox1.Text);
            
            mail.From = new MailAddress("farooqspecials@gmail.com");
            mail.Subject = "My Project Password Recovery System";
 
            string Body = myclass.body;
                          //"using Gmail in ASP.NET";
            mail.Body = Body;
 
            mail.IsBodyHtml = true;
                        SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential
            ("farooqspecials@gmail.com", "blablabla");
            smtp.EnableSsl = true;
            smtp.Send(mail);
            Label1.Text = "Dear User Your Password is Sent to your Email Address";
}
        }
        catch (Exception ex)
        {
            Label1.Text = "some Problem" + ex;
        }.


尝试一下.

将此代码放在dr.read()
的其他部分之后
Try this one.

Put this code after else part of dr.read()
dr.close();
dr.dispose();


这是另一种可行的方法:
Here is the another approach which will also work:
public string getUserPassword(string Email)
        {
            string body = "";
            SqlConnection con = new SqlConnection();    //Create an SqlConnection object.
            //Pass the connection string to the SqlConnection object 
            con.ConnectionString = "Data Source=blaPC\\SQLEXPRESS; Initial Catalog=forgetpassword; Integrated Security=True";
            con.Open();  //Open the connection 
            string procedureText = "retriveUserPassword";
            SqlCommand cmd = new SqlCommand(procedureText, con); //Create an SqlCommand object.
            cmd.CommandType = System.Data.CommandType.StoredProcedure; //Change the behaviour of command text to stored procedure.
            cmd.Parameters.Add(new SqlParameter("@Email", Email));  //Pass value to the parameter that you have created in procedure.
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {

                body = "Your Password is " + dr[0].ToString();
            }
            else
            {
                HttpContext.Current.Response.Write("<script>alert(''Email does not exist'')</script>");
            }
            dr.Dispose();
            dr = null;
            return body;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            myclass obj = new myclass();
            try
            {
                MailMessage mail = new MailMessage();
                string body = obj.getUserPassword(TextBox1.Text);
                if (body.Length > 0)
                {
                    mail.To.Add(TextBox1.Text);
                    mail.From = new MailAddress("farooqspecials@gmail.com");
                    mail.Subject = "My Project Password Recovery System";
                    //"using Gmail in ASP.NET";
                    mail.Body = body;
                    mail.IsBodyHtml = true;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new System.Net.NetworkCredential
                    ("farooqspecials@gmail.com", "blablabla");
                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                    Label1.Text = "Dear User Your Password is Sent to your Email Address";
                }
            }
            catch (Exception ex)
            {
                Label1.Text = "some Problem" + ex;
            }
            finally
            {
                myclass = null;
            }
        }


这篇关于发送电子邮件到电子邮件地址不在数据库中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆