通过SMTP发送邮件并获取异常

时间:2018-12-12 12:33:35

标签: c# smtp

我正在使用实体框架,此代码在smtp.send(msg)上给出了异常。请给我解决方法

 public HttpResponseMessage forgotpasswordmail(String email)
    {
        var user = db.Users.SingleOrDefault(x => x.Email == email);

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("hanzalaah@gmail.com");
        msg.To.Add(email);
        msg.Subject = "Share It: Password Recovery";
        msg.Body = "Hi, "+user.Name+ "<br />As requested, here is your login details<br /><br />Your Username: " + user.Email + "<br /><br />Your Password: " + user.Password + "<br /><br />Thank you for trusting us! Hope to hear from you soon!<br /><br />";
        msg.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient("smtp.gamil.com", 587);
        smtp.UseDefaultCredentials = false;
        //smtp.Host = "smtp.gmail.com";
        //smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential("hanzalaah@gmail.com", "Password.");
        smtp.EnableSsl = true;
        smtp.Send(msg);
        return Request.CreateResponse(HttpStatusCode.OK, "Your Password Details Sent to your mail.");


    }

0 个答案:

没有答案