利用OpenPOP开源组件实现接收电子邮件功能

网上关于接收电子邮件的代码很多,试用后,能满足要求的甚少。OpenPop(http://sourceforge.net/projects/hpop/)现在有了新的版本,且能经过VS2010编译,值得使用。可惜的是我现在对C#不熟悉,其demo经研究后转成VB.net实现。


OpenPOP的test截图:




我自己做的实例:



以下是代码部分:


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
_
Partial Class FetchEmail
Inherits
System.Windows.Forms.Form


'Form 重写 Dispose,以清理组件列表。

<System.Diagnostics.DebuggerNonUserCode()> _

Protected Overrides Sub Dispose(ByVal disposing As
Boolean)

Try
If
disposing AndAlso components IsNot Nothing
Then

components.Dispose()

End If

Finally

MyBase.Dispose(disposing)
End
Try
End Sub


'Windows 窗体设计器所必需的
Private
components As System.ComponentModel.IContainer


'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用
Windows 窗体设计器修改它。
'不要使用代码编辑器修改它。

<System.Diagnostics.DebuggerStepThrough()> _
Private
Sub InitializeComponent()

Me.Label1 = New
System.Windows.Forms.Label()

Me.txtPOP3Server = New
System.Windows.Forms.TextBox()

Me.btnFetchEmail = New
System.Windows.Forms.Button()

Me.txtPort = New
System.Windows.Forms.TextBox()

Me.Label2 = New
System.Windows.Forms.Label()

Me.txtName = New
System.Windows.Forms.TextBox()

Me.Label3 = New
System.Windows.Forms.Label()

Me.txtPassword = New
System.Windows.Forms.TextBox()

Me.Label4 = New
System.Windows.Forms.Label()

Me.txtEmailCount = New
System.Windows.Forms.TextBox()

Me.Label5 = New
System.Windows.Forms.Label()

Me.txtRead = New
System.Windows.Forms.TextBox()

Me.Label6 = New
System.Windows.Forms.Label()

Me.txtBody = New
System.Windows.Forms.TextBox()

Me.txtFrom = New
System.Windows.Forms.TextBox()

Me.Label7 = New
System.Windows.Forms.Label()

Me.Label8 = New
System.Windows.Forms.Label()

Me.Label9 = New
System.Windows.Forms.Label()

Me.txtSubject = New
System.Windows.Forms.TextBox()

Me.Label10 = New
System.Windows.Forms.Label()

Me.lstAttachments = New
System.Windows.Forms.ListBox()

Me.btnConnectLogin = New
System.Windows.Forms.Button()

Me.TableLayoutPanel1 = New
System.Windows.Forms.TableLayoutPanel()

Me.TableLayoutPanel1.SuspendLayout()

Me.SuspendLayout()

'

'Label1

'
Me.Label1.AutoSize =
True
Me.Label1.Dock =
System.Windows.Forms.DockStyle.Right

Me.Label1.Location = New System.Drawing.Point(13,
10)
Me.Label1.Name =
"Label1"
Me.Label1.Size = New
System.Drawing.Size(99, 35)

Me.Label1.TabIndex = 0

Me.Label1.Text = "POP3服务器:"

'

'txtPOP3Server

'
Me.txtPOP3Server.Dock =
System.Windows.Forms.DockStyle.Fill

Me.txtPOP3Server.Location = New System.Drawing.Point(118,
13)
Me.txtPOP3Server.Name =
"txtPOP3Server"

Me.txtPOP3Server.Size = New System.Drawing.Size(236,
25)
Me.txtPOP3Server.TabIndex =
1
Me.txtPOP3Server.Text =
"pop.126.com"

'

'btnFetchEmail

'
Me.btnFetchEmail.Enabled =
False
Me.btnFetchEmail.Location =
New System.Drawing.Point(708, 48)

Me.btnFetchEmail.Name =
"btnFetchEmail"

Me.btnFetchEmail.Size = New System.Drawing.Size(162,
27)
Me.btnFetchEmail.TabIndex =
2
Me.btnFetchEmail.Text =
"读取邮件"

Me.btnFetchEmail.UseVisualStyleBackColor =
True

'

'txtPort

'
Me.txtPort.Dock =
System.Windows.Forms.DockStyle.Fill

Me.txtPort.Location = New System.Drawing.Point(534,
13)
Me.txtPort.Name =
"txtPort"
Me.txtPort.Size = New
System.Drawing.Size(168, 25)

Me.txtPort.TabIndex = 4

Me.txtPort.Text = "110"

'

'Label2

'
Me.Label2.AutoSize =
True
Me.Label2.Dock =
System.Windows.Forms.DockStyle.Right

Me.Label2.Location = New System.Drawing.Point(431,
10)
Me.Label2.Name =
"Label2"
Me.Label2.Size = New
System.Drawing.Size(97, 35)

Me.Label2.TabIndex = 3

Me.Label2.Text = "服务器端口:"

'

'txtName

'
Me.txtName.Dock =
System.Windows.Forms.DockStyle.Fill

Me.txtName.Location = New System.Drawing.Point(118,
48)
Me.txtName.Name =
"txtName"
Me.txtName.Size = New
System.Drawing.Size(236, 25)

Me.txtName.TabIndex = 6

Me.txtName.Text = "youremail"

'

'Label3

'
Me.Label3.AutoSize =
True
Me.Label3.Dock =
System.Windows.Forms.DockStyle.Right

Me.Label3.Location = New System.Drawing.Point(45,
45)
Me.Label3.Name =
"Label3"
Me.Label3.Size = New
System.Drawing.Size(67, 33)

Me.Label3.TabIndex = 5

Me.Label3.Text = "用户名:"

'

'txtPassword

'
Me.txtPassword.Dock =
System.Windows.Forms.DockStyle.Fill

Me.txtPassword.Location = New System.Drawing.Point(534,
48)
Me.txtPassword.Name =
"txtPassword"

Me.txtPassword.PasswordChar =
Global.Microsoft.VisualBasic.ChrW(9679)

Me.txtPassword.Size = New System.Drawing.Size(168,
25)
Me.txtPassword.TabIndex =
8
Me.txtPassword.Text =
"YourPassword"

'

'Label4

'
Me.Label4.AutoSize =
True
Me.Label4.Dock =
System.Windows.Forms.DockStyle.Right

Me.Label4.Location = New System.Drawing.Point(476,
45)
Me.Label4.Name =
"Label4"
Me.Label4.Size = New
System.Drawing.Size(52, 33)

Me.Label4.TabIndex = 7

Me.Label4.Text = "密码:"

'

'txtEmailCount

'
Me.txtEmailCount.Dock =
System.Windows.Forms.DockStyle.Fill

Me.txtEmailCount.ForeColor =
System.Drawing.Color.Red

Me.txtEmailCount.Location = New System.Drawing.Point(118,
81)
Me.txtEmailCount.Name =
"txtEmailCount"

Me.txtEmailCount.ReadOnly = True

Me.txtEmailCount.Size = New System.Drawing.Size(236,
25)
Me.txtEmailCount.TabIndex =
10
Me.txtEmailCount.Text =
"0"

'

'Label5

'
Me.Label5.AutoSize =
True
Me.Label5.Dock =
System.Windows.Forms.DockStyle.Right

Me.Label5.Location = New System.Drawing.Point(30,
78)
Me.Label5.Name =
"Label5"
Me.Label5.Size = New
System.Drawing.Size(82, 39)

Me.Label5.TabIndex = 9

Me.Label5.Text = "邮件总数:"

'

'txtRead

'
Me.txtRead.Dock =
System.Windows.Forms.DockStyle.Fill

Me.txtRead.Location = New System.Drawing.Point(534,
81)
Me.txtRead.Name =
"txtRead"
Me.txtRead.ReadOnly =
True
Me.txtRead.Size = New
System.Drawing.Size(168, 25)

Me.txtRead.TabIndex = 12

Me.txtRead.Text = "0"

'

'Label6

'
Me.Label6.AutoSize =
True
Me.Label6.Dock =
System.Windows.Forms.DockStyle.Right

Me.Label6.Location = New System.Drawing.Point(401,
78)
Me.Label6.Name =
"Label6"
Me.Label6.Size = New
System.Drawing.Size(127, 39)

Me.Label6.TabIndex = 11

Me.Label6.Text = "想读取的邮件号:"

'

'txtBody

'

Me.TableLayoutPanel1.SetColumnSpan(Me.txtBody,
3)
Me.txtBody.Dock =
System.Windows.Forms.DockStyle.Fill

Me.txtBody.Location = New System.Drawing.Point(118,
189)
Me.txtBody.Multiline =
True
Me.txtBody.Name =
"txtBody"
Me.txtBody.Size = New
System.Drawing.Size(584, 309)

Me.txtBody.TabIndex = 13

'

'txtFrom

'

Me.TableLayoutPanel1.SetColumnSpan(Me.txtFrom,
3)
Me.txtFrom.Dock =
System.Windows.Forms.DockStyle.Fill

Me.txtFrom.Location = New System.Drawing.Point(118,
120)
Me.txtFrom.Multiline =
True
Me.txtFrom.Name =
"txtFrom"
Me.txtFrom.Size = New
System.Drawing.Size(584, 27)

Me.txtFrom.TabIndex = 14

'

'Label7

'
Me.Label7.AutoSize =
True
Me.Label7.Dock =
System.Windows.Forms.DockStyle.Right

Me.Label7.Location = New System.Drawing.Point(45,
117)
Me.Label7.Name =
"Label7"
Me.Label7.Size = New
System.Drawing.Size(67, 33)

Me.Label7.TabIndex = 16

Me.Label7.Text = "发件人:"

'

'Label8

'
Me.Label8.AutoSize =
True
Me.Label8.Dock =
System.Windows.Forms.DockStyle.Right

Me.Label8.Location = New System.Drawing.Point(30,
186)
Me.Label8.Name =
"Label8"
Me.Label8.Size = New
System.Drawing.Size(82, 315)

Me.Label8.TabIndex = 17

Me.Label8.Text = "邮件内容:"

'

'Label9

'
Me.Label9.AutoSize =
True
Me.Label9.Location = New
System.Drawing.Point(708, 78)

Me.Label9.Name = "Label9"

Me.Label9.Size = New System.Drawing.Size(128,
30)
Me.Label9.TabIndex =
18
Me.Label9.Text = "邮件附件:" &
Global.Microsoft.VisualBasic.ChrW(13) &
Global.Microsoft.VisualBasic.ChrW(10) &
"(保存到当前目录)"

'

'txtSubject

'

Me.TableLayoutPanel1.SetColumnSpan(Me.txtSubject,
3)
Me.txtSubject.Dock =
System.Windows.Forms.DockStyle.Fill

Me.txtSubject.Location = New System.Drawing.Point(118,
153)
Me.txtSubject.Multiline =
True
Me.txtSubject.Name =
"txtSubject"
Me.txtSubject.Size =
New System.Drawing.Size(584, 30)

Me.txtSubject.TabIndex = 19

'

'Label10

'
Me.Label10.AutoSize =
True
Me.Label10.Dock =
System.Windows.Forms.DockStyle.Right

Me.Label10.Location = New System.Drawing.Point(60,
150)
Me.Label10.Name =
"Label10"
Me.Label10.Size = New
System.Drawing.Size(52, 36)

Me.Label10.TabIndex = 20

Me.Label10.Text = "主题:"

'

'lstAttachments

'
Me.lstAttachments.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Bottom)
_

Or System.Windows.Forms.AnchorStyles.Left)
_

Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)

Me.lstAttachments.FormattingEnabled =
True
Me.lstAttachments.ItemHeight
= 15
Me.lstAttachments.Location =
New System.Drawing.Point(708, 120)

Me.lstAttachments.Name =
"lstAttachments"

Me.TableLayoutPanel1.SetRowSpan(Me.lstAttachments,
3)
Me.lstAttachments.Size = New
System.Drawing.Size(168, 378)

Me.lstAttachments.TabIndex = 21

'

'btnConnectLogin

'
Me.btnConnectLogin.Location =
New System.Drawing.Point(708, 13)

Me.btnConnectLogin.Name =
"btnConnectLogin"

Me.btnConnectLogin.Size = New System.Drawing.Size(162,
29)
Me.btnConnectLogin.TabIndex =
22
Me.btnConnectLogin.Text =
"连接并注册"

Me.btnConnectLogin.UseVisualStyleBackColor =
True

'

'TableLayoutPanel1

'
Me.TableLayoutPanel1.ColumnCount
= 7

Me.TableLayoutPanel1.ColumnStyles.Add(New
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute,
10.0!))

Me.TableLayoutPanel1.ColumnStyles.Add(New
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent,
12.13031!))

Me.TableLayoutPanel1.ColumnStyles.Add(New
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent,
27.84196!))

Me.TableLayoutPanel1.ColumnStyles.Add(New
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent,
20.00924!))

Me.TableLayoutPanel1.ColumnStyles.Add(New
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent,
20.00924!))

Me.TableLayoutPanel1.ColumnStyles.Add(New
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent,
20.00924!))

Me.TableLayoutPanel1.ColumnStyles.Add(New
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute,
10.0!))

Me.TableLayoutPanel1.Controls.Add(Me.txtPOP3Server, 2,
1)

Me.TableLayoutPanel1.Controls.Add(Me.btnFetchEmail, 5,
2)

Me.TableLayoutPanel1.Controls.Add(Me.lstAttachments, 5,
4)

Me.TableLayoutPanel1.Controls.Add(Me.btnConnectLogin, 5,
1)

Me.TableLayoutPanel1.Controls.Add(Me.Label1, 1,
1)

Me.TableLayoutPanel1.Controls.Add(Me.txtName, 2,
2)

Me.TableLayoutPanel1.Controls.Add(Me.Label10, 1,
5)

Me.TableLayoutPanel1.Controls.Add(Me.txtBody, 2,
6)

Me.TableLayoutPanel1.Controls.Add(Me.Label8, 1,
6)

Me.TableLayoutPanel1.Controls.Add(Me.Label3, 1,
2)

Me.TableLayoutPanel1.Controls.Add(Me.Label5, 1,
3)

Me.TableLayoutPanel1.Controls.Add(Me.txtEmailCount, 2,
3)

Me.TableLayoutPanel1.Controls.Add(Me.Label2, 3,
1)

Me.TableLayoutPanel1.Controls.Add(Me.txtFrom, 2,
4)

Me.TableLayoutPanel1.Controls.Add(Me.Label7, 1,
4)

Me.TableLayoutPanel1.Controls.Add(Me.txtPort, 4,
1)

Me.TableLayoutPanel1.Controls.Add(Me.Label4, 3,
2)

Me.TableLayoutPanel1.Controls.Add(Me.txtPassword, 4,
2)

Me.TableLayoutPanel1.Controls.Add(Me.txtRead, 4,
3)

Me.TableLayoutPanel1.Controls.Add(Me.Label6, 3,
3)

Me.TableLayoutPanel1.Controls.Add(Me.txtSubject, 2,
5)

Me.TableLayoutPanel1.Controls.Add(Me.Label9, 5,
3)
Me.TableLayoutPanel1.Dock =
System.Windows.Forms.DockStyle.Fill

Me.TableLayoutPanel1.Location = New System.Drawing.Point(0,
0)
Me.TableLayoutPanel1.Name =
"TableLayoutPanel1"

Me.TableLayoutPanel1.RowCount = 8

Me.TableLayoutPanel1.RowStyles.Add(New
System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute,
10.0!))

Me.TableLayoutPanel1.RowStyles.Add(New
System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent,
7.142858!))

Me.TableLayoutPanel1.RowStyles.Add(New
System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent,
6.709957!))

Me.TableLayoutPanel1.RowStyles.Add(New
System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent,
8.008658!))

Me.TableLayoutPanel1.RowStyles.Add(New
System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent,
6.709957!))

Me.TableLayoutPanel1.RowStyles.Add(New
System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent,
7.359307!))

Me.TableLayoutPanel1.RowStyles.Add(New
System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent,
64.06927!))

Me.TableLayoutPanel1.RowStyles.Add(New
System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute,
10.0!))
Me.TableLayoutPanel1.Size
= New System.Drawing.Size(892,
512)
Me.TableLayoutPanel1.TabIndex
= 23

'

'FetchEmail

'
Me.AutoScaleDimensions = New
System.Drawing.SizeF(8.0!, 15.0!)

Me.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font

Me.ClientSize = New System.Drawing.Size(892,
512)

Me.Controls.Add(Me.TableLayoutPanel1)

Me.Name = "FetchEmail"

Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen

Me.Text = "接收电子邮件"

Me.TableLayoutPanel1.ResumeLayout(False)

Me.TableLayoutPanel1.PerformLayout()

Me.ResumeLayout(False)


End Sub
Friend WithEvents Label1 As
System.Windows.Forms.Label
Friend WithEvents txtPOP3Server
As System.Windows.Forms.TextBox
Friend WithEvents
btnFetchEmail As System.Windows.Forms.Button
Friend
WithEvents txtPort As System.Windows.Forms.TextBox
Friend
WithEvents Label2 As System.Windows.Forms.Label
Friend
WithEvents txtName As System.Windows.Forms.TextBox
Friend
WithEvents Label3 As System.Windows.Forms.Label
Friend
WithEvents txtPassword As System.Windows.Forms.TextBox

Friend WithEvents Label4 As System.Windows.Forms.Label

Friend WithEvents txtEmailCount As
System.Windows.Forms.TextBox
Friend WithEvents Label5 As
System.Windows.Forms.Label
Friend WithEvents txtRead As
System.Windows.Forms.TextBox
Friend WithEvents Label6 As
System.Windows.Forms.Label
Friend WithEvents txtBody As
System.Windows.Forms.TextBox
Friend WithEvents txtFrom As
System.Windows.Forms.TextBox
Friend WithEvents Label7 As
System.Windows.Forms.Label
Friend WithEvents Label8 As
System.Windows.Forms.Label
Friend WithEvents Label9 As
System.Windows.Forms.Label
Friend WithEvents txtSubject As
System.Windows.Forms.TextBox
Friend WithEvents Label10 As
System.Windows.Forms.Label
Friend WithEvents
lstAttachments As System.Windows.Forms.ListBox
Friend
WithEvents btnConnectLogin As System.Windows.Forms.Button

Friend WithEvents TableLayoutPanel1 As
System.Windows.Forms.TableLayoutPanel
End Class



'


Imports OpenPop.Common
Imports OpenPop.Mime
Imports
OpenPop.Pop3
Imports System.IO
Imports
System.Collections.Generic
Imports System.Net.Security
Imports
System.Security.Cryptography.X509Certificates


Public Class FetchEmail
'pop服务器

Private Property pop3Server As
String

Get
Return
Me.txtPOP3Server.Text
End
Get
Set(ByVal value As
String)

Me.txtPOP3Server.Text = value
End
Set
End Property


' 服务器端口
Private Property pop3port As
String

Get
Return
Me.txtPort.Text
End
Get
Set(ByVal value As
String)

Me.txtPort.Text = value
End
Set
End Property

'注册用户名
Private Property LoginName As
String

Get
Return
Me.txtName.Text
End
Get
Set(ByVal value As
String)

Me.txtName.Text = value
End
Set
End Property

'密码
Private Property LoginPassword As
String

Get
Return
Me.txtPassword.Text
End
Get
Set(ByVal value As
String)

Me.txtPassword.Text = value
End
Set
End Property

'要读取的邮件号
Private Property messageNumber As
Integer
Get


Return
CInt(Me.txtRead.Text)
End
Get
Set(ByVal value As
Integer)

If value > CInt(Me.txtEmailCount.Text)
Then

value =
CInt(Me.txtEmailCount.Text)

End If
If
value < 0
Then

value = 0

End If

Me.txtRead.Text = CStr(value)
End
Set
End Property

''全部邮件数
'Dim emailCount As Integer =
0
'是否使用SSL
Dim useSsl As Boolean =
False
'声明pop3client变量
Dim client As
Pop3Client


'读取指定邮件
Private Sub
btnFetchEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnFetchEmail.Click



'设置光标
Me.Cursor =
Cursors.WaitCursor


'We want to check the headers of
the message before we download
'
邮件头信息,
Dim headers As
Header.MessageHeader

Try

headers =
client.GetMessageHeaders(messageNumber)

Catch

MsgBox("未找到该邮件")

'按钮恢复

headers =
Nothing

SetButtonEnabled(False,
0)

'退出过程

exitBtnSub()

Exit Sub
End
Try
Dim from As
Header.RfcMailAddress =
headers.From
Dim subject As String
= headers.Subject
Me.txtFrom.Text
= from.Address
Me.txtSubject.Text
= subject



'下载整个邮件
Dim emailMessage As
Message =
client.GetMessage(messageNumber)

'定义messagepart变量
Dim
emailMessagePart As MessagePart


emailMessagePart =
emailMessage.FindFirstPlainTextVersion

'如果邮件内容是文本格式
If Not
IsNothing(emailMessagePart)
Then

'显示文本内容

Me.txtBody.Text =
emailMessagePart.GetBodyAsText

Else

'尝试其他文本格式显示

Dim lstMessagepart As List(Of MessagePart) '定义一个泛型
list

lstMessagepart =
emailMessage.FindAllTextVersions

If lstMessagepart.Count >= 1
Then

Me.txtBody.Text =
lstMessagepart(0).GetBodyAsText

Else

Me.txtBody.Text =
"不能显示"
End
If


End If



''保存附件
'For Each attachment As
MessagePart In
emailMessage.FindAllAttachments

'
'if(attachment.FileName.Equals("useful.pdf"))

' File.WriteAllBytes(attachment.FileName,
attachment.Body)
'Next


'查找附件



'创建附件列表,首先定义一个包含meaagepart类型的list泛型

Dim EmailAttachments As List(Of
MessagePart)

'
EmailAttachments =
emailMessage.FindAllAttachments

'逐个查找
Dim i As Integer =
0
For Each AttachmentOfEmail As
MessagePart In
EmailAttachments

'将文件名添加到列表

Me.lstAttachments.Items.Add(AttachmentOfEmail.FileName)

'
If Not
IsNothing(AttachmentOfEmail)
Then

'因为要存在当前目录,所以查看当前目录下是否存在该文件

Dim WillSaveFile As New FileInfo(AttachmentOfEmail.FileName)



If WillSaveFile.Exists
Then

'删除

WillSaveFile.Delete()



End
If

Try

'保存

AttachmentOfEmail.SaveToFile(WillSaveFile)

Catch ex As Exception



End Try

End If

Next

'按钮恢复
SetButtonEnabled(False,
0)

'退出过程
exitBtnSub()


Me.Cursor = Cursors.Default


End Sub


'中断连接
Private Sub exitBtnSub()


client = Nothing


End Sub


'连接并注册
Private Sub
btnConnectLogin_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles
btnConnectLogin.Click

'建立一个新的对象
client = New
Pop3Client
Try



'连接到服务器

client.Connect(pop3Server, pop3port,
useSsl)

'在服务器上注册

client.Authenticate(LoginName,
LoginPassword)

'获取邮箱邮件总数

Me.txtEmailCount.Text =
client.GetMessageCount().ToString

'按钮设置

SetButtonEnabled(True, CInt(Me.txtEmailCount.Text))



Catch

MsgBox("不能在服务器上注册")

'按钮恢复

SetButtonEnabled(False,
0)

'退出过程

exitBtnSub()

Exit Sub
End
Try
End Sub


'设置按钮过程
Private Sub
SetButtonEnabled(ByVal TrueOrFalse As Boolean, ByVal emailNumber As Integer)


If TrueOrFalse Then
'成功连接到服务器,并在服务器上注册成功

Me.btnConnectLogin.Enabled = False
'连接按钮不可用

Me.btnFetchEmail.Enabled = True
'收信按钮可用

Me.txtRead.ReadOnly = False
'想读取的邮箱号文本框可写

Me.txtRead.Text = emailNumber
'设置要读取的邮箱号是最大的邮件号

'清空发件人、主题、内容、附件

Me.txtFrom.Clear()

Me.txtSubject.Clear()

Me.txtBody.Clear()

Me.lstAttachments.Items.Clear()

Else
'连接已被取消

Me.btnConnectLogin.Enabled = True
'连接按钮可用

Me.btnFetchEmail.Enabled = False
'收信按钮不可用

Me.txtRead.ReadOnly = True
'想读取的邮箱号文本框不可写

'Me.txtRead.Text = 0 '设置要读取的邮箱号是0



End If

End Sub


'限制该文本框只可以输入整数
Private Sub
txtRead_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles
txtRead.KeyPress
If
Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8)
Then

e.Handled = False

Else

e.Handled = True
End If


End Sub


'设置属性
Private Sub
txtRead_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles txtRead.TextChanged

Me.messageNumber = CInt(Me.txtRead.Text)
End Sub
End
Class


posted on 2011-04-25 08:54  水光  阅读(3363)  评论(1编辑  收藏  举报

导航