【问题标题】:SMTP client fails to send message on local host, succeeds on remote hostSMTP 客户端在本地主机上发送消息失败,在远程主机上成功
【发布时间】:2013-03-02 23:46:51
【问题描述】:

我有一个小型 tcp 服务器,除其他外,它用于通过 SMTP 服务器发送邮件。

问题是,当我在我的开发机器上运行它时它可以正常工作(远程连接到 smtp 服务器),但是当我在与 SMTP 服务器(Windows Server 2008 R2)相同的机器上运行应用程序时,我得到以下例外:

System.Net.Mail.SmtpException:发送邮件失败。 ---> System.IO.IOException:无法从传输连接读取数据:net_io_connectionclosed。

SMTP 服务器配置为仅允许来自 127.0.0.1 和 tcp 应用程序绑定的 IP 地址以及我的开发机器的中继。它还被配置为只允许来自同一 IP 列表的连接。

发送邮件的C#位如下:

var mail = new MailMessage();
mail.To.Add(recipient);
mail.From = new MailAddress("me@mydomain.com");
mail.Subject = "subject";
mail.Body = "message";
var smtpClient = new SmtpClient(MailServerAddress, Constants.MAIL_SERVER_PORT);         
smtpClient.Send(mail);

MailServerAddress 在应用程序首次运行时定义。
我尝试将其设置为 127.0.0.1 和 SMTP 服务器配置的 IP 地址,但仍然遇到相同的异常。

检查服务器日志显示没有连接迹象,这符合上述异常。

我在这里做错了什么?

【问题讨论】:

  • 您是通过 IIS SMTP 服务器发送的吗?

标签: c# smtp windows-server-2008-r2


【解决方案1】:

只是一个黑暗的答案,但试试这个,因为我在大约 3 年前遇到过类似的问题,但不记得修复了,但是当我查看时,这行代码对我来说很突出

_client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

所以也许可以尝试(假设您使用 IIS 进行 SMTP):

var smtpClient = new SmtpClient(MailServerAddress, Constants.MAIL_SERVER_PORT); 
smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;        
smtpClient.Send(mail);

【讨论】:

    【解决方案2】:

    here复制的答案:

    Unable to read data from the transport connection: net_io_connectionclosed
    
    
    What this error means is that System.net.mail was unable to find the smtp server.
    
    The answer will vary depending on whether you have a fixed IP or a dynamic IP but,
    basically, you need to assign a valid IP to your smtp server.
    
    With fixed IP's this is relatively straightforward.
    With dynamic IP's it takes a bit of tweaking.
    
    Open the IIS Manager and check the properties for the smtp server.
    
    In the Default SMTP Virtual Server's properties, in the "Access" tab,
    in the Connection Control and Relay dialogs, make sure that your local IP is assigned.
    ( In my case, it's 10.0.0.2... )
    
    You may also need to modify your hosts file, to point 127.0.0.1 to your machine name.
    ( \WINDOWS\system32\drivers\etc\hosts )
    
    Then, in your code, assign your machine name to the smtp client :
    
    SmtpClient client = New SmtpClient("yourmachinename")
    client.Send(mail)
    

    另外,您的防火墙可能会阻止您的测试。如果可以,请断开 Internet 连接并禁用防火墙,而不会造成安全风险。看看它是否有效。如果是这样,您将需要更深入地查看防火墙设置。如果不是,那么它确实可能是您的 IP 地址,如上所述。

    【讨论】:

    • 我已经尝试过了,但遗憾的是,我仍然遇到同样的异常。我已确保 smtp 服务器的端口已在防火墙中打开;还有其他需要设置的属性吗?
    • @asafsitner 好的,我担心我无法再提供任何帮助,但它可能会帮助其他人了解远程服务器的含义。您的意思是环回从远程服务器工作,还是您可以将 smtp 消息发送到远程服务器?
    • @JABFreewre - 当我在不同于托管 SMTP 服务器的机器上运行 c# 应用程序时,我可以毫无问题地发送邮件。当 c# 应用程序与 SMTP 服务器在同一台机器上运行时,我得到了那个异常。
    猜你喜欢
    • 1970-01-01
    • 2017-12-24
    • 2017-02-20
    • 2011-12-15
    • 1970-01-01
    • 2020-02-07
    • 2020-12-04
    • 1970-01-01
    • 2015-06-29
    相关资源
    最近更新 更多