用Visual C#发送电子邮件(1)

来源:互联网 发布:json值是数组 编辑:程序博客网 时间:2024/06/06 05:03
导读:
  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

本文转自
http://study.qqcf.com/web/224/24027.htm
原创粉丝点击