我有一个小型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地址,但我仍然得到相同的例外。
检查服务器日志显示没有连接的迹象,这符合上述异常。
我在这里做错了什么?
答案 0 :(得分:2)
只是在黑暗的答案中捅了一下,但试试这个,因为我3年前遇到了类似的问题,但是不记得修复了,但是当我看一眼时,这行代码突出了我
_client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
所以也许尝试(假设您使用IIS进行SMTP):
var smtpClient = new SmtpClient(MailServerAddress, Constants.MAIL_SERVER_PORT);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtpClient.Send(mail);
答案 1 :(得分:0)
从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)
您可能还有其他防火墙阻止您的测试。如果可以,请断开互联网并禁用防火墙,而不会带来安全风险。看看它是否有效。如果是这样,您将需要深入了解防火墙设置。如果没有,它实际上可能是您的IP地址,如上所述。