代码之家  ›  专栏  ›  技术社区  ›  syaz

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

  •  1
  • syaz  · 技术社区  · 15 年前

    邮箱不可用。服务器响应为:不是本地主机

    System.Net.Mail.SmtpFailedRecipientException: 邮箱不可用。

    服务器响应为:不是本地主机example.com,不是位于的网关 System.Net.Mail.SmtpTransport.SendMail(MailAddress发件人、MailAddressCollection收件人、String deliveryNotify、SmtpFailedRecipientException和异常)位于 System.Net.Mail.SmtpClient.Send(MailMessage)

    以及守则:

    public static void CreateMessageWithAttachment(byte[] compressed)
        {
            // Create a message and set up the recipients.
            MailMessage message = new MailMessage(
               "noreply@example.com",
               "recepient@example.com",
               "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在原始代码中是有效的。我尝试过使用“localhost”和IP,但得到了相同的错误。谷歌搜索返回了3个结果,其中有用的一个似乎是中文,防火墙阻止我翻译。

    1 回复  |  直到 15 年前
        1
  •  4
  •   TheVillageIdiot    15 年前

    我搜索过: C# smtpclient error The server response was: not local host example.com, not a gateway 获得了27K+的成绩。

    如果您使用的是localhost,请查看 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