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

发布于 2024-08-04 09:36:44 字数 1662 浏览 5 评论 0原文

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

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

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

服务器响应是:不是本地主机 example.com,不是网关 System.Net.Mail.SmtpTransport.SendMail(MailAddress发件人,MailAddressCollection收件人,字符串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(
           "[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是有效的。我尝试过同时使用“localhost”和 IP,但出现相同的错误。谷歌搜索返回了 3 个结果,其中有用的一个似乎是中文的,但防火墙阻止了我翻译它。

提前致谢。

I'm sending a simple mail with attachment using SmtpClient but I get this error:

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)

And the code:

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();
    }

Of course the domain and IP is valid in the original code. I have tried using both "localhost" and IP but getting same error. Googling returned 3 results of which the helpful one seems to be in chinese and firewall preventing me from translating it.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦上情书 2024-08-11 09:36:44

我搜索过:C# smtpclient 错误 服务器响应是:不是本地主机 example.com,不是网关 并获得了 27K+ 结果。

如果您使用的是 localhost,请查看此页面,它显示:

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

I've searched for: C# smtpclient error The server response was: not local host example.com, not a gateway and got 27K+ results.

If you are using localhost have a look at this page it says:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文