SmtpConnection.cs :  » RSS-RDF » Aggie » Bitworking » Smtp » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » RSS RDF » Aggie 
Aggie » Bitworking » Smtp » SmtpConnection.cs
// SmtpConnection.cs
//
// This file is based (in parts) on SmtpEmailer code by Steaven Woyan (mailto:swoyan@hotmail.com),
// and by extension on logic/code design from PJ Naughter's C++ SMTP package
// located at http://www.naughter.com/smpt.html.
//
// The original code has been adapted for use in Aggie by Ziv Caspi.
// See SmtpBatchMailer.cs for a list of changes.

using System;
using System.IO;
using System.Net.Sockets;

namespace Bitworking.Smtp{
  public class SmtpConnection {
    #region Private data
    private TcpClient    socket;
    private StreamReader reader;
    private StreamWriter writer;
    private bool         connected;
    private bool         offline;
    #endregion

    #region Properties
    public bool Offline {
      get { return offline; }
      set { offline = value; } // TODO: Useful only if the connection has not been started yet
    }
    public bool Connected {
      get { return connected; }
    }
    #endregion

    #region Construction and destruction
    public SmtpConnection() {
      socket = new TcpClient();
    }
    public void Open( string host ) {
      Open( host, 25 );
    }
    public void Open( string host, int port ) {
      if ( !offline ) {
        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;
    } // Open( string, int )
    public void Close() {
      if ( !offline ) {
        reader.Close();
        writer.Flush();
        writer.Close();
        reader = null;
        writer = null;
        socket.Close();
      }
      connected = false;
    } // Close
    #endregion

    #region Data exchange
    public void SendCommand( string cmd ) {
      // TODO: (ZIVC) We need better debugging support...
      if ( offline ) {
        // System.Diagnostics.Debug.WriteLine( cmd );
        // System.Diagnostics.Debug.Flush();
      }

      if ( !offline ) {
        writer.WriteLine(cmd);
        writer.Flush();
      }
    } // SendCommand

    public void SendData( char[] buf, int index, int length ) {
      if ( !offline )
        writer.Write(buf, index, length);
    } // SendData

    public void GetReply( out string reply, out int code ) {
      // TODO: Better error control here
      if ( !offline ) {
        while ( true ) {
          reply = reader.ReadLine();
          if ( reply.Length < 4 ) {
            code = -1;
            break;
          }
          if ( reply[3] == '-' )
            continue; // Multiple response lines. Get only the last one (currently!)
          code = ((reply == null)
            ? -1 
            : Int32.Parse( reply.Substring( 0, 3 ) ));
          break;
        }
      }
      else {
        reply = "100 OFFLINE MODE";
        code = 100;
      }
    } // GetReply
    #endregion

  } // class SmtpConnection
} // namespace Bitworking.Smtp

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.