2009-09-11 81 views
1

我使用SmtpClient發送一個簡單的郵件帶有附件,但我得到這個錯誤:C#SmtpClient錯誤:不是本地主機?

Mailbox unavailable. The server response was: not local host example.com, not a gateway

System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable.

The server response was: not local host example.com, not a gateway at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message)

,代碼:

public static void CreateMessageWithAttachment(byte[] compressed) 
    { 
     // Create a message and set up the recipients. 
     MailMessage message = new MailMessage(
      "[email protected]", 
      "[email protected]", 
      "Hello.", 
      "How are you?"); 

     // Create the file attachment for this e-mail message. 
     Stream attachStream = new MemoryStream(compressed); 
     Attachment attachment = new Attachment(attachStream, MediaTypeNames.Application.Octet); 
     message.Attachments.Add(attachment); 

     //Send the message. 
     SmtpClient client = new SmtpClient("123.12.12.123"); 
     // Add credentials if the SMTP server requires them. 
     client.Credentials = CredentialCache.DefaultNetworkCredentials; 

     client.Send(message); 

     attachment.Dispose(); 
    } 

課程領域和IP是在原代碼有效。我曾嘗試使用「本地主機」和IP但獲得相同的錯誤。谷歌搜索返回了3個結果,其中有用的一個似乎在中文和防火牆中阻止我翻譯它。

在此先感謝。

+0

這是從你自己的服務器?我發現大多數ISP(在瑞典)無論如何都有自己的SMTP服務器上的每個域的SMTP,你必須連接到這種情況下,我會smtp.example.com – 2009-09-11 09:56:41

+0

是的,自己的服務器。 – syaz 2009-09-11 10:06:54

回答

3

我的搜索:C# smtpclient error The server response was: not local host example.com, not a gateway並獲得了27K +結果。

如果您正在使用本地主機看看this page它說:

This is a relay error. Make sure you can relay through the SmtpMail.SmtpServer 
either by your IP address, by your MailMessage.From address, or if you need to 
authenticate, check out 3.8 How do I authenticate to send an email? 

If SmtpMail.SmtpServer is set to "127.0.0.1" or "localhost", and you are using 
the built in IIS SMTP Service, you can allow relaying for 127.0.0.1 by 

1) Opening the IIS Admin MMC 
2) Right-Clicking on the SMTP Virtual Server and selecting Properties 
3) On the Access tab, click the Relay button 
4) Grant 127.0.0.1 (or the IP address used by System.Web.Mail) to the 
    Computers list. 
5) Close all dialogs 
6) Restarting the SMTP Service 
+1

不完全是我的搜索風格。我搜查:C#smtpclient「不是本地主機」 – syaz 2009-09-11 10:08:26