C#發送電子郵件

用Visual C#發送電子郵件 (1)
2006-07-02 21:23:36  作者:  來源:  瀏覽次數:0  網友評論0  文字大小:【】【】【】 評分等級:0

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:系統配置界面



點中"Internet信息服務( IIS )",然後點擊"詳細信息",可得到如下界面:

 

圖02:系統配置界面



點中選擇"SMTP Serverce"選項,然後按"確定"按鈕。再按"下一步"按鈕,則系統在重新啓動後,就會安裝好運行本文程序所需要的SMTP Service了。

二.Visual C#如何發送電子郵件:

在.Net FrameWork SDK Beta 2版中,有一個叫做System.Web.Mail的名稱空間,在這個名稱空間中封裝發送電子郵件的方法、對象和屬性。Visual C#就是通過調用此名稱空間中的方法、對象和屬性,發送電子郵件的。在本文中,發送電子郵件主要用到了二個對象:一個是MailMessage對象,此對象主要是封裝電子郵件的各個屬性,即所謂的發信人,收信人,信件的主題,信件的內容和信件的附件等。另外一個是SmtpMail對象,這個對象的最大作用是把已經定義好各個屬性的MailMessage對象給發送出去,而完成此功能,就需要調用SmtpMail對象的Send ( )方法。

三.在Visual C#中正確使用發送電子郵件相關的對象: 

(1).要調用對象,當然首先就要在程序的最前面導入封裝對象的名稱空間,具體如下:

using System.Web.Mail ;

(2).正確定義MailMessage對象的屬性:

MailMessage對象中和電子郵件相關的屬性可以用下表來表示:

屬性名稱 代表意義
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 ) ) ;

//給郵件增加一個附件



注:"="右邊是程序中定義的文本框的"Text"值。 

(3).用SmtpMail對象正確發送電子郵件:

在Visual C#中調用SmtpMail對象的Send ( )方法有多種方式。本文介紹的只是其中的一種比較常用的調用方式,即: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.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 ;

}  }

}

五.總結

至此我們已經用Visual C#完成了一個功能十分完備的電子郵件發送軟件。可見,在程序中,起到關鍵作用的是.Net FrameWork SDK,正是他的豐富的類庫,使得發送電子郵件變成一件十分輕鬆的事情。其實你也只有掌握了.Net FrameWork SDK中的類庫,Visual C#才能發揮其最大的功能。

發佈了58 篇原創文章 · 獲贊 3 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章