Re: Sockets Hanging with SMTP server code

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Tom Shelton (tom_at_YOUKNOWTHEDRILLmtogden.com)
Date: 10/28/04


Date: Thu, 28 Oct 2004 15:46:51 -0700

In article <3cb859f7.0410280820.6151ccf3@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]


Relevant Pages

  • Re: Save File to Different Domain - 2nd Post
    ... maybe the user doesn't have logon permissions on the server ... > impersonate a user with the proper permissions on the file server: ... > Sometimes you might need to obtain a Windows NT account token to ... > Imports System.Collections ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • comboboxes.selectedvalue not getting passed to the connection string
    ... the user types the server name and then clicks on button 'CONNECT' to ... Imports System.Data ... Private Sub Mainform_Load(ByVal sender As System.Object, ... Dim objServers As DataTable ...
    (microsoft.public.dotnet.general)
  • Re: Code sample
    ... You need to dispose the server object, setting it to null will not release it. ... a Class Library called NeverDies to contain Immortal.vb, ... Imports System.EnterpriseServices ... Dim imm As New NeverDies.Immortal ...
    (microsoft.public.dotnet.framework.component_services)
  • Re: Code sample
    ... You need to dispose the server object, setting it to null will not release it. ... a Class Library called NeverDies to contain Immortal.vb, ... Imports System.EnterpriseServices ... Dim imm As New NeverDies.Immortal ...
    (microsoft.public.dotnet.framework.component_services)
  • Re: How do you transfer data from one table to another
    ... data by doing exports and imports using scripts. ... In FileMaker 8, is there a way to easily transfer records from one ... but that may present a problem with the server software. ...
    (comp.databases.filemaker)