長(zhǎng)時(shí)間使用Outlook Express收發(fā)電子郵件的軟件,你是不是也有點(diǎn)煩?是不是像我一樣,也想什么時(shí)候自己動(dòng)手編寫(xiě)一個(gè)屬于自己收發(fā)電子郵件的軟件。如果是這樣,那么隨我來(lái)吧!其實(shí)在VB中,應(yīng)用程序可以通過(guò)調(diào)用微軟公司的MAPI(Messaging Application Programming Interface)消息應(yīng)用程序編程接口,輕松實(shí)現(xiàn)收發(fā)電子郵件的功能。
VB中有兩個(gè)郵件收發(fā)功能的核心控件:MAPI會(huì)話和MAPI消息控件。MAPI會(huì)話控件用于建立和控制一個(gè)Microsoft Mail會(huì)話,MAPI消息控件用于創(chuàng)建和收發(fā)郵件消息。此外,程序必須運(yùn)行在采用MAPI的消息系統(tǒng)(如Microsoft Exchange 、Outlook等)的環(huán)境中。
窗體設(shè)計(jì)
打開(kāi)VB,在Form1中加入5個(gè)文本框,7個(gè)命令按鈕,同時(shí)通過(guò)添加部件的方法將MAPI控件加入窗體中(如圖1)。各對(duì)象的屬性設(shè)置如圖2。
圖1 窗體設(shè)置
圖2 對(duì)象屬性設(shè)置
編寫(xiě)代碼
Private Sub Command1_Click()
If Val(Text2.Text) <= 0 Then Exit Sub
Text2.Text = Val(Text2.Text) - 1
Call xs ()
End Sub
Private Sub Command2_Click()
If Val(Text2.Text) >= Val(Text1.Text) - 1 Then Exit Sub
Text2.Text = Val(Text2.Text) + 1
Call xs()
End Sub
Private Sub Command3_Click()
MAPIMessages1.SessionID = MAPISession1.SessionID
MAPIMessages1.MsgIndex = -1 ′ 建立發(fā)送緩沖區(qū)
MAPIMessages1.Compose ′ 寫(xiě)新郵件
MAPIMessages1.Send True
End Sub
Private Sub xs()
MAPIMessages1.MsgIndex = Val(Text2.Text) ′ 指定電子郵件
Text3.Text = MAPIMessages1.MsgSubject ′ 電子郵件主題
Text4.Text = MAPIMessages1.MsgOrigDisplayName
′ 電子郵件發(fā)信人姓名
Text5.Text = MAPIMessages1.MsgNoteText ′ 電子郵件全文
End Sub
Private Sub Command4_Click()
MAPIMessages1.SessionID = MAPISession1.SessionID
MAPIMessages1.MsgIndex = Val(Text2.Text)
MAPIMessages1.Reply
MAPIMessages1.Send True
End Sub
Private Sub Command5_Click()
MAPISession1.SignOff
MAPISession1.DownLoadMail = True
MAPISession1.SignOn
MAPIMessages1.SessionID = MAPISession1.SessionID
MAPIMessages1.FetchUnreadOnly = False
MAPIMessages1.Fetch
Text1.Text = MAPIMessages1.MsgCount
Text2.Text = 0
End Sub
Private Sub Command6_Click()
On Error GoTo err:
MAPIMessages1.Show
err:
End Sub
Private Sub Form_Activate()
Call xs
End Sub
Private Sub Form_Load() ′ 系統(tǒng)初始化
MAPISession1.DownLoadMail = False
MAPISession1.SignOn ′ 處理開(kāi)始
MAPIMessages1.SessionID = MAPISession1.SessionID
MAPIMessages1.FetchUnreadOnly = False
MAPIMessages1.Fetch
Text1.Text = MAPIMessages1.MsgCount
Text2.Text = 0
End Sub
Private Sub Command7_Click()
MAPISession1.SignOff ′ 處理結(jié)束
End
End Sub
好了,代碼終于寫(xiě)完了,運(yùn)行一下,看看效果,還不錯(cuò)吧!當(dāng)然,如果你想更完善一些,就需要你自己努力了。本程序在Windows XP下運(yùn)行通過(guò)。