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

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Socket Network » Smtp Email 
22.23.3.SMTP based on TcpClient
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 NothingThen
                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(43056)
        Me.btnSend.Name = "btnSend"
        Me.btnSend.Size = New System.Drawing.Size(13324)
        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(51125)
        Me.lblContent.Name = "lblContent"
        Me.lblContent.Size = New System.Drawing.Size(6224)
        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(51100)
        Me.lblSubject.Name = "lblSubject"
        Me.lblSubject.Size = New System.Drawing.Size(6212)
        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(5169)
        Me.lblTo.Name = "lblTo"
        Me.lblTo.Size = New System.Drawing.Size(6216)
        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(5145)
        Me.lblFrom.Name = "lblFrom"
        Me.lblFrom.Size = New System.Drawing.Size(6216)
        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(13364)
        Me.txtTo.Name = "txtTo"
        Me.txtTo.Size = New System.Drawing.Size(25622)
        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(133120)
        Me.txtContent.Multiline = True
        Me.txtContent.Name = "txtContent"
        Me.txtContent.Size = New System.Drawing.Size(44048)
        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(13396)
        Me.txtSubject.Name = "txtSubject"
        Me.txtSubject.Size = New System.Drawing.Size(44022)
        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(13340)
        Me.txtFrom.Name = "txtFrom"
        Me.txtFrom.Size = New System.Drawing.Size(25622)
        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(31184)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(9216)
        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(133176)
        Me.txtResponse.Multiline = True
        Me.txtResponse.Name = "txtResponse"
        Me.txtResponse.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtResponse.Size = New System.Drawing.Size(440144)
        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(1338)
        Me.txtServer.Name = "txtServer"
        Me.txtServer.Size = New System.Drawing.Size(12322)
        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(5112)
        Me.lblServer.Name = "lblServer"
        Me.lblServer.Size = New System.Drawing.Size(6216)
        Me.lblServer.TabIndex = 30
        Me.lblServer.Text = "Server"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(614)
        Me.ClientSize = New System.Drawing.Size(614333)
        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.EventArgsHandles 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.