 
         
         
         
         
        版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Visual C#是微软公司推出的下一代程序开发语言。他不仅具有Visual C+功能强大的特点,又具有Visual Basic的简洁,易上手的特点。所以一经推出,就收到了广大程序开发人员的欢迎。Visual C#和Visual C+的一个明显的区别在于,Visual C#本身是没有类库的,而Visual C+却是自身就带有类库。Visual C#虽然没有类库,但作为.Net框架中的一个十分重要的开发语言。他可以使用.Net框架提供的一个通用的软件开发包-.Net FrameWork SDK。这个软件开发包可以说是Visual C#功能的延伸,Visual C#就是通过他实现了自身无法实现的很
2、多功能。本文就是来介绍Visual C#如何利用这个软件开发包来发送电子邮件的。 一软件开发和运行的环境设置 I > .视窗系统2000服务器版 II > .Net FrameWork SDK Beta 2版 III > .打开"控制面板",进入"添加和删除程序",然后再点击"添加/删除Windows组件",就可以看见以下界面: 图01:系统配置界面 点中"Internet信息服务( IIS )",然后点击"详细信息",可得到如下界面: 图02:系统配置界面 点中选择"
3、;SMTP Serverce"选项,然后按"确定"按钮。再按"下一步"按钮,则系统在重新启动后,就会安装好运行本文程序所需要的SMTP Service了。二Visual C#如何发送电子邮件: 在.Net FrameWork SDK Beta 2版中,有一个叫做System.Web.Mail的名称空间,在这个名称空间中封装发送电子邮件的方法、对象和属性。Visual C#就是通过调用此名称空间中的方法、对象和属性,发送电子邮件的。在本文中,发送电子邮件主要用到了二个对象:一个是MailMessage对象,此对象主要是封装电子邮件的各个属性,即所
4、谓的发信人,收信人,信件的主题,信件的内容和信件的附件等。另外一个是SmtpMail对象,这个对象的最大作用是把已经定义好各个属性的MailMessage对象给发送出去,而完成此功能,就需要调用SmtpMail对象的Send ( )方法。 三在Visual C#中正确使用发送电子邮件相关的对象: (1).要调用对象,当然首先就要在程序的最前面导入封装对象的名称空间,具体如下: using System.Web.Mail ; (2).正确定义MailMessage对象的属性: MailMessage对象中和电子邮件相关的属性可以用下表来表示: 属性名称 代表意义 From 源地址 To
5、 目的地址 Subject 邮件主题 Priority 邮件优先级 ( High , Low , Normal ) Attachments 附件 Bcc 暗送地址 Cc 抄送地址 Body 邮件内容主体 Bodyformat 邮件格式( Html , Text ) Bodyencoding 邮件编码( Base64 , Uuencode ) 在程序中,具体的实现语句如下: MailMessage aMessage = new MailMessage ( ) ; /新建一个MailMessage对象 aMessage.From = FromTextBox.Text ; /定义发信人地址,如果是多
6、人,可以用","分开 aMessage.To = ToTextBox.Text ; /定义收信人地址,如果是多人,可以用","分开 aMessage.Cc = CCTextBox.Text ; /定义抄送人地址,如果是多人,可以用","分开 aMessage.Bcc = BCCTextBox.Text ; /定义暗送人地址,如果是多人,可以用","分开 aMessage.Subject = SubjectTextBox.Text ; /定义邮件的主题 aMessage.Body = MessageTextBox.T
7、ext ; /定义邮件的内容 if ( AttachmentTextBox.Text.Length > 0 ) aMessage.Attachments.Add ( new MailAttachment ( AttachmentTextBox.Text , MailEncoding.Base64 ) ) ; /给邮件增加一个附件 注:""右边是程序中定义的文本框的"Text"值。 (3).用SmtpMail对象正确发送电子邮件: 在Visual C#中调用SmtpMail对象的Send ( )方法有多种方式。本文介绍的只是其中的一种比较常
8、用的调用方式,即:SmtpMail.Send ( MailMessage对象 )。在程序中的实现语句如下:SmtpMail.Send ( aMessage ) ; 四本文源程序代码( Send.cs )以及程序运行界面: 下图是编译好的程序的运行界面: 图03:Send.cs程序运行界面以下是Send.cs源程序代码: using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Da
9、ta ; using System.Web ; using System.Web.Mail ; /导入程序中使用到的名称空间 public class Form1 : Form private Label label1 ; private Label label2 ; private Label label3 ; private Button SendButton ; /发送 private Button ExitButton ; /退出 private TextBox FromTextBox ; /源地址 private TextBox ToTextBox ; /目的地址 private T
10、extBox SubjectTextBox ; /邮件主题 private TextBox MessageTextBox ;/ 邮件内容 private TextBox CCTextBox ; /抄送地址 private Label CCLabel ; private TextBox BCCTextBox ; /暗送地址 private Label label4 ; private Label label5 ; private Button BrowseButton ; /预览邮件 private OpenFileDialog openFileDialog1 ; /打开文件 private T
11、extBox AttachmentTextBox ; /附件 private System.ComponentModel.Container components = null ; public Form1 ( ) /主程序 InitializeComponent ( ) ; /清除在程序中使用的所有资源 protected override void Dispose ( bool disposing ) /调用清除初始化的方法 if ( disposing ) if ( components != null ) components.Dispose ( ) ; base.Dispose (
12、disposing ) ; aMessage.From = FromTextBox.Text ; /定义发信人地址,如果是多人,可以用","分开 注:""右边是程序中定义的文本框的"Text"值。 private void InitializeComponent ( ) /重新定义组件 MessageTextBox = new TextBox ( ) ; /重新命名ToTextBox = new TextBox ( ) ; SendButton = new Button ( ) ; ExitButton = new Button (
13、) ; 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
14、 = 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.
15、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 ) ; CCTe
16、xtBox.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 ( 2
17、40 , 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 =
18、 "" ; 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.Locati
19、on = 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
20、.Point ( 280 , 184 ) ; BrowseButton.Name = "BrowseButton" BrowseButton.Size = new System.Drawing.Size ( 56 , 24 ) ; BrowseButton.Text = "浏览文件" ; BrowseButton.TabIndex = 7 ; BrowseButton.Click += new System.EventHandler ( BrowseButton_Click ) ; SendButton.Location = new System.Dra
21、wing.Point ( 64 , 380 ) ; SendButton.Name = "SendButton" ; SendButton.Size = new System.Drawing.Size ( 72 , 24 ) ; SendButton.Text = "发送邮件" ; SendButton.TabIndex = 8 ; SendButton.Click += new System.EventHandler ( SendButton_Click ) ; ExitButton.Location = new System.Drawing.Poin
22、t ( 200 , 380 ) ; ExitButton.Name = "ExitButton" ; ExitButton.Size = new System.Drawing.Size ( 72 , 24 ) ; ExitButton.Text = "退出程序" ExitButton.TabIndex = 9 ; ExitButton.Click += new System.EventHandler ( ExitButton_Click ) ; label1.Font = new System.Drawing.Font ( "宋体",
23、 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 )
24、 ; 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 Sys
25、tem.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 = "抄 送
26、:" ; 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 ( "宋体&
27、quot;, 9F ) ; label5.Location = new System.Drawing.Point ( 48 , 184 ) ; label5.Name = "label5" ; label5.Size = new System.Drawing.Size ( 48 , 16 ) ; label5.Text = "附 件:" ; openFileDialog1.Title = "选择文件作为邮件的附件:" ; this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13
28、 ) ; this.ClientSize = new System.Drawing.Size ( 344 , 413 ) ; this.Controls.Add ( BrowseButton ) ; this.Controls.Add ( AttachmentTextBox ) ; this.Controls.Add ( label5 ) ; this.Controls.Add ( label4 ) ; this.Controls.Add ( BCCTextBox ) ; this.Controls.Add ( CCLabel ) ; this.Controls.Add ( CCTextBox
29、 ) ; this.Controls.Add ( ExitButton ) ; this.Controls.Add ( SendButton ) ; this.Controls.Add ( label3 ) ; this.Controls.Add ( label2 ) ; this.Controls.Add ( label1 ) ; this.Controls.Add ( SubjectTextBox ) ; this.Controls.Add ( ToTextBox ) ; this.Controls.Add ( FromTextBox ) ; this.Controls.Add ( Mes
30、sageTextBox ) ; this.Name = "Form1" ; this.Text = "用Visual C#做邮件发送软件!" ; this.ResumeLayout ( false ); static void Main ( ) Application.Run ( new Form1 ( ) ) ; private void SendButton_Click ( object sender , System.EventArgs e ) try MailMessage aMessage = new MailMessage ( ) ; /新建一个MailMessage对象 aMessage.From = FromTextBox.Text ; /定义发信人地址,如果是多人,可以用","分开 aMessage.To = ToTextBox.Text ; /定义收信人地址,如
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 上门考察合同(标准版)
- 鹰潭市人民医院病案质量管理考核
- 防震减灾知识讲座活动试题及答案
- 公司室内设计试题带答案
- 苏州市中医院护理人力资源管理基础与排班原则试题
- 大兴安岭人民医院手术并发症处理能力考核
- 景德镇市中医院烧伤重症监护技能考核
- 亳州市中医院卫生经济学原理在医院管理中的应用
- 舟山市中医院泌尿造口护理考核
- 淄博市中医院脑血管病脑电图考核
- 《2025年硅酸盐水泥熟料》知识培训
- 2024风电建设项目水土保持技术标准
- 高中英语新课标3000词汇表(新高考)
- 锚杆劳务承包合同
- 《中国政法大学》课件
- 《粤港澳大湾区发展规划纲要》(双语全文)
- 标准厂房财务分析与资金筹措
- 蓝色插画风运动员孙颖莎介绍
- 20以内的加法口算练习题4000题 205
- 小学生经典阅读英语短文100篇
- 食品安全体系FSSC22000-V6版标准要求及内审员培训教材
 
            
评论
0/150
提交评论