2012-04-27 37 views
1

我想爲我的用戶編寫電子郵件驗證。但我有一些問題。
我有這樣的:未指定SMTP主機。 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="[email protected]"> 
    <network host="smtp.gmail.com" port="587" userName="[email protected]" 
     password="myPassword" defaultCredentials="false" enableSsl="true"/> 
    </smtp> 
</mailSettings> 

但是我有一個InvalidOperationException
有人可以幫我解決這個問題嗎?

+0

你在哪裏添加SMTPClient服務器? – 2012-04-27 14:58:34

+0

我使用Gmail服務器。這就是爲什麼我在Web.config中編寫mailSettings的原因。 如果我理解正確。 – LuckSound 2012-04-27 15:10:59

回答

0

我沒有看到您的文章的評論鏈接,所以我會問這裏的問題。

在哪個語句發生異常?