////// 电子邮件操作类。 /// public class Mail { ////// 初始化一个电子邮件操作类的空实例。 /// public Mail() { } ////// 初始化一个电子邮件操作类的实例。 /// /// 发件人的电子邮件地址。 /// 发件人的姓名。 /// 收件人的电子邮件地址。 /// 收件人的姓名。 /// 电子邮件的主题。 /// 电子邮件的内容。 /// 电子邮件的服务器地址。 /// 电子邮件的主机端口号。 /// 登录电子邮件服务器的用户名。 /// 登录电子邮件服务器的用户密码。 /// 邮件的正文是否是HTML格式。 /// 邮件附件的文件路径。 public Mail(string from, string fromName, string recipient, string recipientName, string subject, string body, string host, int port, string username, string password, bool isBodyHtml, string filepath) { this._from = from; this._fromName = fromName; this._recipient = recipient; this._recipientName = recipientName; this._subject = subject; this._body = body; this._serverHost = host; this._serverPort = port; this._username = username; this._password = password; this._isBodyHtml = isBodyHtml; this._attachment.Add(filepath); } ////// 初始化一个电子邮件操作类的实例。 /// /// 发件人的电子邮件地址。 /// 发件人的姓名。 /// 收件人的电子邮件地址。 /// 收件人的姓名。 /// 电子邮件的主题。 /// 电子邮件的内容。 /// 电子邮件的服务器地址。 /// 电子邮件的主机端口号。 /// 登录电子邮件服务器的用户名。 /// 登录电子邮件服务器的用户密码。 /// 邮件的正文是否是HTML格式。 public Mail(string from, string fromName, string recipient, string recipientName, string subject, string body, string host, int port, string username, string password, bool isBodyHtml) : this(from, fromName, recipient, recipientName, subject, body, host, 25, username, password, isBodyHtml, null) { } ////// 初始化一个电子邮件操作类的实例。 /// /// 发件人的电子邮件地址。 /// 发件人的姓名。 /// 收件人的电子邮件地址。 /// 收件人的姓名。 /// 电子邮件的主题。 /// 电子邮件的内容。 /// 电子邮件的服务器地址。 /// 电子邮件的主机端口号。 /// 登录电子邮件服务器的用户名。 /// 登录电子邮件服务器的用户密码。 /// 邮件的正文是否是HTML格式。 public Mail(string from, string fromName, string recipient, string recipientName, string subject, string body, string host, string username, string password) : this(from, fromName, recipient, recipientName, subject, body, host, 25, username, password, false, null) { } ////// 初始化一个电子邮件操作类的实例(邮件的正文非HTML格式,端口默认25)。 /// /// 发件人的电子邮件地址。 /// 收件人的电子邮件地址。 /// 电子邮件的主题。 /// 电子邮件的内容。 /// 电子邮件的服务器地址。 /// 登录电子邮件服务器的用户名。 /// 登录电子邮件服务器的用户密码。 /// 邮件的正文是否是HTML格式。 public Mail(string from, string recipient, string subject, string body, string host, string username, string password) : this(from, null, recipient, null, subject, body, host, 25, username, password, false, null) { } ////// 初始化一个电子邮件操作类的实例(邮件的正文非HTML格式,端口默认25)。 /// /// 发件人的电子邮件地址。 /// 收件人的电子邮件地址。 /// 电子邮件的主题。 /// 电子邮件的内容。 /// 电子邮件的主机端口号。 /// 电子邮件的服务器地址。 /// 登录电子邮件服务器的用户名。 /// 登录电子邮件服务器的用户密码。 public Mail(string from, string recipient, string subject, string body, string host, int port, string username, string password) : this(from, null, recipient, null, subject, body, host, port, username, password, false, null) { } private string _subject; private string _body; private string _from; private string _fromName; private string _recipientName; private string _serverHost; private int _serverPort; private string _username; private string _password; private bool _isBodyHtml; private string _recipient; private List_attachment = new List { }; /// /// 获取或设置邮件的主题。 /// public string Subject { get { return this._subject; } set { this._subject = value; } } ////// 获取或设置邮件的正文内容。 /// public string Body { get { return this._body; } set { this._body = value; } } ////// 获取或设置发件人的邮件地址。 /// public string From { get { return this._from; } set { this._from = value; } } ////// 获取或设置发件人的姓名。 /// public string FromName { get { return this._fromName; } set { this._fromName = value; } } ////// 获取或设置收件人的姓名。 /// public string RecipientName { get { return this._recipientName; } set { this._recipientName = value; } } ////// 获取或设置收件人的邮件地址。 /// public string Recipient { get { return this._recipient; } set { this._recipient = value; } } ////// 获取或设置邮件服务器主机地址。 /// public string ServerHost { get { return this._serverHost; } set { this._serverHost = value; } } ////// 获取或设置邮件服务器的端口号。 /// public int ServerPort { set { this._serverPort = value; } get { return this._serverPort; } } ////// 获取或设置SMTP认证时使用的用户名。 /// public string Username { get { return this._username; } set { if (value.Trim() != "") { this._username = value.Trim(); } else { this._username = ""; } } } ////// 获取或设置SMTP认证时使用的密码。 /// public string Password { set { this._password = value; } get { return this._password; } } ////// 获取或设置指示邮件正文是否为 Html 格式的值。 /// public bool IsBodyHtml { get { return this._isBodyHtml; } set { this._isBodyHtml = value; } } ////// 获取电子邮件附件。 /// public ListAttachment { get { return this._attachment; } set { this._attachment = value; } } ///// ///// 添加一个收件人的邮箱地址。 ///// ///// 联系人列表。 ///////public bool AddRecipient(params string[] mailList) //{ // this.Recipient = mailList[0].Trim(); // return true; //} /// /// 添加电子邮件附件。 /// /// 文件列表。 ///是否添加成功。 public bool AddAttachment(params string[] fileList) { if (fileList.Length > 0) { foreach (string file in fileList) { if (file != null) { this._attachment.Add(file); } else { return false; } } return true; } else { return false; } } ////// 发送电子邮件。 /// ///是否发送成功。 public bool Send() { //初始化 MailMessage 对象。 MailMessage mail = new MailMessage(); Encoding encoding = Encoding.GetEncoding("utf-8"); mail.From = new MailAddress(this.From, this.FromName, encoding); mail.To.Add(new MailAddress(this.Recipient, this.RecipientName)); mail.Subject = this.Subject; mail.IsBodyHtml = this.IsBodyHtml; mail.Body = this.Body; mail.Priority = MailPriority.Normal; mail.BodyEncoding = encoding; if (this.Attachment.Count > 0) { foreach (string file in this.Attachment) { mail.Attachments.Add(new Attachment(file)); } } //初始化 SmtpClient 对象。 SmtpClient smtp = new SmtpClient(); smtp.Host = this.ServerHost; smtp.Port = this.ServerPort; smtp.Credentials = new NetworkCredential(this.Username, this.Password); if (smtp.Port != 25) { smtp.EnableSsl = true; } try { smtp.Send(mail); } catch (SmtpException ex) { string message = ex.Message; return false; } return true; } }