正版软件交易论坛

首页 » 技术咨询区 » C#技术专区 » C#发送电子邮件代码
水兰色心情 - 2007-8-21 8:42:00
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Web.Mail;
namespace email
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Panel panel1;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.RichTextBox richTextBox1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;
  public Form1()
  {
  //
  // Windows 窗体设计器支持所必需的
  //
  InitializeComponent();
  //
  // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  //
  }
  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
  if( disposing )
  {
    if (components != null)
    {
    components.Dispose();
    }
  }
  base.Dispose( disposing );
  }
  #region Windows Form Designer generated code
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
  this.button1 = new System.Windows.Forms.Button();
  this.panel1 = new System.Windows.Forms.Panel();
  this.textBox2 = new System.Windows.Forms.TextBox();
  this.label2 = new System.Windows.Forms.Label();
  this.label1 = new System.Windows.Forms.Label();
  this.textBox1 = new System.Windows.Forms.TextBox();
  this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  this.panel1.SuspendLayout();
  this.SuspendLayout();
  //
  // button1
  //
  this.button1.Location = new System.Drawing.Point(565, 6);
  this.button1.Name = "button1";
  this.button1.TabIndex = 2;
  this.button1.Text = "发送邮件";
  this.button1.Click += new System.EventHandler(this.button1_Click);
  //
  // panel1
  //
  this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
                    this.textBox2,
                    this.label2,
                    this.label1,
                    this.textBox1,
                    this.button1});
  this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
  this.panel1.Name = "panel1";
  this.panel1.Size = new System.Drawing.Size(648, 32);
  this.panel1.TabIndex = 0;
  //
  // textBox2
  //
  this.textBox2.Location = new System.Drawing.Point(304, 3);
  this.textBox2.Name = "textBox2";
  this.textBox2.Size = new System.Drawing.Size(189, 21);
  this.textBox2.TabIndex = 1;
  this.textBox2.Text = "";
  //
  // label2
  //
  this.label2.AutoSize = true;
  this.label2.Location = new System.Drawing.Point(256, 8);
  this.label2.Name = "label2";
  this.label2.Size = new System.Drawing.Size(42, 14);
  this.label2.TabIndex = 4;
  this.label2.Text = "抄送:";
  //
  // label1
  //
  this.label1.AutoSize = true;
  this.label1.Location = new System.Drawing.Point(4, 9);
  this.label1.Name = "label1";
  this.label1.Size = new System.Drawing.Size(54, 14);
  this.label1.TabIndex = 3;
  this.label1.Text = "收信人:";
  //
  // textBox1
  //
  this.textBox1.Location = new System.Drawing.Point(59, 4);
  this.textBox1.Name = "textBox1";
  this.textBox1.Size = new System.Drawing.Size(189, 21);
  this.textBox1.TabIndex = 0;
  this.textBox1.Text = "";
  //
  // richTextBox1
  //
  this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
  this.richTextBox1.Location = new System.Drawing.Point(0, 32);
  this.richTextBox1.Name = "richTextBox1";
  this.richTextBox1.Size = new System.Drawing.Size(648, 342);
  this.richTextBox1.TabIndex = 5;
  this.richTextBox1.Text = "";
  //
  // Form1
  //
  this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  this.ClientSize = new System.Drawing.Size(648, 374);
  this.Controls.AddRange(new System.Windows.Forms.Control[] {
                    this.richTextBox1,
                    this.panel1});
  this.MaximizeBox = false;
  this.Name = "Form1";
  this.Text = "1";
  this.Load += new System.EventHandler(this.Form1_Load);
  this.panel1.ResumeLayout(false);
  this.ResumeLayout(false);
  }
  #endregion
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
  Application.Run(new Form1());
  }
  private void richTextBox1_TextChanged(object sender, System.EventArgs e)
  {
 
  }
  private void button1_Click(object sender, System.EventArgs e)
  {
  if(textBox1.Text ==""){
    MessageBox.Show("请填写收件人地址!","提示信息");
    textBox1.Focus();
    return;
  }
  MailMessage message = new MailMessage();
  message.From = "xxx@xxx..com"; ////
  message.To = textBox1.Text;
  message.Cc = textBox2.Text;
  message.Subject = "这是测试";
  message.Body = richTextBox1.Text;
  message.BodyFormat=MailFormat.Text;
  message.Priority = MailPriority.Normal;
  SmtpMail.SmtpServer = "192.168.0.111";
  SmtpMail.Send(message);
  }
  private void Form1_Load(object sender, System.EventArgs e)
  {
 
  }
}
}
1
查看完整版本: C#发送电子邮件代码