473,769 Members | 1,723 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.Sock ets
Imports System.Text

Module Module1

Sub Main()
Connect("smtpse rver", "")
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(serve r, port)

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

' Get a client stream for reading and writing.
' Stream stream = client.GetStrea m();
Dim stream As NetworkStream = client.GetStrea m()
' Receive the TcpServer.respo nse.
' 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(dat a, 0, data.Length)
responseData = System.Text.Enc oding.ASCII.Get String(data, 0, bytes)
Console.WriteLi ne("Received: {0}", responseData)
data = System.Text.Enc oding.ASCII.Get Bytes("HELO smtpserver.com" )
stream.Write(da ta, 0, data.Length)
stream.Flush()
'This next line prints out just fine
Console.WriteLi ne("Sent -> HELO smtpserver.com" )

Here is the part that is hanging
------------------------------------------------------------------------
bytes = stream.Read(dat a, 0, data.Length)
responseData = System.Text.Enc oding.ASCII.Get String(data, 0, bytes)
---------------------------------------------------------------------------

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

'Never gets here
Console.WriteLi ne("Never gets here")

' Close everything.
client.Close()
Catch e As ArgumentNullExc eption
Console.WriteLi ne("ArgumentNul lException: {0}", e)
Catch e As SocketException
Console.WriteLi ne("SocketExcep tion: {0}", e)
End Try

Console.WriteLi ne(ControlChars .Cr + " Press Enter to continue...")
Console.Read()
End Sub 'Connect
Nov 21 '05 #1
2 2073

data = System.Text.Enc oding.ASCII.Get Bytes("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.Sock ets
Imports System.Text

Module Module1

Sub Main()
Connect("smtpse rver", "")
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(serve r, port)

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

' Get a client stream for reading and writing.
' Stream stream = client.GetStrea m();
Dim stream As NetworkStream = client.GetStrea m()
' Receive the TcpServer.respo nse.
' 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(dat a, 0, data.Length)
responseData = System.Text.Enc oding.ASCII.Get String(data, 0, bytes)
Console.WriteLi ne("Received: {0}", responseData)
data = System.Text.Enc oding.ASCII.Get Bytes("HELO smtpserver.com" )
stream.Write(da ta, 0, data.Length)
stream.Flush()
'This next line prints out just fine
Console.WriteLi ne("Sent -> HELO smtpserver.com" )

Here is the part that is hanging
------------------------------------------------------------------------
bytes = stream.Read(dat a, 0, data.Length)
responseData = System.Text.Enc oding.ASCII.Get String(data, 0, bytes)
---------------------------------------------------------------------------

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

'Never gets here
Console.WriteLi ne("Never gets here")

' Close everything.
client.Close()
Catch e As ArgumentNullExc eption
Console.WriteLi ne("ArgumentNul lException: {0}", e)
Catch e As SocketException
Console.WriteLi ne("SocketExcep tion: {0}", e)
End Try

Console.WriteLi ne(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.Sock ets

Module Module1

Sub Main()
' Declare a socket, and get our servers address
Dim connection As New Socket(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p)
Dim serverIP As IPAddress =
Dns.GetHostByNa me("smtpserver. com").AddressLi st(0)

' Connect the socket
connection.Conn ect(New IPEndPoint(serv erIP, 25))

' Create a reader and a writer
Dim reader As New StreamReader(Ne w NetworkStream(c onnection,
False))
Dim writer As New StreamWriter(Ne w NetworkStream(c onnection,
True))

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

writer.WriteLin e("HELO smtpserver.com" )
Console.WriteLi ne(reader.ReadL ine())

connection.Clos e()
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
2604
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 the SMTP info header. Using a proxy to investigate, I found that what is getting sent to the server is only an 0a character not an 0d0a sequence, any help on this would be great appreciated. I've already tried sending \r\n and that makes no...
3
2825
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 then set the new client socket to BeginReceive(). My problem: When two client socket connections send data
9
2458
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 data? How do I simulate the DataArrival event from Winsock (VB 6.0)? How can I create and alternate event? This also is the case for the tcpClient control. If you have any solution please tell me if they can be applied to the
10
1994
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 instead of using the FTP protocol. Well im familar with sockets but some concepts im not able to get it. I would be using Vb.Net as a server program but the question pampering my brain is for eg .. ip 192.168.0.0.1 tries to communicate on the...
7
1425
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 a problem if all the client are connected and sending a large quantity of bytes simultaneously to the same port of the server ?
2
7126
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 short while now using a server program a former coworker started. The program listens on a port for incomming connections. When a valid connection is made (we send this init string into the socket from the clients) the server closes the socket so...
1
1342
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 Until Listening = False sb.Socket = Listener.AcceptSocket() sb.Socket.BeginReceive(sb.Buffer, 0, sb.Buffer.Length, 0,
1
1303
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 hangs and it won't process any requests coming from a php script until "I restart it" . When the chat server hangs (I mean when it won't process any requests ) , I could n't even browse any web sites from the system in
1
4470
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 (re)connection (hitting the submit button) then I start to recieve a an error: IOException:System.IO.IOException: Der kunne ikke læses data fra transportforbindelsen (LPF: could not read data from the transport
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5298
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
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 we have to send another system
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.