【问题标题】:C# SmtpClient error: not local host?C# SmtpClient 错误:不是本地主机?
【发布时间】:2010-11-27 10:57:08
【问题描述】:

我正在使用 SmtpClient 发送带有附件的简单邮件,但我收到此错误:

邮箱不可用。服务器响应是:不是本地主机 example.com,不是网关

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 个结果,其中有用的结果似乎是中文,而防火墙阻止我翻译它。

提前致谢。

【问题讨论】:

  • 这是来自您自己的服务器吗?我发现大多数 ISP,不管怎样,在瑞典,他们服务器上的每个域都有自己的 SMTP,在这种情况下,我将是 smtp.example.com

标签: c# smtpclient


【解决方案1】:

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

【讨论】:

  • 不完全是我的搜索风格。我搜过:C# smtpclient“不是本地主机”
猜你喜欢
  • 2016-07-29
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 2014-06-07
  • 2018-06-27
  • 2012-10-31
  • 1970-01-01
相关资源
最近更新 更多