必须指定收件人。当我想使用ASP.NET发送电子邮件时遇到此错误

问题描述 投票:0回答:1
protected void ButPwd_Click(object sender, EventArgs e)
{
    string mainconn = ConfigurationManager.ConnectionStrings["AssDatabaseConnectionString"].ConnectionString;
    SqlConnection sqlconn = new SqlConnection(mainconn);
    string sqlquery = "select UserEmail,UserPassword from [dbo].[User] where UserEmail=@Email";
    SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
    sqlcomm.Parameters.AddWithValue("@Email", TxtEmail.Text);
    sqlconn.Open();
    SqlDataReader sdr = sqlcomm.ExecuteReader();
    if(sdr.Read())
    {
        string username1 = sdr["UserEmail"].ToString();
        string password = sdr["UserPassword"].ToString();



        MailMessage mm = new MailMessage();
        mm.From = new MailAddress("[email protected]");
        mm.Subject = "Your Password is !";
        mm.Body = string.Format("Hello : <h1>{0}</h1> is your Email <br /> Your Password is <h1>{1}<h1>",username1,password);
        mm.IsBodyHtml = true;
        mm.Priority = MailPriority.High;


        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        NetworkCredential nc = new NetworkCredential();
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        nc.UserName = "[email protected]";
        nc.Password = "XohImNooBHFR0OVvjcYpJ3NgPQ1qq73WKhHvch0VQtg=";

        smtp.UseDefaultCredentials = true;
        smtp.Credentials = nc;
        smtp.Port = 587;
        Labmsg.Text = "Your Password has been sent to " + TxtEmail.Text;
        Labmsg.ForeColor = Color.Red;
        smtp.Send(mm);
    }
    else
    {
        Labmsg.Text = TxtEmail.Text + ". This Email is not exist in the database";
        Labmsg.ForeColor = Color.Red;
    }
}
c#
1个回答
1
投票
mm.To.Add("[email protected]");     //Add this line to your code
© www.soinside.com 2019 - 2024. All rights reserved.