百度空间 | 百度首页 
 
查看文章
 
C#发送邮件小程序(支持群发,附件)
2009年07月10日 星期五 23:10


在这里,主要使用了system.web.mail命名空间下的类,具体代码如下:

先新建win项目,再复制:

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;
using System.Text;
using System.Text.RegularExpressions;

namespace SendMail
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Label fromlabel;
private System.Windows.Forms.Label subjectlabel;
private System.Windows.Forms.Label tolabel;
private System.Windows.Forms.Button sendbtn;
private System.Windows.Forms.TextBox frombox;
private System.Windows.Forms.TextBox subjectbox;
private System.Windows.Forms.TextBox tobox;
private System.Windows.Forms.Button exitbtn;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.LinkLabel linkLabel1;
private System.Windows.Forms.TextBox Attachmentbox;
/// <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 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
   this.frombox = new System.Windows.Forms.TextBox();
   this.fromlabel = new System.Windows.Forms.Label();
   this.subjectlabel = new System.Windows.Forms.Label();
   this.tolabel = new System.Windows.Forms.Label();
   this.subjectbox = new System.Windows.Forms.TextBox();
   this.richTextBox1 = new System.Windows.Forms.RichTextBox();
   this.sendbtn = new System.Windows.Forms.Button();
   this.tobox = new System.Windows.Forms.TextBox();
   this.exitbtn = new System.Windows.Forms.Button();
   this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
   this.linkLabel1 = new System.Windows.Forms.LinkLabel();
   this.Attachmentbox = new System.Windows.Forms.TextBox();
   this.SuspendLayout();
   //
   // frombox
   //
   this.frombox.Location = new System.Drawing.Point(120, 32);
   this.frombox.Name = "frombox";
   this.frombox.Size = new System.Drawing.Size(152, 21);
   this.frombox.TabIndex = 0;
   this.frombox.Text = "";
   //
   // fromlabel
   //
   this.fromlabel.Location = new System.Drawing.Point(16, 32);
   this.fromlabel.Name = "fromlabel";
   this.fromlabel.Size = new System.Drawing.Size(72, 23);
   this.fromlabel.TabIndex = 1;
   this.fromlabel.Text = "from";
   //
   // subjectlabel
   //
   this.subjectlabel.Location = new System.Drawing.Point(16, 72);
   this.subjectlabel.Name = "subjectlabel";
   this.subjectlabel.Size = new System.Drawing.Size(64, 23);
   this.subjectlabel.TabIndex = 3;
   this.subjectlabel.Text = "subject";
   //
   // tolabel
   //
   this.tolabel.Location = new System.Drawing.Point(336, 32);
   this.tolabel.Name = "tolabel";
   this.tolabel.Size = new System.Drawing.Size(48, 23);
   this.tolabel.TabIndex = 4;
   this.tolabel.Text = "to";
   //
   // subjectbox
   //
   this.subjectbox.Location = new System.Drawing.Point(120, 72);
   this.subjectbox.Name = "subjectbox";
   this.subjectbox.Size = new System.Drawing.Size(408, 21);
   this.subjectbox.TabIndex = 2;
   this.subjectbox.Text = "";
   //
   // richTextBox1
   //
   this.richTextBox1.Location = new System.Drawing.Point(40, 160);
   this.richTextBox1.Name = "richTextBox1";
   this.richTextBox1.Size = new System.Drawing.Size(648, 248);
   this.richTextBox1.TabIndex = 3;
   this.richTextBox1.Text = "";
   //
   // sendbtn
   //
   this.sendbtn.Location = new System.Drawing.Point(488, 120);
   this.sendbtn.Name = "sendbtn";
   this.sendbtn.TabIndex = 4;
   this.sendbtn.Text = "send";
   this.sendbtn.Click += new System.EventHandler(this.sendbtn_Click);
   //
   // tobox
   //
   this.tobox.Location = new System.Drawing.Point(392, 32);
   this.tobox.Name = "tobox";
   this.tobox.Size = new System.Drawing.Size(296, 21);
   this.tobox.TabIndex = 1;
   this.tobox.Text = "";
   //
   // exitbtn
   //
   this.exitbtn.Location = new System.Drawing.Point(600, 120);
   this.exitbtn.Name = "exitbtn";
   this.exitbtn.TabIndex = 5;
   this.exitbtn.Text = "exit";
   this.exitbtn.Click += new System.EventHandler(this.exitbtn_Click);
   //
   // openFileDialog1
   //
   this.openFileDialog1.Multiselect = true;
   //
   // linkLabel1
   //
   this.linkLabel1.Location = new System.Drawing.Point(8, 104);
   this.linkLabel1.Name = "linkLabel1";
   this.linkLabel1.Size = new System.Drawing.Size(104, 16);
   this.linkLabel1.TabIndex = 6;
   this.linkLabel1.TabStop = true;
   this.linkLabel1.Text = "Add Attachment";
   this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
   //
   // Attachmentbox
   //
   this.Attachmentbox.Location = new System.Drawing.Point(120, 104);
   this.Attachmentbox.Name = "Attachmentbox";
   this.Attachmentbox.Size = new System.Drawing.Size(264, 21);
   this.Attachmentbox.TabIndex = 7;
   this.Attachmentbox.Text = "";
   this.Attachmentbox.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(728, 454);
   this.Controls.Add(this.Attachmentbox);
   this.Controls.Add(this.linkLabel1);
   this.Controls.Add(this.exitbtn);
   this.Controls.Add(this.tobox);
   this.Controls.Add(this.sendbtn);
   this.Controls.Add(this.richTextBox1);
   this.Controls.Add(this.subjectbox);
   this.Controls.Add(this.tolabel);
   this.Controls.Add(this.subjectlabel);
   this.Controls.Add(this.fromlabel);
   this.Controls.Add(this.frombox);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
   Application.Run(new Form1());
}
// validate mail expresion
private bool checkEmail( string dataToCheck )
{

   string reguEmail = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
   return Regex.IsMatch( dataToCheck, reguEmail, RegexOptions.IgnoreCase );
}


private void sendbtn_Click(object sender, System.EventArgs e)
{
   //convert to array
   string[]maillist = tobox.Text.Split(',');
   //trim string in maillist
   for(int i=0;i<maillist.Length;i++)
   maillist[i] = maillist[i].Trim();
   string Attachmentpath = openFileDialog1.FileName;
   //send all mails
   foreach(string tomail in maillist)
    SendMail(frombox.Text, tomail,Attachmentpath);
  
}

void SendMail(string frommail,string tomail,string Attachmentpath)
{
   //whether mail expresion is valid
   if(checkEmail(frommail)&&checkEmail(tomail))
   {
    try
    {
     //initialize Mailmessage object
     MailMessage message = new MailMessage();
     message.Subject = subjectbox.Text;
     message.From = frommail;
     message.To = tomail;
     message.Body = richTextBox1.Text;
     //real send method
     message.Attachments.Add(new MailAttachment(Attachmentpath,MailEncoding.Base64));
     SmtpMail.Send ( message ) ;

     //show mail succeeded
      MessageBox.Show( "Email has sent to ->" + tobox.Text ) ;
    }
    catch(Exception er)
    {
     MessageBox.Show("error:"+er.Message);
    }
   }
   else
    MessageBox.Show("error:wrong email expresion!");
}
private void exitbtn_Click(object sender, System.EventArgs e)
{
   Application.Exit();
}

private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
   openFileDialog1.ShowDialog();
   Attachmentbox.Text = openFileDialog1.FileName;
}

private void textBox1_TextChanged(object sender, System.EventArgs e)
{

}
}
}


关键点:

1 首先使用验证输入的验证邮件格式是否正确,使用了正则表达式,还可以使用errorprovidor,不过我没用过。

2 使用MailMessage类,初始化它的几个属性,非常简单。具体详见msdn

3 使用MailMessage.Attachment.add()方法添加附件

4 今天第一次使用openfileDialogue,原来是要某个链接激活打开的。具体看代码。不过在实现多个附件的话,处理比较复杂,我就没用了。


类别:默认分类 | 浏览() | 评论 (0)
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu