- using System;
- using System.Net;
- using System.Collections.Generic;
- using System.Text;
- namespace NET.Utility
- {
- public class Mail
- {
- //邮件发送客户端
- private System.Net.Mail.SmtpClient client;
- //邮件发送的消息
- private System.Net.Mail.MailMessage _message;
- #region 构造函数
- /// <summary>
- /// 构造自定义参数的Mail客户端
- /// </summary>
- /// <param name="host">Mail客户端Host</param>
- /// <param name="port">Mail客户端端口</param>
- public Mail(string host, int port)
- {
- this.client = new System.Net.Mail.SmtpClient(host, port);
- this._message = new System.Net.Mail.MailMessage();
- }
- /// <summary>
- /// 构造host和port默认为smtp.qq.com和25的Mail客户端
- /// </summary>
- public Mail()
- : this("smtp.qq.com", 25)
- { }
- #endregion
- #region 邮件部件,属性
- private string _from;
- /// <summary>
- /// 获取或设置邮件发送人地址
- /// </summary>
- public string From
- {
- get { return this._from; }
- set
- {
- this._from = value;
- this._message.From = new System.Net.Mail.MailAddress(value);
- this._message.Sender = new System.Net.Mail.MailAddress(value);
- }
- }
- private string _passWord;
- /// <summary>
- /// 获取或设置邮件发送人密码
- /// </summary>
- public string PassWord
- {
- get { return _passWord; }
- set { _passWord = value; }
- }
- private List<string> _attachment;
- /// <summary>
- /// 获取或设置邮件附件地址列表
- /// </summary>
- public List<string> Attachment
- {
- get
- {
- return _attachment;
- }
- set
- {
- this._attachment = value;
- StringBuilder sb = new StringBuilder();
- foreach (string localPath in value)
- {
- if (System.IO.File.Exists(localPath))
- {
- this._message.Attachments.Add(new System.Net.Mail.Attachment(localPath));
- }
- else
- {
- sb.Append(localPath + "\r\n");
- }
- }
- if (sb.Length != 0)
- System.Windows.Forms.MessageBox.Show(sb.ToString() + "给定地址附件不存在,请检查");
- }
- }
- private List<string> _bcc;
- /// <summary>
- /// 获取或设置邮件密送人地址列表
- /// </summary>
- public List<string> BCC
- {
- get { return _bcc; }
- set
- {
- this._bcc = value;
- StringBuilder sb = new StringBuilder();
- foreach (string bcc in value)
- {
- if (InvalidMail.isEmail(bcc))
- {
- this._message.Bcc.Add(bcc);
- }
- else
- {
- sb.Append(bcc + "\r\n");
- }
- }
- if (sb.Length != 0)
- System.Windows.Forms.MessageBox.Show(sb.ToString() + "邮件地址格式不正确,请检查");
- }
- }
- private List<string> _cc;
- /// <summary>
- /// 获取或设置邮件抄送人地址列表
- /// </summary>
- public List<string> CC
- {
- get { return _cc; }
- set
- {
- _cc = value;
- StringBuilder sb = new StringBuilder();
- foreach (string cc in value)
- {
- if (InvalidMail.isEmail(cc))
- {
- this._message.CC.Add(cc);
- }
- else
- {
- sb.Append(cc + "\r\n");
- }
- }
- if (sb.Length != 0)
- System.Windows.Forms.MessageBox.Show(sb.ToString() + "邮件地址格式不正确,请检查");
- }
- }
- /// <summary>
- /// 获取或设置邮件正文
- /// </summary>
- public string Body
- {
- get { return this._message.Body; }
- set { this._message.Body = value; }
- }
- /// <summary>
- /// 获取或设置邮件正文是否Html格式
- /// </summary>
- public bool IsHtml
- {
- get { return this._message.IsBodyHtml; }
- set { this._message.IsBodyHtml = value; }
- }
- /// <summary>
- /// 获取或设置邮件正文的编码格式
- /// </summary>
- public Encoding BodyEncoding
- {
- get { return this._message.BodyEncoding; }
- set { this._message.BodyEncoding = value; }
- }
- /// <summary>
- /// 获取或设置获取或设置邮件主题
- /// </summary>
- public string Subject
- {
- get { return this._message.Subject; }
- set { this._message.Subject = value; }
- }
- /// <summary>
- /// 获取或设置邮件主题编码
- /// </summary>
- public Encoding SubjectEncoding
- {
- get { return this._message.SubjectEncoding; }
- set { this._message.SubjectEncoding = value; }
- }
- private List<string> _to;
- /// <summary>
- /// 获取或设置收件人列表
- /// </summary>
- public List<string> To
- {
- get
- {
- return _to;
- }
- set
- {
- _to = value;
- StringBuilder sb = new StringBuilder();
- foreach (string to in value)
- {
- if (InvalidMail.isEmail(to))
- {
- this._message.To.Add(to);
- }
- else
- {
- sb.Append(to + "\r\n");
- }
- }
- if (sb.Length != 0)
- System.Windows.Forms.MessageBox.Show(sb.ToString() + "邮件地址格式不正确,请检查");
- }
- }
- #endregion
- #region 发送邮件
- /// <summary>
- /// 检查邮件必要信息是否存在
- /// </summary>
- /// <returns></returns>
- private bool CheckMail()
- {
- bool flag = true;
- if (this.Body == null)
- {
- flag = false;
- System.Windows.Forms.MessageBox.Show("邮件正文不能为空");
- }
- if (this.From == null)
- {
- flag = false;
- System.Windows.Forms.MessageBox.Show("邮件发送人不能为空");
- }
- if (this.PassWord == null)
- {
- flag = false;
- System.Windows.Forms.MessageBox.Show("邮件发送人密码不能为空");
- }
- if (this.To == null)
- {
- flag = false;
- System.Windows.Forms.MessageBox.Show("邮件收件人不能为空");
- }
- if (this.Subject == null)
- {
- flag = false;
- System.Windows.Forms.MessageBox.Show("邮件主题不能为空");
- }
- return flag;
- }
- /// <summary>
- /// 同步发送邮件
- /// </summary>
- /// <returns></returns>
- public bool SendMail()
- {
- bool flag = false;
- if (CheckMail())
- {
- try
- {
- this.client.Credentials = new System.Net.NetworkCredential(_from, PassWord);
- this.client.Send(this._message);
- flag = true;
- }
- catch (System.InvalidOperationException e)
- {
- System.Windows.Forms.MessageBox.Show(e.Message);
- }
- catch (System.ArgumentNullException e)
- {
- System.Windows.Forms.MessageBox.Show(e.Message);
- }
- catch (System.ArgumentOutOfRangeException e)
- {
- System.Windows.Forms.MessageBox.Show(e.Message);
- }
- catch (System.Net.Mail.SmtpException e)
- {
- System.Windows.Forms.MessageBox.Show(e.Message);
- }
- catch (Exception e)
- {
- System.Windows.Forms.MessageBox.Show(e.Message);
- }
- }
- return flag;
- }
- /// <summary>
- /// 异步发送邮件
- /// </summary>
- public void SendMailAsync()
- {
- if (CheckMail())
- {
- try
- {
- this.client.Credentials = new System.Net.NetworkCredential(_from, PassWord);
- this.client.SendCompleted += new System.Net.Mail.SendCompletedEventHandler(client_SendCompleted);
- object flag = this._message;
- this.client.SendAsync(this._message, flag);
- }
- catch (System.InvalidOperationException e)
- {
- System.Windows.Forms.MessageBox.Show(e.Message);
- }
- catch (System.ArgumentNullException e)
- {
- System.Windows.Forms.MessageBox.Show(e.Message);
- }
- catch (System.ArgumentOutOfRangeException e)
- {
- System.Windows.Forms.MessageBox.Show(e.Message);
- }
- catch (System.Net.Mail.SmtpException e)
- {
- System.Windows.Forms.MessageBox.Show(e.Message);
- }
- catch (Exception e)
- {
- System.Windows.Forms.MessageBox.Show(e.Message);
- }
- }
- }
- /// <summary>
- /// 异步发送邮件完成时事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
- {
- System.Net.Mail.MailMessage message = (System.Net.Mail.MailMessage)e.UserState;
- if (e.Cancelled)
- {
- if (SendComplet != null)
- {
- SendComplet(this, new SendCompletEventArgs(DateTime.Now, false));
- }
- }
- else if (e.Error != null)
- {
- if (SendComplet != null)
- {
- SendComplet(this, new SendCompletEventArgs(DateTime.Now, false));
- }
- }
- else
- {
- if (SendComplet != null)
- {
- SendComplet(this, new SendCompletEventArgs(DateTime.Now, true));
- }
- }
- }
- public event SendCompletEventHandler SendComplet;
- #endregion
- }
- /// <summary>
- /// 邮件异步发送完成时发生事件的委托
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public delegate void SendCompletEventHandler(object sender,EventArgs e);
- /// <summary>
- /// 自定义参数
- /// </summary>
- public class SendCompletEventArgs : EventArgs
- {
- public readonly DateTime dateTime;
- public readonly bool success;
- public SendCompletEventArgs(DateTime dateTime, bool success)
- {
- this.dateTime = dateTime;
- this.success = success;
- }
- }
- /// <summary>
- /// 静态验证类
- /// </summary>
- public static class InvalidMail
- {
- /// <summary>
- /// 判断用户输入的字符串是否符合邮件地址格式
- /// </summary>
- /// <param name="email"></param>
- /// <returns></returns>
- public static bool isEmail(string email)
- {
- string valid = @"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+{1}quot;;
- return System.Text.RegularExpressions.Regex.IsMatch(email, valid);
- }
- }
- }
客户端调用邮件发送
- /// <summary>
- /// 发送邮件
- /// </summary>
- private void SendMail()
- {
- Mail mail = new Mail();
- //设置邮件附件
- //非必要
- List<string> attachment=new List<string> ();
- OpenFileDialog ofd = new OpenFileDialog();
- if (ofd.ShowDialog() == DialogResult.OK)
- {
- foreach (string path in ofd.FileNames)
- {
- attachment.Add(path);
- }
- }
- mail.Attachment = attachment;
- //设置邮件密送人列表
- //非必要
- List<string> bcc = new List<string>();
- bcc.Add("xxxx@qq.com");
- bcc.Add("yyyy@qq.com");
- mail.BCC = bcc;
- //设置抄送人列表
- //非必要
- List<string> cc = new List<string>();
- cc.Add("aaaa.qq.com");
- cc.Add("bbbb.qq.com");
- mail.CC = cc;
- //设置邮件正文
- //非必要
- mail.Body = "Microsoft Visual Studio<br/>C#.Net";
- //设置邮件正文编码格式
- //非必要
- mail.BodyEncoding = Encoding.UTF8;
- //设置邮件发送人邮箱地址
- //必要
- mail.From = "userName@qq.com";
- //设置邮件发送人邮箱密码
- //非必要
- mail.PassWord = "passWord";
- //邮件正文是否以html格式在邮件中显示
- //如果为false,则收件人收到的邮件正文为:Microsoft Visual Studio<br/>C#.Net
- //如果为true,则收件人收到的邮件正文如下:
- //Microsoft Visual Studio
- //C#.Net
- //非必要
- mail.IsHtml = true;
- //设置邮件标题
- //非必要
- mail.Subject = "try";
- //设置邮件收件人
- //必要
- List<string> to = new List<string>();
- to.Add("qqqq@qq.com");
- to.Add("wwww@qq.com");
- to.Add("rrrr@qq.com");
- mail.To = to;
- //发送邮件,使用该方法在发送邮件时,线程会阻断,直到邮件发送完成
- if (mail.SendMail())
- {
- Console.WriteLine("do something");
- }
- //增加异步发送邮件事件
- mail.SendComplet+=new SendCompletEventHandler(mail_SendComplet);
- //异步发送邮件,使用该方法发送邮件时,线程不会被阻断
- //邮件发送完成后,会引发SendComplet事件
- mail.SendMailAsync();
- }
- /// <summary>
- /// 异步邮件发送完成后
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void mail_SendComplet(object sender, SendCompletEventArgs e)
- {
- Console.WriteLine("do something");
- }