473,511 Members | 17,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sockets Hanging with SMTP server code

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
Nov 21 '05 #1
2 2059

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

"Dave" wrote:
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

Nov 21 '05 #2
In article <3c**************************@posting.google.com >, Dave wrote:
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
--
Tom Shelton [MVP]
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2594
by: Iceberg | last post by:
Hi, I'm using ActivePerl on Win98 and this is starting to annoy me. Why isn't \n accepted as a carriage return when I'm using sockets? If I run the following program, I get no response other than...
3
2803
by: Corne Oosthuizen | last post by:
I'm writing a Telnet Server application using Asynchronous sockets. I spawn a listener thread to handel incomming connections and create a separate client socket for each new connection. I...
9
2444
by: Gita George | last post by:
I'm trying to write a program (a pop3 mail checker) and I'm having a problem. I'm using the socket control and I've noticed that I do not have any events !? How do I use the socket to receive...
10
1975
by: altaf.sunesara | last post by:
Hello ! I have some devices which communicate trough LAN to the server the old form i was using was FTP as a communication between Server and Client i have decided to use personal TCP sockets...
7
1414
by: Steven | last post by:
Hi, Thanks all for you help with my "socket and buffer size" question. I have decided to use async ones, and my code is working pretty well. But i still have a non-answered question: Is it...
2
7095
by: jasonsgeiger | last post by:
From: "Factor" <jasonsgeiger@gmail.com> Newsgroups: microsoft.public.in.csharp Subject: Multiple Clients, One port Date: Wed, 19 Apr 2006 09:36:02 -0700 I'm been working with sockets for a...
1
1324
by: iMaiden | last post by:
I am building a simple smtp server to receive email as follows: Private Sub Listen() Dim Listener As New TcpListener("25") Dim sb As New SocketAndBuffer() Listening = True Listener.Start() Do...
1
1290
by: Prasad | last post by:
Hi group, I have written a web -based chat server in VC++ .. I connect to this server from a php script to send and receive messages . It's working fine .. But, some times the chat server...
1
4438
by: larspeter | last post by:
Hi all. I have a problem with TcpClient ... I am conneting to a server with TcpClient and returning the answer through a webservice. It actully all works fine. BUT if I make a lot of...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.