1

我想为我的用户编写电子邮件验证。但我有一些问题。
我有这个:

public static void SendMail(User user)
    {
        string verifyNewUser = "http://localhost:9928/Account/Activate/" +
                                  user.Name + "/" + user.EmailKey;
        var sr = new StreamReader("ActivateNewUser.txt");
        MailMessage mailMessage = new System.Net.Mail.MailMessage();
        mailMessage.IsBodyHtml = true;
        mailMessage.To.Add(user.Email);
        mailMessage.Subject = "Registration on Harb";
        mailMessage.Body = sr.ReadToEnd();
        sr.Close();
        mailMessage.Body = mailMessage.Body
                           .Replace("@AccountName", user.Name);
        mailMessage.Body = mailMessage.Body
                           .Replace("@VerificationUrl", verifyNewUser);
        var smtpClient = new SmtpClient();
        smtpClient.Send(mailMessage);
    }

在 Web.config 我写:

<system.net>
<mailSettings>
  <smtp deliveryMethod="Network" from="myEmail@gmail.com">
    <network host="smtp.gmail.com" port="587" userName="myEmail@gmail.com"
      password="myPassword" defaultCredentials="false" enableSsl="true"/>
  </smtp>
</mailSettings>

但我有一个InvalidOperationException.
有人可以帮我解决这个问题吗?

4

0 回答 0