用VB实现自己的邮件“自己发”
VB中有两个邮件收发功能的核心控件:MAPI会话和MAPI消息控件。MAPI会话控件用于建立和控制一个Microsoft Mail会话,MAPI消息控件用于创建和收发邮件消息。此外,程序必须运行在采用MAPI的消息系统(如Microsoft Exchange、Outlook等)的环境中。
窗体设计
打开VB,在Form1中加入5个文本框,7个命令按钮,同时通过添加部件的方法将MAPI控件加入窗体中(如图1)。各对象氖粜陨柚萌缤?。
编写代码
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()
Text2.Text = Val(Text2.Text) + 1
Call xs()
End Sub
Private Sub Command3_Click()
MAPIMessages1.SessionID = MAPISession1.SessionID
MAPIMessages1.MsgIndex = -1′建立发送缓冲区
MAPIMessages1.Compose′写新邮件
MAPIMessages1.Send True
End Sub
Private Sub xs()
MAPIMessages1.MsgIndex = Val(Text2.Text)′指定电子邮件
Text3.Text = MAPIMessages1.MsgSubject ′电子邮件主题
Text4.Text = MAPIMessages1.MsgOrigDisplayName
′电子邮件发信人姓名
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()′系统初始化
MAPISession1.DownLoadMail = False
MAPISession1.SignOn′处理开始
MAPIMessages1.SessionID = MAPISession1.SessionID
MAPIMessages1.FetchUnreadOnly = False
MAPIMessages1.Fetch
Text1.Text = MAPIMessages1.MsgCount
Text2.Text = 0
End Sub
Private Sub Command7_Click()
MAPISession1.SignOff ′处理结束
End
End Sub