基本的に、メールサーバーで受信メール用のスパムフィルターを作成しようとしています。ポート 25 で受信メールをリッスンできる VB.NET プログラムを作成し、そのスクリプトでスクリプトを実行して、別のポートで実行されているメール サーバーに渡したいと考えています。
プログラムをただ座ってメッセージがポート 25 に着信するのを待ち、それに反応するにはどうすればよいですか?
ありがとう。
ここでは、例として、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