2012-03-27 88 views
0

我正在使用this問題中描述的代碼。但發送電子郵件時出現以下錯誤。發送電子郵件時出錯

郵箱不可用。服務器響應是:請驗證到 使用此郵件服務器

任何想法可能是錯的什麼?

UPATE:以下是代碼

System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient(); 
MailMessage Message = new MailMessage("From", "To", "Subject", "Body"); 
Client.Send(Message); 

隨着App.config中以下。

<system.net> 
    <mailSettings> 
     <smtp from="[email protected]"> 
     <network host="smtp.MyDomain1.com" port="111" userName="abc" password="helloPassword1" /> 
     </smtp> 
    </mailSettings> 
    </system.net> 
+0

身份驗證聲音,您需要提供憑據?用戶密碼? – gbianchi 2012-03-27 14:41:19

+0

我已經提供了這些配置文件。我加倍檢查,這些都是正確的值。 – imak 2012-03-27 14:42:12

+1

向我們顯示您的代碼。我們希望看到您所做的任何修改。 – Msonic 2012-03-27 14:42:18

回答

2

張貼在那裏的代碼應該工作。如果沒有,您可以嘗試在代碼隱藏中設置用戶名和密碼,而不是從web.config中讀取它們。從systemnetmail.com

代碼示例:

static void Authenticate() 
{ 
    //create the mail message 
    MailMessage mail = new MailMessage(); 

    //set the addresses 
    mail.From = new MailAddress("[email protected]"); 
    mail.To.Add("[email protected]"); 

    //set the content 
    mail.Subject = "This is an email"; 
    mail.Body = "this is the body content of the email."; 

    //send the message 
    SmtpClient smtp = new SmtpClient("127.0.0.1"); 

    //to authenticate we set the username and password properites on the SmtpClient 
    smtp.Credentials = new NetworkCredential("username", "secret"); 
    smtp.Send(mail); 

} 
+0

感謝您的提示。我會嘗試 – imak 2012-03-27 14:46:21

1

是的,smtp服務器告訴你,爲了給你轉發電子郵件,你需要在嘗試發送電子郵件之前進行身份驗證。如果您擁有smptp服務器的帳戶,則可以相應地在SmtpClient對象上設置憑據。根據smtp服務器支持的身份驗證機制,端口等會有所不同。從MSDN

例子:

public static void CreateTestMessage1(string server, int port) 
{ 
      string to = "[email protected]"; 
      string from = "[email protected]"; 
      string subject = "Using the new SMTP client."; 
      string body = @"Using this new feature, you can send an e-mail message from an application very easily."; 
      MailMessage message = new MailMessage(from, to, subject, body); 
      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; 

     try { 
       client.Send(message); 
     } 
      catch (Exception ex) { 
       Console.WriteLine("Exception caught in CreateTestMessage1(): {0}", 
        ex.ToString()); 
     }    
} 

的底線是,您的憑據沒有被傳遞給SMTP服務器,否則你不會得到這個錯誤。

相關問題