VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 用Visual Basic .Net发送电子邮件
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:8429, 日期:2002-01-07
 如果要用Visual Basic来做一个发送电子邮件的程序,大部分人将会采用Active X组件的方式,虽然目前已经有很多可以用来发送电子邮件的Active X组件,但这些组件一般都是外国的产品,一般也都是试用版本,有着各种各样的限制,就是没有限制,那些免费的产品一般也都没有使用说明,所以要想在程序中使用他是很有难度的。自从微软公司推出了.Net框架以后,做为.Net框架中的一种重要的程序设计语言--Visual Basic .Net,可以十分方便的实现这个功能,做一个发送电子邮件程序,并且也不需要使用任何的ActiveX组件。这是因为在.Net框架中有一个通用的软件开发包--.Net FrameWork SDK。这个软件开发包内容十分丰富,封装了许多功能十分强大的类库,这当中就包含着用以发送电子邮件的类库。Visual Basic .Net作为.Net框架中的一个重要组成部分,可以直接调用这个软件开发包中的类库。Visual Basic .Net就是通过调用这个软件开发包中发送电子邮件相关的类库来发送电子邮件。

  一.软件开发和运行的环境设置以及前期准备条件:

  I > .视窗系统2000服务器版

  II > ..Net FrameWork SDK Beta 2版

  III > .打开"控制面板",进入"添加和删除程序",然后再点击"添加/删除Windows组件",就可以看见以下界面:


图01:系统配置界面01

  点中"Internet信息服务( IIS )",然后点击"详细信息",可得到如下界面:


      图02:系统配置界面

  点中选择"SMTP Serverce"选项,然后按"确定"按钮。再按"下一步"按钮,则系统在重新启动后,就会安装好运行本文程序所需要的SMTP Service了。

 二.Visual Basic .Net是如何发送电子邮件:

  在.Net FrameWork SDK Beta 2版中,有一个叫做System.Web.Mail的名称空间,这个名称空间被封装在System.Web.dll文件中。在这个名称空间中定义了关于发送电子邮件的方法、对象和属性。Visual Basic .Net就是通过调用此名称空间中的方法、对象和属性,发送电子邮件的。在本文中,发送电子邮件主要用到了二个对象:一个是MailMessage对象,此对象主要是封装电子邮件的各个属性,即所谓的发信人,收信人,信件的主题,信件的内容和信件的附件等。另外一个是SmtpMail对象,这个对象的主要的作用是把已经定义好各个属性的MailMessage对象(即电子邮件内容)给发送出去,这是通过调用SmtpMail对象的Send ( )方法来实现的

  三.在Visual Basic .Net中做发送电子邮件程序的主要步骤:

  (1).首先就要在程序的最前面导入封装对象的名称空间,具体如下:

    Imports System.Web.Mail

  (2). MailMessage对象中的各个属性的含义:

  下表是MailMessage对象中的各个属性以及他们所代表的意思:

属性名称 代表意义
From 电子邮件源地址
To 电子邮件目的地址
Subject 电子邮件邮件主题
Priority 电子邮件优先级 ( High , Low , Normal )
Attachments 电子邮件的附件
Bcc 电子邮件的暗送地址
Cc 电子邮件的抄送地址
Body 电子邮件内容主体
Bodyformat 电子邮件格式( Html , Text )
Bodyencoding 电子邮件编码( Base64 , Uuencode )

  在程序中,通过以下代码实现对电子邮件的各个属性的定义,具体如下:

Dim aMessage as new MailMessage
'新建一个MailMessage对象
aMessage.From = FromTextBox.Text
'定义发信人地址,如果是多人,可以用","分开
aMessage.To = ToTextBox.Text
'定义收信人地址,如果是多人,可以用","分开
aMessage.Cc = CCTextBox.Text
'定义抄送人地址,如果是多人,可以用","分开
aMessage.Bcc = BCCTextBox.Text
'定义暗送人地址,如果是多人,可以用","分开
aMessage.Subject = SubjectTextBox.Text
'定义邮件的主题
aMessage.Body = MessageTextBox.Text
'定义邮件的内容
if AttachmentTextBox.Text.Length > 0 then
aMessage.Attachments.Add ( new MailAttachment ( AttachmentTextBox.Text , MailEncoding.Base64 ) )
End if
'给邮件增加一个附件

  可见定义用Visual Basic .Net来定义电子邮件属性是比较方便的。

  注:以上程序代码中"="右边是程序中定义的文本框的"Text"值。

  (3).用SmtpMail对象发送已经定义好的各个属性的MailMessage对象:

  用Visual Basic .Net调用SmtpMail对象的Send ( )方法有多种方式。本文介绍的只是其中的一种比较常用的调用方式,即:SmtpMail.Send ( MailMessage对象 )。在程序中的实现语句如下:

SmtpMail.Send ( aMessage )
'发送电子邮件

 四.本文源程序代码( Send.vb ):

  在介绍完以上的内容以后,不难得到一个用Visual Basic .Net做发送电子邮件的完整的程序代码,具体如下:

Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Web
Imports System.Web.Mail
'导入程序中使用到的名称空间
Public Class Form1
Inherits Form
private label1 as Label
private label2 as Label
private label3 as Label
private WithEvents SendButton as Button
private WithEvents ExitButton as Button
private FromTextBox as TextBox
private ToTextBox as TextBox
private SubjectTextBox as TextBox
private MessageTextBox as TextBox
private CCTextBox as TextBox
private CCLabel as Label
private BCCTextBox as TextBox
private label4 as Label
private label5 as Label
private WithEvents BrowseButton as Button
private openFileDialog1 as OpenFileDialog
private AttachmentTextBox as TextBox
private components as System.ComponentModel.Container

public Sub 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 Sub InitializeComponent ( )
MessageTextBox = new TextBox ( )
ToTextBox = new TextBox ( )
SendButton = new Button ( )
ExitButton = new Button ( )
FromTextBox = new TextBox ( )
label1 = new Label ( )
SubjectTextBox = new TextBox ( )
label2 = new Label ( )
label3 = new Label ( )
CCTextBox = new TextBox ( )
CCLabel = new Label ( )
BCCTextBox = new TextBox ( )
label4 = new Label ( )
label5 = new Label ( )
AttachmentTextBox = new TextBox ( )
BrowseButton = new Button ( )
openFileDialog1 = new OpenFileDialog ( )

FromTextBox.Location = new System.Drawing.Point ( 96 , 16 )
FromTextBox.Name = "FromTextBox"
FromTextBox.Size = new System.Drawing.Size ( 240 , 20 )
FromTextBox.TabIndex = 0
FromTextBox.Text = ""

ToTextBox.Location = new System.Drawing.Point ( 96 , 53 )
ToTextBox.Name = "ToTextBox"
ToTextBox.Size = new System.Drawing.Size ( 240 , 20 )
ToTextBox.Text = ""
ToTextBox.TabIndex = 1

CCTextBox.Location = new System.Drawing.Point ( 96 , 88 )
CCTextBox.Name = "CCTextBox"
CCTextBox.Size = new System.Drawing.Size ( 240 , 20 )
CCTextBox.TabIndex = 2
CCTextBox.Text = ""

BCCTextBox.Location = new System.Drawing.Point ( 96 , 120 )
BCCTextBox.Name = "BCCTextBox"
BCCTextBox.Size = new System.Drawing.Size ( 240 , 20 )
BCCTextBox.TabIndex = 3
BCCTextBox.Text = ""

SubjectTextBox.Location = new System.Drawing.Point ( 96 , 152 )
SubjectTextBox.Name = "SubjectTextBox"
SubjectTextBox.Size = new System.Drawing.Size ( 240 , 20 )
SubjectTextBox.TabIndex = 4
SubjectTextBox.Text = ""

AttachmentTextBox.Location = new System.Drawing.Point ( 96 , 184 )
AttachmentTextBox.Name = "AttachmentTextBox"
AttachmentTextBox.Size = new System.Drawing.Size ( 168 , 20 )
AttachmentTextBox.TabIndex = 5
AttachmentTextBox.Text = ""

MessageTextBox.Location = new System.Drawing.Point ( 8 , 216 )
MessageTextBox.Multiline = true
MessageTextBox.Name = "MessageTextBox"
MessageTextBox.Size = new System.Drawing.Size ( 320 , 152 )
MessageTextBox.Text = ""
MessageTextBox.TabIndex = 6

BrowseButton.Location = new System.Drawing.Point ( 280 , 184 )
BrowseButton.Name = "BrowseButton"
BrowseButton.Size = new System.Drawing.Size ( 56 , 24 )
BrowseButton.Text = "浏览文件"
BrowseButton.TabIndex = 7

SendButton.Location = new System.Drawing.Point ( 64 , 380 )
SendButton.Name = "SendButton"
SendButton.Size = new System.Drawing.Size ( 72 , 24 )
SendButton.Text = "发送邮件"
SendButton.TabIndex = 8

ExitButton.Location = new System.Drawing.Point ( 200 , 380 )
ExitButton.Name = "ExitButton"
ExitButton.Size = new System.Drawing.Size ( 72 , 24 )
ExitButton.Text = "退出程序"
ExitButton.TabIndex = 9

label1.Font = new System.Drawing.Font ( "宋体", 9F )
label1.Location = new System.Drawing.Point ( 48 , 16 )
label1.Name = "label1"
label1.Size = new System.Drawing.Size ( 48 , 16 )
label1.Text = "发信人:"

label2.Font = new System.Drawing.Font ( "宋体", 9F )
label2.Location = new System.Drawing.Point ( 48 , 53 )
label2.Name = "label2"
label2.Size = new System.Drawing.Size ( 48 , 16 )
label2.Text = "收信人:"

label3.Font = new System.Drawing.Font ( "宋体", 9F )
label3.Location = new System.Drawing.Point ( 48 , 152 )
label3.Name = "label3"
label3.Size = new System.Drawing.Size ( 48 , 16 )
label3.Text = "主 题:"

CCLabel.Font = new System.Drawing.Font ( "宋体", 9F )
CCLabel.Location = new System.Drawing.Point ( 48 , 88 )
CCLabel.Name = "CCLabel"
CCLabel.Size = new System.Drawing.Size ( 48 , 16 )
CCLabel.Text = "抄 送:"

label4.Font = new System.Drawing.Font ( "宋体", 9F )
label4.Location = new System.Drawing.Point ( 48 , 120 )
label4.Name = "label4"
label4.Size = new System.Drawing.Size ( 48 , 16 )
label4.Text = "暗 送:"

label5.Font = new System.Drawing.Font ( "宋体", 9F )
label5.Location = new System.Drawing.Point ( 48 , 184 )
label5.Name = "label5"
label5.Size = new System.Drawing.Size ( 48 , 16 )
label5.Text = "附 件:"

openFileDialog1.Title = "选择文件作为邮件的附件:"

Me.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 )
Me.ClientSize = new System.Drawing.Size ( 344 , 413 )

Me.Controls.Add ( BrowseButton )
Me.Controls.Add ( AttachmentTextBox )
Me.Controls.Add ( label5 )
Me.Controls.Add ( label4 )
Me.Controls.Add ( BCCTextBox )
Me.Controls.Add ( CCLabel )
Me.Controls.Add ( CCTextBox )
Me.Controls.Add ( ExitButton )
Me.Controls.Add ( SendButton )
Me.Controls.Add ( label3 )
Me.Controls.Add ( label2 )
Me.Controls.Add ( label1 )
Me.Controls.Add ( SubjectTextBox )
Me.Controls.Add ( ToTextBox )
Me.Controls.Add ( FromTextBox )
Me.Controls.Add ( MessageTextBox )
Me.Name = "Form1"
Me.Text = "用Visual Basic .Net做邮件发送软件!"
Me.ResumeLayout ( false )

End Sub


'处理发送按钮单击事件
Private Sub SendButton_Click ( ByVal sender As Object , _
ByVal e As System.EventArgs ) Handles SendButton.Click
Dim aMessage as new MailMessage
'新建一个MailMessage对象
aMessage.From = FromTextBox.Text
'定义发信人地址,如果是多人,可以用","分开
aMessage.To = ToTextBox.Text
'定义收信人地址,如果是多人,可以用","分开
aMessage.Cc = CCTextBox.Text
'定义抄送人地址,如果是多人,可以用","分开
aMessage.Bcc = BCCTextBox.Text
'定义暗送人地址,如果是多人,可以用","分开
aMessage.Subject = SubjectTextBox.Text
'定义邮件的主题
aMessage.Body = MessageTextBox.Text
'定义邮件的内容
if AttachmentTextBox.Text.Length > 0 then
aMessage.Attachments.Add ( new MailAttachment ( AttachmentTextBox.Text , MailEncoding.Base64 ) )
End if


'给邮件增加一个附件
SmtpMail.Send ( aMessage )
'发送电子邮件
MessageBox.Show( "电子邮件已经发送到->" + ToTextBox.Text )
End Sub
'处理退出按钮单击的事件
Private Sub ExitButton_Click ( ByVal sender As Object , _
ByVal e As System.EventArgs ) Handles ExitButton.Click
Application.Exit ( )
End Sub
'处理浏览文件按钮单击的事件
Private Sub BrowseButton_Click ( ByVal sender As Object , _
ByVal e As System.EventArgs ) Handles BrowseButton.Click
if openFileDialog1.ShowDialog ( ) = DialogResult.OK then
AttachmentTextBox.Text = openFileDialog1.FileName
End if
End Sub
End Class

Module Module1
Sub Main ( )
Application.Run ( new Form1 ( ) )
End sub
End Module


  五.编译源程序文件和编译好的程序的执行界面:

  在编译Visual Basic .Net和Visual C#的程序的时候,发现一个比较大的不同点,就是用Visual C#的编译器Csc.exe编译文件,可以在编译器后面不加上引用,编译器自动加上,可以正确编译。但在用Visual Basic .Net的编译器Vbc.exe来编译文件,如果在源程序中用到的引用,在编译的时候没有加上,则会出错。所以只有通过以下命令编译后,才可以得到执行程序Send.exe。

Vbc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /r:system.web.dll send.vb

下图是Send.exe执行的界面:


  图03:用Visual Basic .Net做的邮件发送程序的运行界面

  六.总结:

  至此已经介绍了在ASP.NET页面和用Visual C#、Visual Basic .Net来发送电子邮件,由于他们的各自语法不同,所以具体的实现语句也就不一样。但主要的实现思想是一致的,就是通过调用.Net FrameWork SDK中的类库。