"无法从传输连接中读取数据:net_io_connectionclosed." - Windows Vista商业和SMTP

joh*_*taa 38 .net c# ssis system.net.mail windows-vista

无法测试从Windows Vista Business中的.NET代码发送电子邮件.

我正在编写代码,一旦证实,我将迁移到SSIS包.代码是通过电子邮件将错误消息发送到收件人列表.

代码如下,但是当我执行代码时遇到异常.

我创建了一个简单的类来进行邮件...设计可能更好,我在实现更强大的功能,方法等之前测试功能.

namespace LabDemos
{
    class Program
    {
        static void Main(string[] args)
        {
            Mailer m = new Mailer();
            m.test();    
        }
    }
}

namespace LabDemos
{
    class MyMailer
    {    
        List<string> _to = new List<string>();
        List<string> _cc = new List<string>();
        List<string> _bcc = new List<string>();
        String _msgFrom = "";
        String _msgSubject = "";
        String _msgBody = "";            

        public void test(){
        //create the mail message
        MailMessage mail = new MailMessage();

        //set the addresses
        mail.From = new MailAddress("me@domain.com");            

        //set the content
        mail.Subject = "This is an email";
        mail.Body = "this is a sample body";
        mail.IsBodyHtml = false;    

        //send the message
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "emailservername";
        smtp.Port = 25;
        smtp.UseDefaultCredentials = true;
        smtp.Send(mail);            
    }
}
Run Code Online (Sandbox Code Playgroud)

异常消息

Inner Exception
{"Unable to read data from the transport connection: net_io_connectionclosed."}

Stack Trace
"   at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)\r\n   at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)\r\n   at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)\r\n   at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)\r\n   at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)\r\n   at System.Net.Mail.SmtpClient.GetConnection()\r\n   at System.Net.Mail.SmtpClient.Send(MailMessage message)"


Outer Exception
  System.Net.Mail.SmtpException was unhandled
  Message="Failure sending mail."
  Source="System"
  StackTrace:
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at LabDemos.Mailer.test() in C:\Users\username\Documents\Visual Studio 2008\Projects\LabDemos\LabDemos\Mailer.cs:line 40
       at LabDemos.Program.Main(String[] args) in C:\Users\username\Documents\Visual Studio 2008\Projects\LabDemos\LabDemos\Program.cs:line 48
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.IO.IOException
       Message="Unable to read data from the transport connection: net_io_connectionclosed."
       Source="System"
       StackTrace:
            at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
            at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
            at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
            at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
            at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
            at System.Net.Mail.SmtpClient.GetConnection()
            at System.Net.Mail.SmtpClient.Send(MailMessage message)
       InnerException: 
Run Code Online (Sandbox Code Playgroud)

小智 16

有几件事可能导致这个问题,这里有一些我发现的东西.问题:你提到"交换" - 这是一个交换服务器吗?主机是否需要身份验证(可能需要向客户端添加身份验证)?我首先尝试做的是将主机分配给静态IP地址而不是主机名,以查看它是否先工作.如果确实如此,则很可能是DNS映射存在问题.

如果使用虚拟化运行Exchange服务器,则需要将SmtpClient配置为虚拟服务器的主机IP,而不是物理主机服务器名称或IP.右键单击"默认SMTP虚拟服务器",选择"属性",然后转到"访问"选项卡,单击"中继",然后将发送SMTP请求的计算机的IP地址添加到SMTP服务器.(ASP.net网站)

如果这不起作用,那是因为服务器阻止您发送SMTP数据包.您需要确保将发送SMTP邮件的框添加到SMTP服务器.这是通过IIS的身份验证选项卡完成的.

我还想指出你应该处理邮件客户端,或者在"使用"语句中使用客户端.这将确保将"QUIT"发送到服务器并正常关闭连接.

using(SmtpClient smtp = new SmtpClient())
{
    smtp.Host = "emailservername";
    smtp.Port = 25;
    smtp.UseDefaultCredentials = true;
    smtp.Send(mail)
}
Run Code Online (Sandbox Code Playgroud)


小智 7

重启IIS.我知道这听起来很傻,要重新启动一切.(抱歉要碰到一个旧线程).但有时重启IIS会产生魔力.我遇到了完全相同的问题并重新启动解决了它.

可能已经发生暂时导致名称'localhost'无法解决.我只是发布在这里,以便现在面对它的人可能会尝试这个快速修复,然后再尝试进一步调查.希望能帮助到你


Sim*_*ver 6

如果您已在SMTP服务设置中指定了IP地址,请确保在IIS7中指定该计算机的IP地址而不是放入localhost.

IIS7可以轻松选择"localhost",但如果IP为10.0.0.1,则会导致此错误


小智 6

如果在IIS 7中使用localhost(Use Localhost),则将其更改为计算机的IP地址而不是localhost或127.0.0.1

也请按照以下链接相应地更新您的邮件服务器中继:

信箱不可用.服务器响应为:5.7.1无法中继abc@xyz.com


joh*_*taa 1

我发现 Vista Business 操作系统没有附带 IIS SMTP。我怀疑这就是拼图中缺失的一块。有人在这个问题上有运气吗?