发送本地主机链接到ASP中的电子邮件?

时间:2014-02-15 03:21:03

标签: c# asp.net email localhost email-integration

我创建了一个密码恢复页面,该页面向用户发送一封电子邮件,其中包含指向另一个页面的链接,用户可以在其中重置密码。这是我的代码文件..

protected void btnSubmit_Click(object sender, EventArgs e)
{
        try
        {
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection("Data Source=DBS_WINDOWS7;Initial Catalog=LMS;Integrated Security=True"))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT AccountID, Password FROM tblAccount Where Email= '" + txtEmail2.Text.Trim() + "'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(txtEmail2.Text);
                // Recipient e-mail address.
                Msg.To.Add(txtEmail2.Text);
                Msg.Subject = "Your Password Details";
                Msg.Body = HttpContext.Current.Request.Url.Host + @"Login.aspx?id=id";
                    Msg.IsBodyHtml = true;
                // your remote SMTP server IP.
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential("test.email@gmail.com", "dummypassword");
                smtp.EnableSsl = true;
                smtp.Send(Msg);
                //Msg = null;
                lbltxt.Text = "<i class='icon-check-sign'></i>&nbsp;&nbsp;We have sent your password. Go check it out!";
                lbltxt.CssClass = "alert alert-success";
                // Clear the textbox valuess
                txtEmail.Text = "";
                txtEmail2.Text = "";
            }
            else
            {
                lbltxt.CssClass = "alert alert-error";
                lbltxt.Text = "<i class='icon-question-sign'></i>&nbsp;&nbsp;Something went wrong. Are you sure that's your email with us?";
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }
    }

电子邮件发送工作完美,但我尝试显示的链接无法点击,@"Login.aspx?id=id";代码行直接打印在发送的电子邮件中。

顺便说一句,这个项目托管在localhost上。

所以我的问题是: 为什么这段代码不会创建一个可点击链接到用户应该重置密码的页面?有人可以分享他们的想法吗?

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

尝试更改此行

Msg.Body = HttpContext.Current.Request.Url.Host + @"Login.aspx?id=id";

阅读本文:

Msg.Body = String.Format("<a href='{0}/Login.aspx?id=id'>Link</a>", HttpContext.Current.Request.Url.Host);