Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Imports System.Threading
- Imports System.Net
- Imports System.Net.Sockets
- Imports System.Text
- Public Class Form1
- 'Add a TEXTBOX1 to form
- 'Add a TEXTBOX1 to form
- 'Add a TEXTBOX1 to form
- 'Add a TEXTBOX1 to form
- Private Server As Sockets.TcpListener
- Private Port As Integer = 25
- Private Thread As Thread
- Delegate Sub LogCallback(S As String)
- Sub Log(S As String)
- If Me.TextBox1.InvokeRequired Then
- Dim d As LogCallback = AddressOf Log
- Me.Invoke(d, S)
- Else
- Me.TextBox1.AppendText(S.Replace("n", vbCrLf))
- End If
- End Sub
- Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
- Me.Text = String.Format("Listening port {0} v{1}", Port, AppVersion)
- Thread = New Thread(New ThreadStart(AddressOf Listening))
- Thread.Start()
- End Sub
- Async Function Listening() As Task
- Server = New Sockets.TcpListener(Net.IPAddress.Any, Port)
- Server.Start()
- Log("Starting listening on port 25nn")
- Do
- Dim HandleThread As New Thread(New ParameterizedThreadStart(AddressOf HandleConnection))
- HandleThread.Start(Await Server.AcceptTcpClientAsync)
- Loop
- End Function
- Async Function HandleConnection(TcpClient As TcpClient) As Task
- Dim Stream As NetworkStream = TcpClient.GetStream
- Log(String.Format("nClient connected: {0}n", TcpClient.Client.RemoteEndPoint.ToString))
- Await R(Stream, String.Concat("220 your.mail.server.com Service ready", vbCrLf))
- While True
- Do
- Dim BuffSize As Integer = 4096
- Dim Buffer As Byte() = New Byte(BuffSize - 1) {}
- Dim BytesRead As Integer
- BytesRead = Await Stream.ReadAsync(Buffer, 0, Buffer.Length)
- If BytesRead = 0 Then Exit Function
- Dim Msg As String = Encoding.ASCII.GetString(Buffer.Take(BytesRead).ToArray)
- Log(String.Concat("<<< ", Msg))
- If Msg.StartsWith("EHLO") Then
- Await R(Stream, "502 Command not implemented")
- ElseIf Msg.StartsWith("HELO") Then
- Await R(Stream, "250 your.mail.server.com")
- ElseIf Msg.StartsWith("MAIL") Or Msg.StartsWith("RCPT") Or Msg.StartsWith("Date") Or Msg.StartsWith("From") Then
- Await R(Stream, "250 OK")
- ElseIf Msg.StartsWith("DATA") Then
- Await R(Stream, "354 Start mail input; end with <CRLF>.<CRLF>")
- ElseIf Msg.StartsWith("From") Or Msg.StartsWith("Received") Then
- Await R(Stream, "250 OK")
- ElseIf Msg = vbCrLf Then
- Await R(Stream, "250 OK")
- ElseIf Msg.StartsWith("QUIT") Then
- Await R(Stream, "221 BBN-UNIX.ARPA Service closing transmission channel")
- Stream.Close()
- TcpClient.Close()
- Exit Function
- Else
- Await R(Stream, "502 Command not implemented")
- End If
- Loop While Stream.DataAvailable
- End While
- End Function
- Private AppVersion As Integer = 8
- Async Function R(Stream As NetworkStream, Message As String) As Task
- Dim ResponseBuffer As Byte() = Encoding.ASCII.GetBytes(String.Concat(Message, vbCrLf))
- Await Stream.WriteAsync(ResponseBuffer, 0, ResponseBuffer.Length)
- Log(String.Concat(">>> ", Message, vbCrLf, vbCrLf))
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement