SMTP Filter Problems????  
Author Message
Danny_Ganda





PostPosted: .NET Framework Networking and Communication, SMTP Filter Problems???? Top

We are in the middle of making a SMTP Filter in vb.net but we seem to be having a few problems.

When we use the following code we get what we would expect from the .net form (SMTPClient)

Dim infoRecieved As Integer
Dim byteArray(900000) As Byte
Dim msg As Byte() = Encoding.UTF8.GetBytes("220 OB-DEV3" & CrLf)

currentSocket.Send(msg, 0, msg.Length, 0)

infoRecieved = currentSocket.Receive(byteArray)

SMTPMessage = System.Text.Encoding.Default.GetString(byteArray)

MsgBox(SMTPMessage)

msg = Encoding.UTF8.GetBytes("250 OK" & CrLf)

currentSocket.Send(msg, 0, msg.Length, 0)

infoRecieved = currentSocket.Receive(byteArray)

SMTPMessage = Encoding.UTF8.GetString(byteArray)

MsgBox(SMTPMessage)

msg = Encoding.UTF8.GetBytes("250 Mail OK" & CrLf)

currentSocket.Send(msg, 0, msg.Length, 0)

As you can see we are manually sending back the SMTP commands the form would expect and in turn the form sends us the correct information.

However we are trying to make a SMTP filter not a SMTP server so we have got another connection to our Exchange 2003 server. When our SMTP filter receives a connection we make a new connection to the exchange server and send the SMTPClient what we receive from the exchange server. This seems to work for the first few messages but then the SMTPClient send an unexpected QUIT command and i cant work out why

Please see the code below and the messages we see the system process.

Dim currentSocket As Socket

Dim ExchangeSocket As New TcpClient

Dim exchangeEndPoint As New IPEndPoint(IPAddress.Parse("192.168.111.7"), 25)

Dim SMTPMessage As String

currentSocket = newSocket

ExchangeSocket.Connect(exchangeEndPoint)

Dim byteArray(9000000) As Byte

Dim infoRecieved As Integer = ExchangeSocket.Client.Receive(byteArray)

SMTPMessage = Encoding.UTF8.GetString(byteArray)

Dim msg As Byte() = Encoding.UTF8.GetBytes(SMTPMessage)

SMTPMessage = Encoding.UTF8.GetString(msg)

MsgBox(SMTPMessage)

currentSocket.Send(msg, 0, msg.Length, 0)

While (True)

infoRecieved = currentSocket.Receive(byteArray)

SMTPMessage = Encoding.UTF8.GetString(byteArray)

msg = Encoding.UTF8.GetBytes(SMTPMessage)

ExchangeSocket.Client.Send(msg, 0, msg.Length, 0)

infoRecieved = ExchangeSocket.Client.Receive(byteArray)

SMTPMessage = Encoding.UTF8.GetString(byteArray)

msg = Encoding.UTF8.GetBytes(SMTPMessage)

SMTPMessage = Encoding.UTF8.GetString(msg)

MsgBox(SMTPMessage)

currentSocket.Send(msg, 0, msg.Length, 0)

End While

We Get this from the exchange server and send it to the SMTPClient:-
220 ob-exchange.officebroker.uk Microsoft ESMTP MAIL Service, Version: 6.0.3790.1830 ready at Wed, 11 Oct 2006 10:12:52 +0100

The SMTPClient them send us back this message which we pass onto the Exchange server:-
EHLO OB-DEV3
e.officebroker.uk Microsoft ESMTP MAIL Service, Version: 6.0.3790.1830 ready at Wed, 11 Oct 2006 10:12:52 +0100

The Exchange Server then sends us this message which we again send back to the SMTPClient
250-ob-exchange.officebroker.uk Hello [192.168.111.72]
250-TURN
250-SIZE
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-X-EXPS GSSAPI NTLM LOGIN
250-X-EXPS=LOGIN
250-AUTH GSSAPI NTLM LOGIN
250-AUTH=LOGIN
250-X-LINK2STATE
250-XEXCH50
250 OK

The SMTPClient then sends us this unexpected message:-
QUIT
-exchange.officebroker.uk Hello [192.168.111.72]
250-TURN
250-SIZE
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-X-EXPS GSSAPI NTLM LOGIN
250-X-EXPS=LOGIN
250-AUTH GSSAPI NTLM LOGIN
250-AUTH=LOGIN
250-X-LINK2STATE
250-XEXCH50
250 OK

as you can see, when the SMTPClient sends us back the messages it omits the first few characters of the message we sent it, im not sure if this is normal behavior or not

the SMTP Client we are using the send the E-Mail is the one that comes with .net 2.0



.NET Development17