【问题标题】:how to listen on port 25 with vb.net如何使用 vb.net 监听 25 端口
【发布时间】:2013-07-06 00:17:41
【问题描述】:

我基本上是在尝试为邮件服务器上的传入电子邮件编写垃圾邮件过滤器。我想编写一个 VB.NET 程序,它可以侦听端口 25 上的任何传入邮件,然后在其上运行我的脚本,然后将其传递给在不同端口上运行的邮件服务器。

我需要怎么做才能让我的程序坐下来等待消息从端口 25 进入然后对其做出反应?

谢谢。

【问题讨论】:

  • 你做了什么来尝试自己解决这个问题?
  • 试图在互联网上找到任何关于监听端口 25 的引用,但结果为空。
  • 你错过这个问题了吗:stackoverflow.com/questions/627031/…
  • vb.net 和 C# 本质上是一样的,因此很容易找到在线服务将 C# 代码转换为 VB.net:developerfusion.com/tools/convert/vb-to-csharp 另外,因为 C# 是一个更常见的使用过的语言,你会发现 C# 中的示例/教程比 VB.net 多得多,因此如果不编写 C#,花时间开发一个至少用于阅读的工具是值得的。最后,如果你正在寻求帮助,如果你有耐心和礼貌,你会更成功。
  • 你最好在你的邮件服务器中实现某种过滤插件,而不是重新构建所有的 SMTP 东西。

标签: vb.net email filter spam


【解决方案1】:

作为一个例子,这里是我不久前在 VB.NET 的教程中修改的套接字侦听服务的一部分。基本上,当服务启动时,套接字会在端口 25 上侦听流量,接受连接,然后将该连接分配给新线程,发送响应,然后关闭 TCP 连接。

Dim serverSocket As New TcpListener(IPAddress.Any, "25")
Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName()).AddressList(0)
Dim ipLocalEndPoint As New System.Net.IPEndPoint(IPAddress, 25)

Protected Overrides Sub OnStart(ByVal args() As String)
    Dim listenThread As New Thread(New ThreadStart(AddressOf ListenForClients))
    listenThread.Start()
End Sub

Protected Overrides Sub OnStop()
    ' Add code here to perform any tear-down necessary to stop your service.
End Sub

Private Sub ListenForClients()
    serverSocket = New TcpListener(ipLocalEndPoint)
    serverSocket.Start()
    While True
        Dim client As TcpClient = Me.serverSocket.AcceptTcpClient
        Dim clientThread As New Thread(New ParameterizedThreadStart(AddressOf HandleClientComm))
        clientThread.Start(client)
    End While
End Sub

Private Sub HandleClientComm(ByVal client As Object)
    Dim tcpClient As TcpClient = DirectCast(client, TcpClient)
    Dim clientStream As NetworkStream = tcpClient.GetStream

    Dim message As Byte() = New Byte(4095) {}
    Dim bytesRead As Integer

    While True
        If (bytesRead = 0) Then
            Exit While
        End If
        Dim encoder As New asciiencoding()
        Dim serverResponse As String = "Response to send"
        'Response to send back to the testing client
        Dim sendBytes As [Byte]() = encoding.ascii.getbytes(serverResponse)
        clientStream.Write(sendBytes, 0, sendBytes.Length)
    End While
    tcpClient.Close()
End Sub

【讨论】:

  • 谢谢戴夫。感谢您提供有用的答案。
猜你喜欢
  • 2021-12-16
  • 1970-01-01
  • 2016-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-29
  • 2011-12-15
  • 2013-03-29
相关资源
最近更新 更多