SMTP based on TcpClient : Smtp Email « Socket Network « VB.Net Tutorial






SMTP based on TcpClient
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports System.Threading

Imports System.Windows.Forms

public class SMTPBasedOnTcpClient
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form


    Public Sub New()
        MyBase.New()

        InitializeComponent()

    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    Friend WithEvents btnSend As System.Windows.Forms.Button
    Friend WithEvents lblContent As System.Windows.Forms.Label
    Friend WithEvents lblSubject As System.Windows.Forms.Label
    Friend WithEvents lblTo As System.Windows.Forms.Label
    Friend WithEvents lblFrom As System.Windows.Forms.Label
    Friend WithEvents txtTo As System.Windows.Forms.TextBox
    Friend WithEvents txtContent As System.Windows.Forms.TextBox
    Friend WithEvents txtSubject As System.Windows.Forms.TextBox
    Friend WithEvents txtFrom As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents txtResponse As System.Windows.Forms.TextBox
    Friend WithEvents lblServer As System.Windows.Forms.Label
    Friend WithEvents txtServer As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnSend = New System.Windows.Forms.Button()
        Me.lblContent = New System.Windows.Forms.Label()
        Me.lblSubject = New System.Windows.Forms.Label()
        Me.lblTo = New System.Windows.Forms.Label()
        Me.lblFrom = New System.Windows.Forms.Label()
        Me.txtTo = New System.Windows.Forms.TextBox()
        Me.txtContent = New System.Windows.Forms.TextBox()
        Me.txtSubject = New System.Windows.Forms.TextBox()
        Me.txtFrom = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.txtResponse = New System.Windows.Forms.TextBox()
        Me.txtServer = New System.Windows.Forms.TextBox()
        Me.lblServer = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'btnSend
        '
        Me.btnSend.Location = New System.Drawing.Point(430, 56)
        Me.btnSend.Name = "btnSend"
        Me.btnSend.Size = New System.Drawing.Size(133, 24)
        Me.btnSend.TabIndex = 26
        Me.btnSend.Text = "Send"
        '
        'lblContent
        '
        Me.lblContent.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblContent.ImageAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.lblContent.Location = New System.Drawing.Point(51, 125)
        Me.lblContent.Name = "lblContent"
        Me.lblContent.Size = New System.Drawing.Size(62, 24)
        Me.lblContent.TabIndex = 25
        Me.lblContent.Text = "Message"
        '
        'lblSubject
        '
        Me.lblSubject.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblSubject.ImageAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.lblSubject.Location = New System.Drawing.Point(51, 100)
        Me.lblSubject.Name = "lblSubject"
        Me.lblSubject.Size = New System.Drawing.Size(62, 12)
        Me.lblSubject.TabIndex = 24
        Me.lblSubject.Text = "Subjet"
        '
        'lblTo
        '
        Me.lblTo.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblTo.ImageAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.lblTo.Location = New System.Drawing.Point(51, 69)
        Me.lblTo.Name = "lblTo"
        Me.lblTo.Size = New System.Drawing.Size(62, 16)
        Me.lblTo.TabIndex = 23
        Me.lblTo.Text = "To"
        '
        'lblFrom
        '
        Me.lblFrom.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblFrom.ImageAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.lblFrom.Location = New System.Drawing.Point(51, 45)
        Me.lblFrom.Name = "lblFrom"
        Me.lblFrom.Size = New System.Drawing.Size(62, 16)
        Me.lblFrom.TabIndex = 22
        Me.lblFrom.Text = "From"
        '
        'txtTo
        '
        Me.txtTo.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtTo.Location = New System.Drawing.Point(133, 64)
        Me.txtTo.Name = "txtTo"
        Me.txtTo.Size = New System.Drawing.Size(256, 22)
        Me.txtTo.TabIndex = 21
        Me.txtTo.Text = "tim@pie.com.tw"
        '
        'txtContent
        '
        Me.txtContent.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtContent.Location = New System.Drawing.Point(133, 120)
        Me.txtContent.Multiline = True
        Me.txtContent.Name = "txtContent"
        Me.txtContent.Size = New System.Drawing.Size(440, 48)
        Me.txtContent.TabIndex = 20
        Me.txtContent.Text = "This is a Test STMP Send Message "
        '
        'txtSubject
        '
        Me.txtSubject.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtSubject.Location = New System.Drawing.Point(133, 96)
        Me.txtSubject.Name = "txtSubject"
        Me.txtSubject.Size = New System.Drawing.Size(440, 22)
        Me.txtSubject.TabIndex = 19
        Me.txtSubject.Text = "SMTP Test Subject"
        '
        'txtFrom
        '
        Me.txtFrom.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtFrom.Location = New System.Drawing.Point(133, 40)
        Me.txtFrom.Name = "txtFrom"
        Me.txtFrom.Size = New System.Drawing.Size(256, 22)
        Me.txtFrom.TabIndex = 18
        Me.txtFrom.Text = "name@yourserver.com"
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label1.ImageAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.Label1.Location = New System.Drawing.Point(31, 184)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(92, 16)
        Me.Label1.TabIndex = 28
        Me.Label1.Text = "Feedback"
        '
        'txtResponse
        '
        Me.txtResponse.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtResponse.Location = New System.Drawing.Point(133, 176)
        Me.txtResponse.Multiline = True
        Me.txtResponse.Name = "txtResponse"
        Me.txtResponse.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtResponse.Size = New System.Drawing.Size(440, 144)
        Me.txtResponse.TabIndex = 27
        Me.txtResponse.Text = ""
        '
        'txtServer
        '
        Me.txtServer.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtServer.Location = New System.Drawing.Point(133, 8)
        Me.txtServer.Name = "txtServer"
        Me.txtServer.Size = New System.Drawing.Size(123, 22)
        Me.txtServer.TabIndex = 29
        Me.txtServer.Text = "your.server"
        '
        'lblServer
        '
        Me.lblServer.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblServer.ImageAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.lblServer.Location = New System.Drawing.Point(51, 12)
        Me.lblServer.Name = "lblServer"
        Me.lblServer.Size = New System.Drawing.Size(62, 16)
        Me.lblServer.TabIndex = 30
        Me.lblServer.Text = "Server"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(614, 333)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblServer, Me.txtServer, Me.Label1, Me.txtResponse, Me.btnSend, Me.lblContent, Me.lblSubject, Me.lblTo, Me.lblFrom, Me.txtTo, Me.txtContent, Me.txtSubject, Me.txtFrom})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

    Dim strResponse As String
    Dim myTcpClient As New TcpClient()
    Dim myNetworkStream As NetworkStream
    Dim myThread As Thread

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        myThread = New Thread(AddressOf SMTPMailProcess)
        myThread.Start()
    End Sub

    Private Sub SMTPMailProcess()

        Dim strFrom As String = txtFrom.Text
        Dim strTo As String = txtTo.Text
        Dim strSubject As String = txtSubject.Text
        Dim strContent As String = txtContent.Text
        Dim strCommand As String

        Dim dteSendData() As Byte
        Dim lngSize As Long
        Try
            myTcpClient.Connect(txtServer.Text, 25)
            myNetworkStream = myTcpClient.GetStream
            lngSize = myTcpClient.ReceiveBufferSize
            ReDim dteSendData(lngSize)
            myNetworkStream.Read(dteSendData, 0, lngSize)
            strResponse += Encoding.UTF8.GetString(dteSendData) + vbCrLf
            txtResponse.Text = strResponse

            strCommand = "HELO " + strFrom
            SMTPCommandSend(strCommand)
            strCommand = "MAIL FROM: <" + strFrom + ">"
            SMTPCommandSend(strCommand)
            strCommand = "RCPT TO: <" + strTo + ">"
            SMTPCommandSend(strCommand)
            strCommand = "DATA "
            SMTPCommandSend(strCommand)
            strCommand = "DATE: " + Date.Now.ToString + vbCrLf + _
                         "FROM: " + strFrom + vbCrLf + _
                         "TO: " + strTo + vbCrLf + _
                         "SUBJECT: " + strSubject + vbCrLf + vbCrLf + _
                         strContent + vbCrLf + "."
            SMTPCommandSend(strCommand)
            strCommand = "QUIT "
            SMTPCommandSend(strCommand)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub SMTPCommandSend(ByVal strSend As String)

        Dim bteSend() As Byte
        Dim bteResponse() As Byte
        Try
            txtResponse.Text += strSend + vbCrLf
            bteSend = Encoding.ASCII.GetBytes(strSend + vbCrLf)
            myNetworkStream.Write(bteSend, 0, bteSend.Length)
            ReDim bteResponse(myTcpClient.ReceiveBufferSize)
            myNetworkStream.Read(bteResponse, 0, bteResponse.Length)
            txtResponse.Text += Encoding.UTF8.GetString(bteResponse) + vbCrLf
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class








22.23.Smtp Email
22.23.1.Send Email by SmtpMail
22.23.2.Socket based SMTP clientSocket based SMTP client
22.23.3.SMTP based on TcpClientSMTP based on TcpClient
22.23.4.Pop3 Email ClientPop3 Email Client