代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
发送邮件小程序(支持群发,附件)
作者:
dezai
/ 发布于
2014/5/21
/
506
在这里,主要使用了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,原来是要某个链接激活打开的。具体看代码。不过在实现多个附件的话,处理比较复杂,我就没用了。
试试其它关键字
发送邮件
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3