博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
邮件操作类
阅读量:4509 次
发布时间:2019-06-08

本文共 8371 字,大约阅读时间需要 27 分钟。

///  /// 电子邮件操作类。 ///      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 List
Attachment { 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; } }

 

转载于:https://www.cnblogs.com/anbylau2130/archive/2013/01/14/2859677.html

你可能感兴趣的文章
小程序生成二维码,海报
查看>>
.NET Core 原生动态代理
查看>>
.net core 拦截器的使用
查看>>
sqlyog 下载
查看>>
动态WebAPI实现原理
查看>>
ACM-ICPC 2015 Changchun Preliminary Contest——J题
查看>>
CF1214D Treasure Island
查看>>
关于分页的一些前后台知识与应用
查看>>
Visual Studio中的快捷键
查看>>
Mac下显示和隐藏“隐藏文件”
查看>>
Chessboard POJ - 2446(最大流 || 匹配)
查看>>
Warning: Cannot modify header information原因及解决方案
查看>>
Python ConfigParser模块
查看>>
程序员的学习和积累
查看>>
.net实现支付宝在线支付
查看>>
centos7 swoole 三步搞定全部
查看>>
noip2014day1题解
查看>>
Excel:一些方法的理解
查看>>
【转】在RHEL上升级Python
查看>>
java:环境变量设置
查看>>