C#SmtpClient错误:不是本地主机?

问题描述:

我使用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

我的搜索: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