2009-07-17 190 views
37

无法测试从Windows Vista Business中的.NET代码发送电子邮件。“无法从传输连接读取数据:net_io_connectionclosed。” - Windows Vista Business和SMTP

我正在写代码,一旦它被证实,我将迁移到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("[email protected]");    

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

异常消息

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: 
+0

我不打算为这个问题提供答案,因为我对SMTP感觉不太满意。但是,这听起来像是一个环境问题(可能在IIS设置中)。您是否使用1)“localhost”作为smtp.Host 2)用于smtp.Host的远程服务器和/或3)特定的IP而不是主机名? – 2009-07-20 11:08:43

+0

我的邮件主机正在通过名称引用。例如“交换”。我通过ping服务器来测试DNS。这是成功的。我还通过telnet测试了端口访问端口25.这也是成功的。我已经在另一台机器上测试了代码并能够发送消息。在我看来,我需要在机器上安装某种类型的SMTP服务/协议。代码失败的机器是Windows Vista Business。在Windows Server 2008 Standard上测试成功。 – 2009-07-22 04:03:00

+0

请参考: http://stackoverflow.com/questions/13689265/net-3-5-smtpclient-failure-sending-email-works-only-after-restarting-everyt/14272912#14272912 – 2013-01-11 06:36:34

回答

1

我发现Vista Business OS不附带IIS SMTP。我怀疑这是拼图的缺失部分。有没有人有这个问题的运气?

6

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

IIS7可以很容易地选择“本地主机”,但是,这将导致这个错误,如果IP例如为10.0.0.1

1

是您的代码incompleted.Dash在识别it.Other那么检查SMTP正确组件安装或不 检查link text

3

Vista和Windows 7没有任何SMTP服务器,该设施已自Windows XP中删除,那么你需要设置你的SMTP服务器明确的,有几个工具可以在市场是什么,你可以检查一下。 另外,一旦配置了SMTP,请记得检查您将用于发送电子邮件的服务器名称。

0

升级到.NET 4.0和应用所有安全更新

16

有几件事情,可能会导致这个问题,这里有一些我找到的东西。 问题:您提到“Exchange” - 这是交换服务器吗?主机是否需要身份验证(也许您需要向客户端添加身份验证)?我会先尝试的是将主机分配给静态IP地址而不是主机名,以查看是否可以首先使用。如果确实如此,那很可能是DNS映射的问题。

If you are running your exchange server with Virtualization, you need to configure the SmtpClient to the host IP of the Virtual Server, not the physical hosting server name or IP. Right click on Default SMTP virtual server, select Properties, then go to Access tab, click Relay then add the IP address of the computer that send SMTP request to the SMTP server. (ASP.net site)

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

我也想指出,你应该处置邮件客户端,或在“using”语句中使用客户端。这将确保将“QUIT”发送到服务器并正常关闭连接。

using(SmtpClient smtp = new SmtpClient()) 
{ 
    smtp.Host = "emailservername"; 
    smtp.Port = 25; 
    smtp.UseDefaultCredentials = true; 
    smtp.Send(mail) 
} 
7

重新启动IIS。我知道这听起来很愚蠢,要做所有事情的重启。 (对不起,碰到一个旧线程)。但有时重新启动IIS的作品神奇。我遇到了完全相同的问题,并重新开始解决它。

可能发生的原因暂时名称'localhost'无法解析。我只是在这里发帖,以便现在面临的人可能会尝试这个快速修复,然后再尝试进一步调查。希望它可以帮助

1

我面对这个问题最后6小时,我对新的,所以我想在下面的方式

1来解决这个问题)我打开命令提示符(例如,按Ctrl + R型cmd并回车)。
2)然后检查SMTP服务器ping并检查响应。
3)如果它得到回应,那么它是可以的,并移动下一步4其他明智的smtp名称是错误的。
4)然后我通过telnet检查smtp端口(例如25)是否打开。
5)使用这个cmd我们检查SMTP响应。
6)远程登录 “yoursmtpname”“PORTNO”(例如远程登录smtp.gmail.com 25)
7)如果Telnet不能工作,请安装或从控制面板添加在添加新的功能。
8 )如果它的工作,然后就好像通过结果。
9)220 mx.google.com ESMTP。

使用上面的步骤,我们可以发现SMTP连接已经准备好要不要发送邮件。

2

我因为我在我的服务器上有多个IP,所以有同样的问题ER(虚拟服务器)和我的主人指着本地主机,而我的SMTP虚拟服务器被分配到一个特定的IP:

<smtp deliveryMethod="Network"> 
     <network host="localhost" port="25" defaultCredentials="false"/> 
</smtp> 

这有2个解决方案:

  1. 更改代码并将主机设置为由SMTP虚拟服务器而不是本地主机使用的特定IP。
  2. 更改SMTP虚拟服务器IP到所有未分配

要查看/修改SMTP虚拟服务器IP: 默认SMTP虚拟服务器,选择属性上点击右键,在常规选项卡改变/看到的IP地址。

希望这可以节省一个人的同一天的问题。

1

我使用.NET Framework v4.5在VB中开发了一个Windows服务应用程序,最近遇到了这个问题。

首先,有一点背景 - 我试图处理错误“服务不可用,关闭传输通道。服务器响应是:错误:一个会话中消息太多”在发送每封电子邮件后调用SmtpClient对象上的Dispose方法。为了超级安全,我还在MailMessage对象上调用了Dispose。

好吧,现在'一个会话中消息太多'问题已解决,但现在我偶尔会收到'无法从传输连接读取数据:net_io_connectionclosed'错误。真棒。事实证明,我并没有始终如一地执行我的电子邮件课程 - 有一次,它正在被处置,而另一个则不是。

这里有什么解决这两个问题,我(这是在VB,但你的想法)的例子:

With New clsEmail(True) 
    With .Message 
     .To.Add("[email protected]") 
     .Subject = msgSubject 
     .Body = msgContents 
    End With 

    .Server.Send(.Message) 
    .Server.Dispose() 
    .Message.Dispose() 
End With 

我的电子邮件类有,除其他事项外,有2个属性 - 消息(System.Net .Mail.MailMessage)和服务器(System.Net.Mail.SmtpClient),这是本例中的配置。

在我的服务中发送电子邮件的每个实例都以这种方式实现后,我没有发生数百封电子邮件中的单个问题。

相关问题