• Email工具类


    /// <summary>
        /// Email工具类
        /// </summary>
        public class MailUtils
        {
            #region Fields
            private string _userName;
            private string _userPassword;
            private string _host;
            private int _port;
            private SmtpClient smtp = null;
            #endregion

            #region Constructors
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="host">服务器地址(IP或域名)</param>
            /// <param name="userName">认证用户名</param>
            /// <param name="userPassword">认证密码</param>
            public MailUtils(string host, string userName, string userPassword)
            {
                smtp = new SmtpClient(host);
                Initialize(userName, userPassword);
            }

            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="host">服务器地址(IP或域名)</param>
            /// <param name="port">服务侦听端口</param>
            /// <param name="userName">认证用户名</param>
            /// <param name="userPassword">认证密码</param>
            public MailUtils(string host, int port, string userName, string userPassword)
            {
                smtp = new SmtpClient(host, port);
                Initialize(userName, userPassword);
            }
            #endregion

            #region Properties
            /// <summary>
            /// 认证用户名
            /// </summary>
            public string UserName
            {
                get { return _userName; }
                set { _userName = value; }
            }

            /// <summary>
            /// 认证密码
            /// </summary>
            public string UserPassword
            {
                get { return _userPassword; }
                set { _userPassword = value; }
            }

            /// <summary>
            /// 服务器地址(IP或域名)
            /// </summary>
            public string Host
            {
                get { return _host; }
                set { _host = value; }
            }

            /// <summary>
            /// 服务侦听端口
            /// </summary>
            public int Port
            {
                get { return _port; }
                set { _port = value; }
            }
            #endregion

            #region Instance Methods
            /// <summary>
            /// 初始化SMTP对象
            /// </summary>
            /// <param name="userName">认证用户名</param>
            /// <param name="userPassword">认证密码</param>
            public void Initialize(string userName, string userPassword)
            {
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential(userName, userPassword);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                UserName = userName;
                UserPassword = userPassword;
            }

            /// <summary>
            /// 发送邮件
            /// </summary>
            /// <param name="strReceiver">收件人地址</param>
            /// <param name="strTitle">邮件标题</param>
            /// <param name="strContent">邮件内容</param>
            /// <returns></returns>
            public bool Send(string strReceiver, string strTitle, string strContent)
            {
                return Send(strReceiver, strTitle, strContent, null);
            }

            /// <summary>
            /// 发送邮件(可带附件)
            /// </summary>
            /// <param name="strReceiver">收件人地址</param>
            /// <param name="strSubject">邮件主题</param>
            /// <param name="strContent">邮件内容</param>
            /// <param name="strAttachmentPath">附件的物理地址</param>
            /// <returns></returns>
            public bool Send(string strReceiver, string strSubject, string strContent, string strAttachmentPath)
            {
                try
                {
                    MailMessage message = new MailMessage(UserName, strReceiver, strSubject, strContent);
                    message.BodyEncoding = System.Text.Encoding.UTF8;
                    if (strAttachmentPath != null && File.Exists(strAttachmentPath))
                    {
                        message.Attachments.Add(new Attachment(strAttachmentPath));
                    }
                    message.IsBodyHtml = true;
                    smtp.Send(message);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            #endregion

            #region Static Methods
            /// <summary>
            /// 发送邮件
            /// </summary>
            /// <param name="server">stmp服务器地址</param>
            /// <param name="user">用户名,包括@和域名的,这个要注意</param>
            /// <param name="pwd">密码</param>
            /// <param name="receiver">收件人地址</param>
            /// <param name="title">邮件标题</param>
            /// <param name="content">邮件内容</param>
            /// <returns></returns>
            public static bool Send(string server, string user, string pwd, string receiver, string title, string content)
            {
                return Send(server, user, pwd, receiver, title, content, null);
            }

            /// <summary>
            /// 发送邮件
            /// </summary>
            /// <param name="server">stmp服务器地址</param>
            /// <param name="user">用户名,包括@和域名的,这个要注意</param>
            /// <param name="pwd">密码</param>
            /// <param name="receiver">收件人地址</param>
            /// <param name="title">邮件标题</param>
            /// <param name="content">邮件内容</param>
            /// <param name="attachmentPath">附件的绝对路径</param>
            ///  <returns></returns>
            public static bool Send(string server, string user, string pwd, string receiver, string title, string content, string attachmentPath)
            {
                try
                {
                    SmtpClient smtp = new SmtpClient(server);
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new System.Net.NetworkCredential(user, pwd);
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    MailMessage message = new MailMessage(user, receiver, title, content);
                    message.BodyEncoding = System.Text.Encoding.UTF8;
                    if (attachmentPath != null && System.IO.File.Exists(attachmentPath))
                    {
                        message.Attachments.Add(new Attachment(attachmentPath));
                    }
                    message.IsBodyHtml = true;
                    smtp.Send(message);
                    return true;

                }
                catch
                {
                    return false;
                }         
            }
            #endregion
        }

  • 相关阅读:
    Mysql存储过程详解
    自动化测试——人人都可自制“呼死你”
    Apktool(1)——Apktool的安装
    Apktool(2)——使用前必须知道的apk知识
    写博是种心情
    webpack使用tree shaking的问题。及关于UglifyJs不支持ES6的解决方案。
    angular2 笔记
    angular2 content projection
    angular2aotwebpack 生产环境下编译angular2
    ionic2配置问题集
  • 原文地址:https://www.cnblogs.com/xsmhero/p/1749697.html
Copyright © 2020-2023  润新知