做一个自己的电子邮件软件
时间:2018-08-05 11:33:27来源:网络整理
长时间使用Outlook Express收发电子邮件的软件,你是不是也有点烦?是不是像我一样,也想什么时候自己动手编写一个属于自己收发电子邮件的软件。如果是这样,那么随我来吧!其实在VB中,应用程序可以通过调用微软公司的MAPI(Messaging Application Programming Interface)消息应用程序编程接口,轻松实现收发电子邮件的功能。
VB中有两个邮件收发功能的核心控件:MAPI会话和MAPI消息控件。MAPI会话控件用于建立和控制一个Microsoft Mail会话,MAPI消息控件用于创建和收发邮件消息。此外,程序必须运行在采用MAPI的消息系统(如Microsoft Exchange 、Outlook等)的环境中。
窗体设计
打开VB,在Form1中加入5个文本框,7个命令按钮,同时通过添加部件的方法将MAPI控件加入窗体中(如图1)。各对象的属性设置如图2。
图1 窗体设置
图2 对象属性设置
编写代码
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 ′ 建立发送缓冲区
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
好了,代码终于写完了,运行一下,看看效果,还不错吧!当然,如果你想更完善一些,就需要你自己努力了。本程序在Windows XP下运行通过。
VB中有两个邮件收发功能的核心控件:MAPI会话和MAPI消息控件。MAPI会话控件用于建立和控制一个Microsoft Mail会话,MAPI消息控件用于创建和收发邮件消息。此外,程序必须运行在采用MAPI的消息系统(如Microsoft Exchange 、Outlook等)的环境中。
窗体设计
打开VB,在Form1中加入5个文本框,7个命令按钮,同时通过添加部件的方法将MAPI控件加入窗体中(如图1)。各对象的属性设置如图2。
图1 窗体设置
图2 对象属性设置
编写代码
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 ′ 建立发送缓冲区
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
好了,代码终于写完了,运行一下,看看效果,还不错吧!当然,如果你想更完善一些,就需要你自己努力了。本程序在Windows XP下运行通过。
本类热门