2012-06-13 173 views
2

我想在asp.net中使用PasswordRecovery控件恢復密碼,但是當我點擊提交按鈕。我得到這樣的消息:如何配置smtp設置?

錯誤的命令序列。服務器響應是:此郵件服務器在嘗試發送到非本地電子郵件地址時需要進行身份驗證。請檢查您的郵件客戶端設置,或與您的管理員聯繫,以驗證爲此服務器 定義了域或地址。 這是我的html代碼:

<asp:PasswordRecovery ID="PasswordRecovery1" runat="server" 
      style="font-family: Tahoma; font-size: small" 
      onsendingmail="PasswordRecovery1_SendingMail" > 
      <MailDefinition BodyFileName="../User Pages/RecoveryPass.htm" From="[email protected]" 
      IsBodyHtml="True" Subject="بازیابی کلمه ی عبور" Priority="High"></MailDefinition> 
     <UserNameTemplate> 
      <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;"> 
       <tr> 
        <td> 
         <table cellpadding="0"> 
          <tr> 
           <td align="center" colspan="2"> 
            &nbsp;</td> 
          </tr> 
          <tr> 
           <td align="center" colspan="2"> 
            <asp:Label ID="Label1" runat="server" 
             Text="<%$ Resources:resource, passrecovery %>" 
             style="font-size: small; font-family: Tahoma"></asp:Label></td> 
          </tr> 
          <tr> 
           <td align="right"> 
            <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName"></asp:Label> 
           </td> 
           <td> 
            <asp:TextBox ID="UserName" runat="server"></asp:TextBox> 
            <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
             ControlToValidate="UserName" ErrorMessage="User Name is required." 
             ToolTip="User Name is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator> 
           </td> 
          </tr> 
          <tr> 
           <td align="center" colspan="2" style="color:Red;"> 
            <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal> 
           </td> 
          </tr> 
          <tr> 
           <td align="right" colspan="2"> 
            <asp:Button ID="SubmitButton" runat="server" CommandName="Submit" Text="<%$ Resources:resource, submit %>" 
             ValidationGroup="PasswordRecovery1" Font-Names="tahoma" 
             Font-Size="X-Small" Width="80px" /> 
           </td> 
          </tr> 
         </table> 
        </td> 
       </tr> 
      </table> 
     </UserNameTemplate> 
</asp:PasswordRecovery> 

,這是後面的代碼:

protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e) 
    { 
     SmtpClient smtp = new SmtpClient(); 

     smtp.Send(e.Message); 
    } 

,這是在web.config中 SMTP設置:

<system.net> 
    <mailSettings > 
     <smtp > 
     <network host="mail.domainname.com" userName="[email protected]" password ="password" defaultCredentials="false"/> 
     </smtp> 
      </mailSettings> 
    </system.net> 

回答

0

的DefaultCredentials應設置爲true,並且應該明確地將憑據添加到客戶端。

SmtpClient client = new SmtpClient(server, port); 
     // Credentials are necessary if the server requires the client 
     // to authenticate before it will send e-mail on the client's behalf. 
     client.Credentials = CredentialCache.DefaultNetworkCredentials; 
1

設置您的電子郵件帳戶credentials,假設您正在使用您的帳戶Gmail發送電子郵件,所以,你必須設置credentials (username and password)smtpClient object

以下是例子::

MailMessage m = new MailMessage(); 
SmtpClient sc = new SmtpClient(); 
MAILMESSAGE

幫助我們創建郵件和SmtpClient使我們能夠將郵件發送到reciepient。

注意:下面的代碼被配置爲從Gmail帳戶發送郵件..

m.From = new MailAddress("[email protected]", "Display name"); 
m.To.Add(new MailAddress("[email protected]", "Display name To")); 
m.Subject = "Test"; 
m.Body = "This is a Test Mail"; 
sc.Host = "smtp.gmail.com"; 
sc.Port = 587; 
sc.Credentials = new System.Net.NetworkCredential("[email protected]", "password of from"); 
sc.EnableSsl = true; // runtime encrypt the SMTP communications using SSL 
sc.Send(m); 
+0

這些的arent解決我的問題 –