您的位置是: 软件 > 开发者网络 > 开发工具 > 开发专栏 > VB > 正文 查cseek报价

用Visual Basic .Net发送电子邮件
2001-12-31作者: 阿虎出处: yesky
我对此感兴趣
订阅电子杂志
发表评论
advertisement
热点推荐
· 群雄逐鹿 十四种Java开发工具点评
· 网文快捕,将网络资源搬回家
· Windows系统瘦身计划
· PS多媒体教程带你享受快乐学习
· 网络游戏外挂设计深度探索
· 武装你的桌面,桌面工具大阅兵

上一页  1 2 3  



  四.本文源程序代码( 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中的类库。



上一页  1 2 3  


【责任编辑:方舟】
推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志发表评论

天极商业街
相关内容阅读排行榜
  • 日本发行新型限定版GameCube
  • 图谋低端市场 Sempron一门三杰再续辉煌
  • 密蜂大盗木马 开MM摄像头偷窥隐私
  • Win2000注册表的备份与恢复
  • 制作无人值守的Win XP安装光盘
  • 联众世界高校棋牌联赛完美谢幕
  • 全新技嘉i915P主板GA-8GPNXP Duo抢先细看
  • 《信仰》7月火热引爆精彩活动

  • advertisement