未指定SMTP主机。出现InvalidOperationException

时间:2012-04-27 14:52:01

标签: asp.net smtp email-validation invalidoperationexception

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

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
有人可以帮我解决这个问题吗?

0 个答案:

没有答案