Home | Download | Library | About | Blog

How to create SMTP Server with Visual Basic .NET


The code bellow shows how to write a synchronous TCP listener which listens on port 25 for SMTP messages. This is just a head start, not a full implementation of the SMTP protocol.

This example is written based on RFC-821 (SMTP).

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic

Public Class SynchronousSocketListener

   ' Incoming data from the client.
   Public Shared data As String = Nothing


   Public Shared Function Main(ByVal args() As [String]) As Integer
      ' Data buffer for incoming data.
      Dim bytes() As Byte = New [Byte](1024) {}

      ' Establish the local endpoint for the socket.
      ' Dns.GetHostName returns the name of the host running the application.
      Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
      Dim localEndPoint As New IPEndPoint(ipHostInfo.AddressList(0), 25)
      ' Create a TCP/IP socket.
      Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

      ' Bind the socket to the local endpoint and listen for incoming connections.
      Try
         listener.Bind(localEndPoint)
         listener.Listen(10)

         ' Start listening for connections.
         While True
            Console.WriteLine("Waiting for a connection...")
            ' Program is suspended while waiting for an incoming connection.
            Dim handler As Socket = listener.Accept()

            Console.WriteLine("Incoming connection ...")
            'Send 220 to show SMTP server is ready
            handler.Send(Encoding.ASCII.GetBytes("220 Test SMTP Service ready" & vbCrLf))

            While True
               bytes = New Byte(1024) {}
               Dim bytesRec As Integer = handler.Receive(bytes)
               data = Encoding.ASCII.GetString(bytes, 0, bytesRec)
               ' Show the data on the console.
               Console.WriteLine("Text received : {0}", data)

               'Process Commands
               Dim CMD As String = data.Substring(0, 4).ToUpper
               Select Case CMD
                  Case "HELO"
                     handler.Send(Encoding.ASCII.GetBytes("250 OK" & vbCrLf))
                  Case "MAIL"
                     handler.Send(Encoding.ASCII.GetBytes("250 OK" & vbCrLf))
                  Case "RCPT"
                     Dim SentTo As String = data.Substring(8).Trim
                     If SentTo.ToLower <> "<lookup>" Then
                        handler.Send(Encoding.ASCII.GetBytes("550 No such user here" & vbCrLf))
                     Else
                        handler.Send(Encoding.ASCII.GetBytes("250 OK" & vbCrLf))
                     End If
                  Case "DATA"
                     handler.Send(Encoding.ASCII.GetBytes("354 Start mail input; end with ." & vbCrLf))
                  Case "QUIT"
                     handler.Send(Encoding.ASCII.GetBytes("221 Service closing transmission channel" & vbCrLf))
                     Exit While
                  Case Else
                     Dim Message As String
                     While data <> vbCrLf & "." & vbCrLf
                        Message += data
                        bytesRec = handler.Receive(bytes)
                        data = Encoding.ASCII.GetString(bytes, 0, bytesRec)
                     End While
                     handler.Send(Encoding.ASCII.GetBytes("250 OK" & vbCrLf))
                     'Process Message Here
               End Select

            End While

            'Close Connection
            handler.Shutdown(SocketShutdown.Both)
            handler.Close()
         End While

      Catch e As Exception
         Console.WriteLine(e.ToString())
      End Try

      Console.WriteLine(ControlChars.Cr + "Press ENTER to continue...")
      Console.Read()
      Return 0
   End Function

End Class


To test this SMTP server, use the following VB.NET code. Make sure to change the SmtpServer address to address of the computer running above code.

Dim mySMTPMail As Web.Mail.SmtpMail
mySMTPMail.SmtpServer = "192.168.2.100"
mySMTPMail.Send("me", "lookup", "Subject", "Body")

Home - Download - Library - About - Blog

Copyright 1999 - 2024, MazSoft.com