Sockets Hanging with SMTP server code

D

Dave

Hello,

I am trying to write an application that will talk to an SMTP server
using sockets. I can connect to the server just fine, then I can
receive the first message from the server. I can then send out a
command to the server, but after that the thing just hangs when I am
waiting for a response. I can't understand why this is happening. I am
new to this language, so this might be something obvious that I am
overlooking. Any help would be great. Here is the code. I based this
code off an example from msdn.

Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Module Module1

Sub Main()
Connect("smtpserver", "")
End Sub

Sub Connect(ByVal server As [String], ByVal message As [String])
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim port As Int32 = 25
Dim client As New TcpClient(server, port)

' Translate the passed message into ASCII and store it as a Byte
array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)

' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()


' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}

' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty

' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)


data = System.Text.Encoding.ASCII.GetBytes("HELO smtpserver.com")
stream.Write(data, 0, data.Length)
stream.Flush()
'This next line prints out just fine
Console.WriteLine("Sent -> HELO smtpserver.com")

Here is the part that is hanging
------------------------------------------------------------------------
bytes = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
---------------------------------------------------------------------------

Console.WriteLine("Received: {0}", responseData)

'Never gets here
Console.WriteLine("Never gets here")

' Close everything.
client.Close()
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try

Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
Console.Read()
End Sub 'Connect
 
G

Guest

data = System.Text.Encoding.ASCII.GetBytes("HELO smtpserver.com" & vbCrLf)
vbCrLf means end of command. Otherwise SMTP server never responds your
command.
 
T

Tom Shelton

Hello,

I am trying to write an application that will talk to an SMTP server
using sockets. I can connect to the server just fine, then I can
receive the first message from the server. I can then send out a
command to the server, but after that the thing just hangs when I am
waiting for a response. I can't understand why this is happening. I am
new to this language, so this might be something obvious that I am
overlooking. Any help would be great. Here is the code. I based this
code off an example from msdn.

<snip>

Simple code example:

Option Strict On
Option Explicit On

Imports System
Imports System.IO
Imports System.Text
Imports System.Net
Imports System.Net.Sockets

Module Module1

Sub Main()
' Declare a socket, and get our servers address
Dim connection As New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Dim serverIP As IPAddress =
Dns.GetHostByName("smtpserver.com").AddressList(0)

' Connect the socket
connection.Connect(New IPEndPoint(serverIP, 25))

' Create a reader and a writer
Dim reader As New StreamReader(New NetworkStream(connection,
False))
Dim writer As New StreamWriter(New NetworkStream(connection,
True))

writer.AutoFlush = True
writer.NewLine = vbCrLf ' so this will be protable to Unix :)

writer.WriteLine("HELO smtpserver.com")
Console.WriteLine(reader.ReadLine())

connection.Close()
End Sub

End Module

SMTP commands need to be terminated with a CrLF pair :)
HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top