SMTP 客户端无法在本地主机上发送消息,在远程主机上成功 [英] SMTP client fails to send message on local host, succeeds on remote host

查看:66
本文介绍了SMTP 客户端无法在本地主机上发送消息,在远程主机上成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型 tcp 服务器,用于通过 SMTP 服务器发送邮件.

I have a small tcp server that is used, among other things, to send mails through a SMTP server.

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

The problem is, it works correctly when I run it on my development machine (a remote connection to the smtp server), however when I run the application on the same machine as the SMTP server (Windows Server 2008 R2) I get the following exception:

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

System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

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

The SMTP Server is configured to only allow relay from 127.0.0.1 and the IP address that the tcp application binds to, as well as my development machine. It's also configured to only allow connections from the same IP list.

发送邮件的C#位如下:

The C# bit to send the mail is as follows:

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 地址,但我仍然遇到相同的异常.

MailServerAddress is defined when the application is first run.
I've tried setting it to both 127.0.0.1 and the SMTP Server's configured IP address, but I still get the same exception.

查看服务器日志没有连接迹象,符合上述异常.

Checking the server logs shows no sign of connection, which fits with the above exception.

我在这里做错了什么?

推荐答案

只是在黑暗中刺伤答案,但试试这个,因为我大约 3 年前遇到过类似的问题,但不记得修复方案,但是这一行当我看到代码时,我觉得很明显

Just a stab in the dark answer, but try this as I had a similar problem about 3 years ago but can't remember the fix, but this line of code stands out to me when I had a look

_client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

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

So maybe try (assuming your using IIS for SMTP):

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

这篇关于SMTP 客户端无法在本地主机上发送消息,在远程主机上成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆