从桌面应用程序发送电子邮件链接

时间:2015-01-14 11:27:24

标签: c#

我有一个桌面应用程序,它有登录/注册/忘记密码..他在应用程序上注册的电子邮件是他的个人电子邮件..例如,他的个人电子邮件是abc@gmail.com,他也是在这个桌面应用程序上注册,现在忘记了应用程序的密码..我想发送一个链接到他的电子邮件,重置应用程序的密码.. 这是我的代码:

private void forgotPwdLabel_Click(object sender, EventArgs e)
    {
        string email = txtLoginID.Text;//(1)I get emailId from textbox
        var ac = new AccountController();
        var user = ac.FetchUserbyEmail(email);//(2)based on email I retrieve his details
        string UserName = user.UserName;
//(3)Check if there is password for that email,if yes,proceed..If no password, the emailid could  be wrong           
if (user.UserPassword != null)
        {
            using (MailMessage mm = new MailMessage("sender@gmail.com", txtLoginID.Text))
//sender@gmail.com here could me my desktop app
//txtLoginID.Text here is email for which link is to be sent 
            {
                mm.Subject = "Resetting Password";
                mm.Body += "Please click the following link to activate your account" + "<br    />aaaaaaaaaaaaaaaa";//sending some junk to see if I can actually send succesfully
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential(UserName, "<password>");
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
            }
        }

这是正确的方法吗?答案非常感谢,谢谢...

0 个答案:

没有答案