VB.Net Tutorial/Socket Network/Smtp Email
Содержание
Pop3 Email Client
<source lang="vbnet">" Quote from Book " Visual Basic.net Programming " Publisher Water Publisher " China, Beijing
Imports System.Net Imports System.Net.Sockets Imports System.Text Imports System.IO Imports System.Threading Imports System.Windows.Forms public class Pop3Receive
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.ruponentModel.IContainer Friend WithEvents txtMailContent As System.Windows.Forms.TextBox Friend WithEvents btnMailReceive As System.Windows.Forms.Button 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 Label4 As System.Windows.Forms.Label Friend WithEvents txtID As System.Windows.Forms.TextBox Friend WithEvents txtPassWord As System.Windows.Forms.TextBox Friend WithEvents txtServer As System.Windows.Forms.TextBox Friend WithEvents txtPort As System.Windows.Forms.TextBox Friend WithEvents lstMailList As System.Windows.Forms.ListBox Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents txtNumber As System.Windows.Forms.TextBox Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents txtSize As System.Windows.Forms.TextBox Friend WithEvents btnDelete As System.Windows.Forms.Button Friend WithEvents btnQUIT As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.lstMailList = New System.Windows.Forms.ListBox() Me.txtMailContent = New System.Windows.Forms.TextBox() Me.txtID = New System.Windows.Forms.TextBox() Me.txtServer = New System.Windows.Forms.TextBox() Me.txtPort = New System.Windows.Forms.TextBox() Me.txtPassWord = New System.Windows.Forms.TextBox() Me.btnMailReceive = New System.Windows.Forms.Button() Me.Label1 = New System.Windows.Forms.Label() Me.Label2 = New System.Windows.Forms.Label() Me.Label3 = New System.Windows.Forms.Label() Me.Label4 = New System.Windows.Forms.Label() Me.Label5 = New System.Windows.Forms.Label() Me.txtNumber = New System.Windows.Forms.TextBox() Me.txtSize = New System.Windows.Forms.TextBox() Me.Label6 = New System.Windows.Forms.Label() Me.btnDelete = New System.Windows.Forms.Button() Me.btnQUIT = New System.Windows.Forms.Button() Me.SuspendLayout() " "lstMailList " Me.lstMailList.ItemHeight = 12 Me.lstMailList.Location = New System.Drawing.Point(20, 48) Me.lstMailList.Name = "lstMailList" Me.lstMailList.Size = New System.Drawing.Size(666, 112) Me.lstMailList.TabIndex = 8 " "txtMailContent " Me.txtMailContent.Location = New System.Drawing.Point(20, 176) Me.txtMailContent.Multiline = True Me.txtMailContent.Name = "txtMailContent" Me.txtMailContent.ScrollBars = System.Windows.Forms.ScrollBars.Both Me.txtMailContent.Size = New System.Drawing.Size(666, 344) Me.txtMailContent.TabIndex = 9 Me.txtMailContent.Text = "" " "txtID " Me.txtID.Location = New System.Drawing.Point(727, 40) Me.txtID.Name = "txtID" Me.txtID.Size = New System.Drawing.Size(164, 21) Me.txtID.TabIndex = 0 Me.txtID.Text = "" " "txtServer " Me.txtServer.Location = New System.Drawing.Point(727, 200) Me.txtServer.Name = "txtServer" Me.txtServer.Size = New System.Drawing.Size(164, 21) Me.txtServer.TabIndex = 3 Me.txtServer.Text = "" " "txtPort " Me.txtPort.Location = New System.Drawing.Point(727, 144) Me.txtPort.Name = "txtPort" Me.txtPort.Size = New System.Drawing.Size(164, 21) Me.txtPort.TabIndex = 2 Me.txtPort.Text = "110" " "txtPassWord " Me.txtPassWord.Location = New System.Drawing.Point(727, 93) Me.txtPassWord.Name = "txtPassWord" Me.txtPassWord.PasswordChar = Microsoft.VisualBasic.ChrW(42) Me.txtPassWord.Size = New System.Drawing.Size(164, 21) Me.txtPassWord.TabIndex = 1 Me.txtPassWord.Text = "" " "btnMailReceive " Me.btnMailReceive.Location = New System.Drawing.Point(727, 240) Me.btnMailReceive.Name = "btnMailReceive" Me.btnMailReceive.Size = New System.Drawing.Size(164, 24) Me.btnMailReceive.TabIndex = 6 Me.btnMailReceive.Text = "Check Mail" " "Label1 " Me.Label1.Location = New System.Drawing.Point(727, 16) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(143, 16) Me.Label1.TabIndex = 7 Me.Label1.Text = "Account" " "Label2 " Me.Label2.Location = New System.Drawing.Point(727, 176) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(143, 16) Me.Label2.TabIndex = 8 Me.Label2.Text = "Server" " "Label3 " Me.Label3.Location = New System.Drawing.Point(727, 128) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(143, 16) Me.Label3.TabIndex = 9 Me.Label3.Text = "port" " "Label4 " Me.Label4.Location = New System.Drawing.Point(727, 72) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(143, 16) Me.Label4.TabIndex = 10 Me.Label4.Text = "Password" " "Label5 " Me.Label5.Location = New System.Drawing.Point(730, 275) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(153, 13) Me.Label5.TabIndex = 11 Me.Label5.Text = "Total" " "txtNumber " Me.txtNumber.Location = New System.Drawing.Point(730, 299) Me.txtNumber.Name = "txtNumber" Me.txtNumber.Size = New System.Drawing.Size(153, 21) Me.txtNumber.TabIndex = 4 Me.txtNumber.Text = "" " "txtSize " Me.txtSize.Location = New System.Drawing.Point(731, 355) Me.txtSize.Name = "txtSize" Me.txtSize.Size = New System.Drawing.Size(153, 21) Me.txtSize.TabIndex = 5 Me.txtSize.Text = "" " "Label6 " Me.Label6.Location = New System.Drawing.Point(731, 331) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(153, 13) Me.Label6.TabIndex = 13 Me.Label6.Text = "Size(k)" " "btnDelete " Me.btnDelete.Location = New System.Drawing.Point(492, 8) Me.btnDelete.Name = "btnDelete" Me.btnDelete.Size = New System.Drawing.Size(194, 24) Me.btnDelete.TabIndex = 7 Me.btnDelete.Text = "Delete" " "btnQUIT " Me.btnQUIT.Location = New System.Drawing.Point(732, 395) Me.btnQUIT.Name = "btnQUIT" Me.btnQUIT.Size = New System.Drawing.Size(164, 24) Me.btnQUIT.TabIndex = 6 Me.btnQUIT.Text = "Quit" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) Me.ClientSize = New System.Drawing.Size(911, 533) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnQUIT, Me.btnDelete, Me.txtSize, Me.Label6, Me.txtNumber, Me.Label5, Me.Label4, Me.Label3, Me.Label2, Me.Label1, Me.btnMailReceive, Me.txtPassWord, Me.txtPort, Me.txtServer, Me.txtID, Me.txtMailContent, Me.lstMailList}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub Dim myTcpClient As New TcpClient() Dim myNetworkStream As NetworkStream Dim blnDelete As Boolean = False Private Sub btnMailReceive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMailReceive.Click Dim strMessage ConnectToMailServer() Dim t As New Thread(AddressOf GetMailNumber) t.Start() End Sub Public Sub ConnectToMailServer() Try Dim strReturnMessage As String myTcpClient.Connect(txtServer.Text, Integer.Parse(txtPort.Text)) myNetworkStream = myTcpClient.GetStream() Dim myStreamReader As New StreamReader(myNetworkStream) strReturnMessage = myStreamReader.ReadLine() + vbCrLf strReturnMessage += SendCommand(myNetworkStream, "USER " + txtID.Text) + vbCrLf strReturnMessage += SendCommand(myNetworkStream, "PASS " + txtPassWord.Text) + vbCrLf MessageBox.Show(strReturnMessage) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Sub GetMailNumber() Dim strMessage(2) As String Dim strMessageLine As String strMessageLine = SendCommand(myNetworkStream, "STAT") strMessage = strMessageLine.Split(" ") txtNumber.Text = strMessage(1) txtSize.Text = (Long.Parse(strMessage(2)) / 1000).ToString GetMailList(Long.Parse(strMessage(1))) End Sub Private Sub GetMailList(ByVal lngNumber As Long) Dim strReturnMessage As String Dim lngLoop As Long Dim lngIndex As Long Try For lngLoop = 0 To lngNumber - 1 lngIndex = lngLoop + 1 strReturnMessage = "[" + lngIndex.ToString + "]" + SendCommand(myNetworkStream, "LIST " + lngIndex.ToString) lstMailList.Items.Add(strReturnMessage) Next Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Function SendCommand(ByRef pNetStream As NetworkStream, ByVal pstrCommand As String) As String Try Dim strCommand = pstrCommand + vbCrLf Dim bteCommand() As Byte = Encoding.ASCII.GetBytes(strCommand) Dim myStreamReader As StreamReader Dim sstrLine As String Dim strLine As String pNetStream.Write(bteCommand, 0, bteCommand.Length) myStreamReader = New StreamReader(pNetStream) strLine = myStreamReader.ReadLine() Return strLine Catch ex As Exception Return ex.Message End Try End Function Private Sub lstMailList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstMailList.SelectedIndexChanged If blnDelete = False Then Dim t As New Thread(AddressOf GetMessage) t.Start() End If End Sub Private Sub GetMessage() Dim strMessageContent As String = "" Dim intRead As Integer Dim strCommand = "RETR " + (lstMailList.SelectedIndex + 1).ToString + vbCrLf Dim bteCommand() As Byte = Encoding.ASCII.GetBytes(strCommand) Dim myStreamReader As StreamReader
txtMailContent.Text = "" Try myNetworkStream.Write(bteCommand, 0, bteCommand.Length) myStreamReader = New StreamReader(myNetworkStream) AnalyMessage(myStreamReader) Catch ex As Exception MessageBox.Show(ex.Message) End Try
End Sub Private Sub AnalyMessage(ByVal pStreamReader As StreamReader) Dim strReadLine As String = "" Dim sstrReadLine As String = "" Do While (sstrReadLine = Nothing Or sstrReadLine.Length = 0) sstrReadLine = pStreamReader.ReadLine Loop txtMailContent.Text += vbCrLf + "--- Header --" + vbCrLf Do While sstrReadLine.Trim.Length <> 0 sstrReadLine = pStreamReader.ReadLine txtMailContent.Text += vbCrLf + sstrReadLine Loop txtMailContent.Text += vbCrLf + "--- Body --" + vbCrLf Do sstrReadLine = pStreamReader.ReadLine If sstrReadLine.Trim() = "." Then Exit Do If sstrReadLine <> Nothing Then txtMailContent.Text += vbCrLf + sstrReadLine Loop End Sub Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click Dim strReturnMessage As String Dim intDelete As Integer intDelete = lstMailList.SelectedIndex + 1 Try strReturnMessage = SendCommand(myNetworkStream, "DELE " + intDelete.ToString) txtMailContent.Text = strReturnMessage Catch ex As Exception MessageBox.Show(ex.Message) End Try blnDelete = True lstMailList.Items.RemoveAt(intDelete - 1) blnDelete = False End Sub Private Sub btnQUIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQUIT.Click Dim strReturnMessage As String Try strReturnMessage = SendCommand(myNetworkStream, "QUIT") Catch ex As Exception MessageBox.Show(ex.Message) End Try
End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub
End Class</source>
Send Email by SmtpMail
<source lang="vbnet">Imports System.Web.Mail Imports System.Web.Mail.MailMessage Imports System.Web.Mail.MailAttachment Imports System.Web.Mail.SmtpMail Imports System.IO Imports System.Text Imports System.Text.Encoding Imports System.Threading
Public Class Tester
Public Shared Sub Main Dim mailmsg As System.Web.Mail.MailMessage Dim mailAttach As System.Web.Mail.MailAttachment Dim i As Integer Try SmtpMail.SmtpServer = "server" mailmsg = New MailMessage() mailmsg.From = "from@from.net" mailmsg.To = "to@to.net" mailmsg.Cc = "cc@cc.net" mailmsg.Bcc = "bcc@bcc.net" mailmsg.Subject = "sub" mailmsg.BodyEncoding = System.Text.Encoding.UTF8 mailmsg.Body = "body" mailmsg.BodyFormat = MailFormat.Html "mailmsg.BodyFormat = MailFormat.Text mailAttach = New MailAttachment("c:test.txt") mailmsg.Attachments.Add(mailAttach) mailmsg.Priority = MailPriority.Normal mailmsg.Priority = MailPriority.Low mailmsg.Priority = MailPriority.High Dim CurThread As Thread CurThread = System.Threading.Thread.CurrentThread SyncLock CurThread SmtpMail.Send(mailmsg) End SyncLock mailmsg = Nothing Console.WriteLine("Send Mail Successfully.") Catch ex As Exception Console.WriteLine("Send Mail Error: " & ex.ToString) End Try End Sub
End Class</source>
SMTP based on TcpClient
<source lang="vbnet">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.ruponentModel.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.ru.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.ru" " "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</source>
Socket based SMTP client
<source lang="vbnet">"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.ruponentModel.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 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.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.ruponents) 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.ruponentModel.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 <System.Diagnostics.DebuggerStepThrough()> 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</source>