利用.net发送电子邮件
作者:网际浪子
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Web.Mail;
namespace 利用.Net类库发送电子邮件
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox5;
/// <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.button2 = new System.Windows.Forms.Button();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(200, 114);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(66, 28);
this.button2.TabIndex = 19;
this.button2.Text = "添加附件";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox4
//
this.textBox4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox4.Location = new System.Drawing.Point(72, 116);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(122, 21);
this.textBox4.TabIndex = 17;
this.textBox4.Text = "";
//
// label4
//
this.label4.Font = new System.Drawing.Font("宋体", 9F);
this.label4.Location = new System.Drawing.Point(18, 116);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(62, 18);
this.label4.TabIndex = 20;
this.label4.Text = "附 件:";
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(106, 286);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(92, 27);
this.button1.TabIndex = 21;
this.button1.Text = "发送邮件";
this.button1.Click += new System.EventHandler(this.SendButton_Click);
//
// label3
//
this.label3.Font = new System.Drawing.Font("宋体", 9F);
this.label3.Location = new System.Drawing.Point(18, 84);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(62, 18);
this.label3.TabIndex = 22;
this.label3.Text = "主 题:";
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋体", 9F);
this.label2.Location = new System.Drawing.Point(18, 52);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(62, 18);
this.label2.TabIndex = 23;
this.label2.Text = "收信人:";
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 9F);
this.label1.Location = new System.Drawing.Point(18, 22);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(62, 18);
this.label1.TabIndex = 24;
this.label1.Text = "发信人:";
//
// textBox3
//
this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox3.Location = new System.Drawing.Point(72, 82);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(174, 21);
this.textBox3.TabIndex = 16;
this.textBox3.Text = "";
//
// textBox2
//
this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox2.Location = new System.Drawing.Point(72, 48);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(174, 21);
this.textBox2.TabIndex = 15;
this.textBox2.Text = "";
//
// textBox1
//
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox1.Location = new System.Drawing.Point(72, 14);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(174, 21);
this.textBox1.TabIndex = 14;
this.textBox1.Text = "";
//
// textBox5
//
this.textBox5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox5.Location = new System.Drawing.Point(10, 154);
this.textBox5.Multiline = true;
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(276, 108);
this.textBox5.TabIndex = 18;
this.textBox5.Text = "";
//
// openFileDialog1
//
this.openFileDialog1.Title = "选择附件:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(300, 335);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.textBox4,
this.button1,
this.textBox3,
this.textBox2,
this.textBox1,
this.textBox5,
this.label4,
this.label3,
this.label2,
this.label1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "利用.Net类库发送电子邮件";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void SendButton_Click(object sender, System.EventArgs e)
{
try
{
MailMessage Message = new MailMessage();
//新建一个MailMessage对象
Message.From = textBox1.Text;
//定义发信人地址
Message.To = textBox2.Text;
//定义收信人地址,如果是多人,可以用“,”分开
Message.Subject = textBox3.Text;
//定义邮件的主题
Message.Body = textBox4.Text;
//定义邮件的内容
if (openFileDialog1.FileName != "")
{
MailAttachment Attachment = new MailAttachment(openFileDialog1.FileName, MailEncoding.Base64);
//以Base64编码创建一个附件实例
Message.Attachments.Add(Attachment);
//给邮件增加附件
}
//判断要发送的邮件是否有附件,如果有则加入
SmtpMail.Send(Message);
//发送电子邮件
MessageBox.Show("电子邮件已经发送到->" + textBox2.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void button2_Click(object sender, System.EventArgs e)
{
openFileDialog1.ShowDialog();
//显示附件选择对话框
textBox4.Text = openFileDialog1.FileName;
}
}
}