'Visual Basic.Net JingCai Programming 100 Examples
'Author: Yong Zhang
'Publisher: Water Publisher China
'ISBN: 750841156
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
'Imports System
'Imports System.Threading
Imports System.Windows.Forms
public class EmailClientForm
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()
sHost = SetupForm.txtHost.Text
sPort = SetupForm.txtPort.Text
sUser = SetupForm.txtUser.Text
sAddress = SetupForm.txtAddress.Text
sName = SetupForm.txtName.Text
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 MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
Friend WithEvents ToolBar1 As System.Windows.Forms.ToolBar
Friend WithEvents ToolBarButton1 As System.Windows.Forms.ToolBarButton
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents Panel2 As System.Windows.Forms.Panel
Friend WithEvents txtMessage As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txtTo As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents lstLog As System.Windows.Forms.ListBox
Friend WithEvents txtSubject As System.Windows.Forms.TextBox
Friend WithEvents mnuProfile As System.Windows.Forms.MenuItem
Friend WithEvents mnuExit As System.Windows.Forms.MenuItem
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.MainMenu1 = New System.Windows.Forms.MainMenu()
Me.MenuItem1 = New System.Windows.Forms.MenuItem()
Me.mnuProfile = New System.Windows.Forms.MenuItem()
Me.MenuItem3 = New System.Windows.Forms.MenuItem()
Me.mnuExit = New System.Windows.Forms.MenuItem()
Me.ToolBar1 = New System.Windows.Forms.ToolBar()
Me.ToolBarButton1 = New System.Windows.Forms.ToolBarButton()
Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
Me.TabControl1 = New System.Windows.Forms.TabControl()
Me.TabPage1 = New System.Windows.Forms.TabPage()
Me.Panel2 = New System.Windows.Forms.Panel()
Me.txtMessage = New System.Windows.Forms.TextBox()
Me.Panel1 = New System.Windows.Forms.Panel()
Me.txtSubject = New System.Windows.Forms.TextBox()
Me.Label2 = New System.Windows.Forms.Label()
Me.txtTo = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.TabPage2 = New System.Windows.Forms.TabPage()
Me.lstLog = New System.Windows.Forms.ListBox()
Me.TabControl1.SuspendLayout()
Me.TabPage1.SuspendLayout()
Me.Panel2.SuspendLayout()
Me.Panel1.SuspendLayout()
Me.TabPage2.SuspendLayout()
Me.SuspendLayout()
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
'
'MenuItem1
'
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuProfile, Me.MenuItem3, Me.mnuExit})
Me.MenuItem1.Text = "&File"
'
'mnuProfile
'
Me.mnuProfile.Index = 0
Me.mnuProfile.Text = "&Profile"
'
'MenuItem3
'
Me.MenuItem3.Index = 1
Me.MenuItem3.Text = "-"
'
'mnuExit
'
Me.mnuExit.Index = 2
Me.mnuExit.Text = "E&xit"
'
'ToolBar1
'
Me.ToolBar1.AllowDrop = True
Me.ToolBar1.Buttons.AddRange(New System.Windows.Forms.ToolBarButton() {Me.ToolBarButton1})
Me.ToolBar1.ButtonSize = New System.Drawing.Size(35, 35)
Me.ToolBar1.DropDownArrows = True
Me.ToolBar1.Name = "ToolBar1"
Me.ToolBar1.ShowToolTips = True
Me.ToolBar1.Size = New System.Drawing.Size(310, 38)
Me.ToolBar1.TabIndex = 0
'
'ToolBarButton1
'
Me.ToolBarButton1.ImageIndex = 0
Me.ToolBarButton1.Text = "Send"
'
'TabControl1
'
Me.TabControl1.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabPage1, Me.TabPage2})
Me.TabControl1.Dock = System.Windows.Forms.DockStyle.Fill
Me.TabControl1.Location = New System.Drawing.Point(0, 38)
Me.TabControl1.Name = "TabControl1"
Me.TabControl1.SelectedIndex = 0
Me.TabControl1.Size = New System.Drawing.Size(310, 321)
Me.TabControl1.TabIndex = 1
'
'TabPage1
'
Me.TabPage1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel2, Me.Panel1})
Me.TabPage1.Location = New System.Drawing.Point(4, 21)
Me.TabPage1.Name = "TabPage1"
Me.TabPage1.Size = New System.Drawing.Size(302, 296)
Me.TabPage1.TabIndex = 0
Me.TabPage1.Text = "SMTP"
'
'Panel2
'
Me.Panel2.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtMessage})
Me.Panel2.Dock = System.Windows.Forms.DockStyle.Fill
Me.Panel2.DockPadding.All = 2
Me.Panel2.Location = New System.Drawing.Point(0, 55)
Me.Panel2.Name = "Panel2"
Me.Panel2.Size = New System.Drawing.Size(302, 241)
Me.Panel2.TabIndex = 1
'
'txtMessage
'
Me.txtMessage.Dock = System.Windows.Forms.DockStyle.Fill
Me.txtMessage.Location = New System.Drawing.Point(2, 2)
Me.txtMessage.Multiline = True
Me.txtMessage.Name = "txtMessage"
Me.txtMessage.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtMessage.Size = New System.Drawing.Size(298, 237)
Me.txtMessage.TabIndex = 2
Me.txtMessage.Text = ""
'
'Panel1
'
Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtSubject, Me.Label2, Me.txtTo, Me.Label1})
Me.Panel1.Dock = System.Windows.Forms.DockStyle.Top
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(302, 55)
Me.Panel1.TabIndex = 0
'
'txtSubject
'
Me.txtSubject.Location = New System.Drawing.Point(60, 30)
Me.txtSubject.Name = "txtSubject"
Me.txtSubject.Size = New System.Drawing.Size(236, 22)
Me.txtSubject.TabIndex = 1
Me.txtSubject.Text = ""
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(10, 35)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(45, 15)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Subject:"
'
'txtTo
'
Me.txtTo.Location = New System.Drawing.Point(60, 5)
Me.txtTo.Name = "txtTo"
Me.txtTo.Size = New System.Drawing.Size(236, 22)
Me.txtTo.TabIndex = 0
Me.txtTo.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(10, 10)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(45, 15)
Me.Label1.TabIndex = 0
Me.Label1.Text = "To:"
'
'TabPage2
'
Me.TabPage2.Controls.AddRange(New System.Windows.Forms.Control() {Me.lstLog})
Me.TabPage2.Location = New System.Drawing.Point(4, 21)
Me.TabPage2.Name = "TabPage2"
Me.TabPage2.Size = New System.Drawing.Size(302, 296)
Me.TabPage2.TabIndex = 1
Me.TabPage2.Text = "Transaction"
'
'lstLog
'
Me.lstLog.Dock = System.Windows.Forms.DockStyle.Fill
Me.lstLog.HorizontalScrollbar = True
Me.lstLog.ItemHeight = 12
Me.lstLog.Name = "lstLog"
Me.lstLog.Size = New System.Drawing.Size(302, 292)
Me.lstLog.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
Me.ClientSize = New System.Drawing.Size(310, 359)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabControl1, Me.ToolBar1})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "SMTP"
Me.TabControl1.ResumeLayout(False)
Me.TabPage1.ResumeLayout(False)
Me.Panel2.ResumeLayout(False)
Me.Panel1.ResumeLayout(False)
Me.TabPage2.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
Private SetupForm As New Form2()
Private smtpSocket As New System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
Dim IPAdd As System.Net.IPAddress
Dim IPEndAdd As System.Net.IPEndPoint
Dim IpHost As New System.Net.IPHostEntry()
Dim strHost, strPort, strUser, strName, strFrom, strTo, strSubject, strMsg As String
Dim msg As String
strHost = SetupForm.txtHost.Text
strPort = SetupForm.txtPort.Text
strFrom = SetupForm.txtAddress.Text
strTo = txtTo.Text
strSubject = txtSubject.Text
strMsg = txtMessage.Text
lstLog.Items.Clear()
Try
' Connect to SMTP server
lstLog.Items.Add("C: Trying to connect to host " & strHost & ", port: " & strPort)
IpHost = System.Net.Dns.Resolve(strHost)
IPAdd = IpHost.AddressList(0)
IPEndAdd = New System.Net.IPEndPoint(IPAdd.Address, Int32.Parse(strPort))
smtpSocket.Connect(IPEndAdd)
If Not smtpSocket.Connected Then
lstLog.Items.Add("Unable to connect to " & strHost & ":" & strPort)
End If
If Not SMTPResponse("220") Then
Return
End If
Catch ex As Exception
lstLog.Items.Add("Socket: " & ex.ToString())
End Try
Try
msg = "HELO " & strHost & ControlChars.CrLf
If Not SMTPSend(msg) Then
Return
End If
If Not SMTPResponse("250") Then
Return
End If
msg = "MAIL FROM: " & strFrom.Trim & ControlChars.CrLf
If Not SMTPSend(msg) Then
Return
End If
If Not SMTPResponse("250") Then
Return
End If
Dim i As Short
Dim tString As String
Dim strArray() As String
strArray = Split(strTo.Trim, ",")
For i = 0 To UBound(strArray)
If strArray(i).Trim().ToString <> "" Then
msg = "RCPT TO: " & strArray(i).Trim().ToString & ControlChars.CrLf
If Not SMTPSend(msg) Then
Return
End If
If Not SMTPResponse("250") Then
Return
End If
End If
Next
msg = "DATA" & ControlChars.CrLf
If Not SMTPSend(msg) Then
Return
End If
If Not SMTPResponse("354") Then
Return
End If
Dim strDate As String = DateTime.Now.ToShortDateString() & " " & DateTime.Now.ToLongTimeString()
msg = "Date: " & strDate & ControlChars.CrLf
If Not SMTPSend(msg) Then
Return
End If
msg = "From: " & strFrom.Trim() & ControlChars.CrLf
If Not SMTPSend(msg) Then
Return
End If
msg = "To: " & strTo.Trim & ControlChars.CrLf
If Not SMTPSend(msg) Then
Return
End If
msg = "Subject: " & strSubject.ToString & ControlChars.CrLf & ControlChars.CrLf
If Not SMTPSend(msg) Then
Return
End If
msg = strMsg.ToString & ControlChars.CrLf
If Not SMTPSend(msg) Then
Return
End If
msg = ControlChars.CrLf & "." & ControlChars.CrLf
If Not SMTPSend(msg) Then
Return
End If
If Not SMTPResponse("250") Then
Return
End If
msg = "QUIT" & ControlChars.CrLf
If Not SMTPSend(msg) Then
Return
End If
If Not SMTPResponse("221") Then
Return
End If
smtpSocket.Shutdown(SocketShutdown.Both)
smtpSocket.Close()
Catch ex As Exception
lstLog.Items.Add(ex.ToString())
End Try
End Sub
Private Sub mnuProfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuProfile.Click
SetupForm.ShowDialog()
End Sub
Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
End
End Sub
Private Function SMTPSend(ByVal strMsg As String) As Boolean
Dim byteMsg() As Byte
Try
lstLog.Items.Add("C: " & strMsg.ToString())
byteMsg = Encoding.ASCII.GetBytes(strMsg.ToCharArray())
smtpSocket.Send(byteMsg, byteMsg.Length, SocketFlags.None)
Return True
Catch ex As Exception
lstLog.Items.Add("SMTPSend Error: " & ex.ToString())
smtpSocket.Shutdown(SocketShutdown.Both)
smtpSocket.Close()
Return False
End Try
End Function
Private Function SMTPResponse(ByVal strEcho As String) As Boolean
Dim RecvBytes(256) As Byte
Dim strResponse As String = Nothing
Dim bytes As Int32
Try
bytes = smtpSocket.Receive(RecvBytes, RecvBytes.Length, 0)
strResponse = Encoding.ASCII.GetString(RecvBytes, 0, bytes)
lstLog.Items.Add("S: " & strResponse.ToString())
If Not strResponse.StartsWith(strEcho) Then
lstLog.Items.Add("SMTPResponse Error.")
smtpSocket.Shutdown(SocketShutdown.Both)
smtpSocket.Close()
Return False
Else
Return True
End If
Catch ex As Exception
lstLog.Items.Add("SMTPResponse Error: " & ex.ToString())
smtpSocket.Shutdown(SocketShutdown.Both)
smtpSocket.Close()
Return False
End Try
End Function
End Class
Module Module1
Public sHost As String
Public sPort As String
Public sUser As String
Public sAddress As String
Public sName As String
Public sTo As String
Public sSubject As String
Public sMsg As String
End Module
Public Class Form2
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 GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents txtHost As System.Windows.Forms.TextBox
Friend WithEvents txtPort As System.Windows.Forms.TextBox
Friend WithEvents txtUser As System.Windows.Forms.TextBox
Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
Friend WithEvents txtAddress As System.Windows.Forms.TextBox
Friend WithEvents txtName As System.Windows.Forms.TextBox
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents btnOK As System.Windows.Forms.Button
Friend WithEvents btnCancel As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.GroupBox1 = New System.Windows.Forms.GroupBox()
Me.txtUser = New System.Windows.Forms.TextBox()
Me.txtPort = New System.Windows.Forms.TextBox()
Me.txtHost = New System.Windows.Forms.TextBox()
Me.Label3 = New System.Windows.Forms.Label()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.GroupBox2 = New System.Windows.Forms.GroupBox()
Me.txtAddress = New System.Windows.Forms.TextBox()
Me.txtName = New System.Windows.Forms.TextBox()
Me.Label5 = New System.Windows.Forms.Label()
Me.Label6 = New System.Windows.Forms.Label()
Me.btnOK = New System.Windows.Forms.Button()
Me.btnCancel = New System.Windows.Forms.Button()
Me.GroupBox1.SuspendLayout()
Me.GroupBox2.SuspendLayout()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtUser, Me.txtPort, Me.txtHost, Me.Label3, Me.Label1, Me.Label2})
Me.GroupBox1.Location = New System.Drawing.Point(3, 5)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(240, 100)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "SMTP Server"
'
'txtUser
'
Me.txtUser.Location = New System.Drawing.Point(70, 70)
Me.txtUser.Name = "txtUser"
Me.txtUser.Size = New System.Drawing.Size(160, 22)
Me.txtUser.TabIndex = 2
Me.txtUser.Text = ""
'
'txtPort
'
Me.txtPort.Location = New System.Drawing.Point(70, 45)
Me.txtPort.Name = "txtPort"
Me.txtPort.Size = New System.Drawing.Size(160, 22)
Me.txtPort.TabIndex = 1
Me.txtPort.Text = "25"
'
'txtHost
'
Me.txtHost.Location = New System.Drawing.Point(70, 20)
Me.txtHost.Name = "txtHost"
Me.txtHost.Size = New System.Drawing.Size(160, 22)
Me.txtHost.TabIndex = 0
Me.txtHost.Text = ""
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(10, 75)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(70, 15)
Me.Label3.TabIndex = 2
Me.Label3.Text = "User Name:"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(10, 25)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(70, 15)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Host:"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(10, 50)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(70, 15)
Me.Label2.TabIndex = 1
Me.Label2.Text = "Port:"
'
'GroupBox2
'
Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtAddress, Me.txtName, Me.Label5, Me.Label6})
Me.GroupBox2.Location = New System.Drawing.Point(3, 110)
Me.GroupBox2.Name = "GroupBox2"
Me.GroupBox2.Size = New System.Drawing.Size(240, 75)
Me.GroupBox2.TabIndex = 1
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "User Information"
'
'txtAddress
'
Me.txtAddress.Location = New System.Drawing.Point(70, 45)
Me.txtAddress.Name = "txtAddress"
Me.txtAddress.Size = New System.Drawing.Size(160, 22)
Me.txtAddress.TabIndex = 4
Me.txtAddress.Text = ""
'
'txtName
'
Me.txtName.Location = New System.Drawing.Point(70, 20)
Me.txtName.Name = "txtName"
Me.txtName.Size = New System.Drawing.Size(160, 22)
Me.txtName.TabIndex = 3
Me.txtName.Text = ""
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(10, 25)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(70, 15)
Me.Label5.TabIndex = 0
Me.Label5.Text = "Name:"
'
'Label6
'
Me.Label6.Location = New System.Drawing.Point(10, 50)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(70, 15)
Me.Label6.TabIndex = 1
Me.Label6.Text = "Address:"
'
'btnOK
'
Me.btnOK.Location = New System.Drawing.Point(28, 195)
Me.btnOK.Name = "btnOK"
Me.btnOK.Size = New System.Drawing.Size(85, 25)
Me.btnOK.TabIndex = 2
Me.btnOK.Text = "OK"
'
'btnCancel
'
Me.btnCancel.Location = New System.Drawing.Point(133, 195)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.Size = New System.Drawing.Size(85, 25)
Me.btnCancel.TabIndex = 3
Me.btnCancel.Text = "Cancel"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
Me.ClientSize = New System.Drawing.Size(247, 228)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnCancel, Me.btnOK, Me.GroupBox2, Me.GroupBox1})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "Form2"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
Me.Text = "SMTP Profile"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox2.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtHost.Text = sHost
txtPort.Text = sPort
txtUser.Text = sUser
txtAddress.Text = sAddress
txtName.Text = sName
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
If txtHost.Text = "" Or txtPort.Text = "" Or txtUser.Text = "" Or txtAddress.Text = "" Or txtName.Text = "" Then
MessageBox.Show("Please enter the information.", "SMTP", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
Else
sHost = txtHost.Text
sPort = txtPort.Text
sUser = txtUser.Text
sAddress = txtAddress.Text
sName = txtName.Text
Me.Close()
End If
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
End Class