Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Sending Mail in VB.NET using Sockets

Total Hit ( 3525)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


This class provides all the functionality. This class was implemented without error handing on purpose to make it easier to understand the program flow.

Click here to copy the following block
Imports System.IO
Imports System.Net.Sockets
Imports System.Text

Public Class SocketMail
  Private _subject As String
  Private _to As String
  Private _server As String
  Private _port As Integer = 25
  Private _body As String
  Private _date As Date = Now
  Private _from As String
  Private _cc As String
  Private _bcc As String

  Private ns As NetworkStream
  Private client As New TcpClient()

  Public Sub New()
  End Sub

  Public Sub New(ByVal server As String, ByVal port As Integer)
    _server = server
    _port = port
  End Sub

  Public Sub New(ByVal server As String, ByVal port As Integer, _
          ByVal from As String, ByVal [to] As String, _
           ByVal subject As String, ByVal message As String)
    _server = server
    _port = port
    _from = from
    _to = [to]
    _subject = subject
    _body = message
  End Sub

  Public Property SMTPServer() As String
    Get
      Return _server
    End Get
    Set(ByVal Value As String)
      _server = Value
    End Set
  End Property

  Public Property [To]() As String
    Get
      Return _to
    End Get
    Set(ByVal Value As String)
      _to = Value
    End Set
  End Property

  Public Property From() As String
    Get
      Return _from
    End Get
    Set(ByVal Value As String)
      _from = Value
    End Set
  End Property

  Public Property CarbonCopy() As String
    Get
      Return _cc
    End Get
    Set(ByVal Value As String)
      _cc = Value
    End Set
  End Property

  Public Property BlindCarbonCopy() As String
    Get
      Return _bcc
    End Get
    Set(ByVal Value As String)
      _bcc = Value
    End Set
  End Property


  Public Property Port() As Integer
    Get
      Return _port
    End Get
    Set(ByVal Value As Integer)
      _port = Value
    End Set
  End Property

  Public Property Message() As String
    Get
      Return _body
    End Get
    Set(ByVal Value As String)
      _body = Value
    End Set
  End Property

  Public Property SendDate() As Date
    Get
      Return _date
    End Get
    Set(ByVal Value As Date)
      _date = Value
    End Set
  End Property

  Public Property Subject() As String
    Get
      Return _subject
    End Get
    Set(ByVal Value As String)
      _subject = Value
    End Set
  End Property

  Public Function Open()
    client.Connect(_server, _port)

    ns = client.GetStream()

    Dim strMessage As String = "HELO TEST" & ControlChars.CrLf

    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(strMessage)
    ns.Write(sendBytes, 0, sendBytes.Length)
  End Function

  Public Function Close()
    Try
      Dim strMessage As String = "QUIT" & ControlChars.CrLf

      Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(strMessage)
      ns.Write(sendBytes, 0, sendBytes.Length)
    Catch

    End Try

    client.Close()
  End Function

  Public Function Send() As Boolean
    Try
      Dim response As Integer
      Dim strMessage As String

      strMessage = "MAIL FROM:" & _from & ControlChars.CrLf & _
             "RCPT TO:" & _to & ControlChars.CrLf & _
             "DATA" & ControlChars.CrLf & _
             "date:" & _date.ToString & ControlChars.CrLf & _
             "from:" & _from & ControlChars.CrLf & _
             "to:" & _to & ControlChars.CrLf & _
             "cc:" & _cc & ControlChars.CrLf & _
             "bcc:" & _bcc & ControlChars.CrLf & _
             "subject:" & _subject & ControlChars.CrLf & _
             _body & ControlChars.CrLf & _
             "." & ControlChars.CrLf

      Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(strMessage)
      ns.Write(sendBytes, 0, sendBytes.Length)
    Finally
      client.Close()
    End Try

    Return True
  End Function

End Class


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.