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#就是通过他实现了自身无法实现的很多功能。本文就是来介绍Visual C#如何利用这个软件开发包来发送电子邮件的。
一.软件开发和运行的环境设置:
I > .视窗系统2000服务器版
II > ..Net FrameWork SDK Beta 2版
III > .打开"控制面板",进入"添加和删除程序",然后再点击"添加/删除Windows组件",就可以看见以下界面:
图01:系统配置界面 |
图02:系统配置界面 |
属性名称 | 代表意义 |
From | 源地址 |
To | 目的地址 |
Subject | 邮件主题 |
Priority | 邮件优先级 ( High , Low , Normal ) |
Attachments | 附件 |
Bcc | 暗送地址 |
Cc | 抄送地址 |
Body | 邮件内容主体 |
Bodyformat | 邮件格式( Html , Text ) |
Bodyencoding | 邮件编码( Base64 , Uuencode ) |
MailMessage aMessage = 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 ) aMessage.Attachments.Add ( new MailAttachment ( AttachmentTextBox.Text , MailEncoding.Base64 ) ) ; //给邮件增加一个附件 |
图03:Send.cs程序运行界面 |
using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; 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 TextBox 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 TextBox 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 ( disposing ) ; } private void 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 ; BrowseButton.Click += new System.EventHandler ( BrowseButton_Click ) ; SendButton.Location = new System.Drawing.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.Point ( 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 ( "宋体", 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 = "选择文件作为邮件的附件:" ; this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ; 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 ) ; 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 ( MessageTextBox ) ; 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 ; //定义收信人地址,如果是多人,可以用","分开 aMessage.Cc = CCTextBox.Text ; //定义抄送人地址,如果是多人,可以用","分开 aMessage.Bcc = BCCTextBox.Text ; //定义暗送人地址,如果是多人,可以用","分开 aMessage.Subject = SubjectTextBox.Text ; //定义邮件的主题 aMessage.Body = MessageTextBox.Text ; //定义邮件的内容 if ( AttachmentTextBox.Text.Length > 0 ) aMessage.Attachments.Add ( new MailAttachment ( AttachmentTextBox.Text , MailEncoding.Base64 ) ) ; //给邮件增加一个附件 SmtpMail.Send ( aMessage ) ; //发送电子邮件 MessageBox.Show( "电子邮件已经发送到->" + ToTextBox.Text ) ; } catch ( Exception ex ) { MessageBox.Show ( ex.Message.ToString ( ) ) ; } } private void ExitButton_Click ( object sender , System.EventArgs e ) { Application.Exit ( ) ; } private void BrowseButton_Click ( object sender , System.EventArgs e ) { if ( openFileDialog1.ShowDialog ( ) == DialogResult.OK ) { AttachmentTextBox.Text = openFileDialog1.FileName ; } } } |