Click here to Skip to main content
15,896,111 members
Articles / Programming Languages / C#

A .NET Wizard control

Rate me:
Please Sign up or sign in to vote.
4.86/5 (89 votes)
24 Apr 2003CPOL7 min read 667.6K   8.7K   216  
A .NET Wizard control for the VS.IDE and client apps
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Diagnostics;

namespace UtilityLibrary.Smtp
{ 
  /// <summary>
  /// Class for handling an SMTP connection.
  /// </summary>
  internal class SmtpConnection
  {
    #region Class members
    private TcpClient socket;
    private StreamReader reader;
    private StreamWriter writer;
    private bool connected;
    #endregion

    #region Class Constructor
    /// <summary>
    /// Create a new connection.
    /// </summary>
    internal SmtpConnection()
    {
      socket = new TcpClient();     
    }
    #endregion 

    #region Class methods
    /// <summary>
    /// Connection status.
    /// </summary>
    public bool Connected
    {
      get{return connected;}
    }

    /// <summary>
    /// Open connection with specified host and default port.
    /// </summary>
    /// <param name="host">Host name to connect to.</param>
    internal void Open(string host)
    {
      Open(host, 25);
    }
    
    /// <summary>
    /// Open connection to host on port.
    /// </summary>
    /// <param name="host">Host name to connect to.</param>
    /// <param name="port">Port to connect to.</param>
    internal void Open(string host, int port)
    {
      if(host == null || host.Trim().Length == 0 || port <= 0)
      {
        throw new System.ArgumentException("Invalid Argument found.");
      }
      socket.Connect(host, port);
      reader = new StreamReader(socket.GetStream(), System.Text.Encoding.ASCII);
      writer = new StreamWriter(socket.GetStream(), System.Text.Encoding.ASCII);
      connected = true;
    }
    
    /// <summary>
    /// Close connection.
    /// </summary>
    internal void Close()
    {
      reader.Close();
      writer.Flush();
      writer.Close();
      reader = null;
      writer = null;
      socket.Close();
      connected = false;
    }

    /// <summary>
    /// Send a command to the server.
    /// </summary>
    /// <param name="cmd">Command to send.</param>
    internal void SendCommand(string cmd)
    {
#if DEBUG
      Trace.WriteLine( cmd, "TO SMTP" );
#endif

      writer.WriteLine( cmd );
      writer.Flush();
    }

    /// <summary>
    /// Send data to the server. Used for sending attachments.
    /// </summary>
    /// <param name="buf">Data buffer.</param>
    /// <param name="start">Starting position in buffer.</param>
    /// <param name="length">Length to send.</param>
    internal void SendData(char[] buf, int start, int length)
    {
      writer.Write(buf, start, length);
    }

    /// <summary>
    /// Get the reply message from the server.
    /// </summary>
    /// <param name="reply">Text reply from server.</param>
    /// <param name="code">Status code from server.</param>
    internal void GetReply(out string reply, out int code)
    {
      reply = reader.ReadLine();

#if DEBUG
      Trace.WriteLine( reply, "FROM SMTP" );
#endif

      code = ( reply == null ) ? -1 : Int32.Parse(reply.Substring(0, 3));     
    }
    #endregion
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO ArtfulBits Inc.
Ukraine Ukraine
Name:Kucherenko Oleksandr

Born:September 20, 1979

Platforms: Win32, Linux; - well known and MS-DOS; Win16; OS/2 - old time not touched;

Hardware: IBM PC

Programming Languages: Assembler (for Intel 80386); Borland C/C++; Borland Pascal; Object Pascal; Borland C++Builder; Delphi; Perl; Java; Visual C++; Visual J++; UML; XML/XSL; C#; VB.NET; T-SQL; PL/SQL; and etc.

Development Environments: MS Visual Studio 2001-2008; MS Visual C++; Borland Delphi; Borland C++Builder; C/C++ any; Rational Rose; GDPro; Together and etc.

Libraries: STL, ATL, WTL, MFC, NuMega Driver Works, VCL; .NET 1.0, 1.1, 2.0, 3.5; and etc.

Technologies: Client/Server; COM; DirectX; DirectX Media; BDE; HTML/DHTML; ActiveX; Java Servlets; DCOM; COM+; ADO; CORBA; .NET; Windows Forms; GDI/GDI+; and etc.

Application Skills: Databases - design and maintain, support, programming; GUI Design; System Programming, Security; Business Software Development. Win/Web Services development and etc.

Comments and Discussions