메 일 보 내기 클래스 ASP. NET C \ #

8588 단어 asp.net

소 B 의 디자인 모델 이 없고 코드 가 세련 되 고 실 용적 이 며 기능 이 완비 되 어 호출 이 간단 합 니 다.다 생각 해 줘. 
 
  MailSmtp ms = new MailSmtp("smtp.qq.com","1215247044","xxxx");

            //    
            ms.SetCC("[email protected]");//      
            ms.SetBC("[email protected]");//      
            ms.SetIsHtml(true);//  :true
            ms.SetEncoding(System.Text.Encoding.UTF8);//       utf-8
            ms.SetIsSSL(true);//  ssl      false

            //    
            bool isSuccess = ms.Send("[email protected]", "test", "[email protected]", "  ", "  ", Server.MapPath("~/Test.dll"));

            //    
            Response.Write(ms.Result);

  
 
코드:
using System;
using System.IO;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Linq;
using System.Text.RegularExpressions;

namespace SyntacticSugar
{
    /// <summary>
    /// **   :    
    /// **     :2015-6-8
    /// **     :-
    /// **   :sunkaixuan
    /// **     :http://www.cnblogs.com/sunkaixuan/p/4562147.html
    /// </summary>
    public class MailSmtp
    {
        /// <summary>
        ///         
        /// </summary>
        /// <param name="contentEncoding"></param>
        public void SetEncoding(Encoding contentEncoding)
        {
            this._encoding = contentEncoding;

        }
        /// <summary>
        ///          Html    
        /// </summary>
        /// <param name="isHtml"></param>
        public void SetIsHtml(bool isHtml)
        {
            this._isHtml = isHtml;
        }
        /// <summary>
        ///   
        /// </summary>
        /// <param name="cc"></param>
        public void SetCC(params string[] cc)
        {
            this._cc = cc;
        }
        /// <summary>
        ///   
        /// </summary>
        /// <param name="cc"></param>
        public void SetBC(params string[] bc)
        {
            this._bcc = bc;
        }
        /// <summary>
        ///   ssl  
        /// </summary>
        /// <param name="isSSL"></param>
        public void SetIsSSL(bool isSSL)
        {
            this._smtp.EnableSsl = isSSL;
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="host"></param>
        /// <param name="username">    </param>
        /// <param name="password">  </param>
        public MailSmtp(string host, string username, string password)
        {
            this._smtp.Host = host;
            this._smtp.Port = 0x19;
            this._smtp.EnableSsl = false;
            this._isHtml = true;
            this._encoding = Encoding.UTF8;
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                this._smtp.UseDefaultCredentials = false;
            }
            else
            {
                this._smtp.Credentials = new NetworkCredential(username, password);
            }
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="from">       </param>
        /// <param name="sender">       </param>
        /// <param name="to">     </param>
        /// <param name="subject">    </param>
        /// <param name="body">    </param>
        /// <param name="file">      </param>
        /// <returns>bool      </returns>
        public bool Send(string from, string sender, string to, string subject, string body, params string[] file)
        {
            return Send(from, sender, new string[] { to }, subject, body, file);
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="from">       </param>
        /// <param name="sender">       </param>
        /// <param name="to">     </param>
        /// <param name="subject">    </param>
        /// <param name="body">    </param>
        /// <param name="file">      </param>
        /// <returns>bool      </returns>
        public bool Send(string from, string sender, string[] to, string subject, string body, params string[] file)
        {
            string mailReg = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
            if (to == null)
            {
                throw new ArgumentNullException("MailSmtp.Send.to");
            }

            if (to.Any(oit => !Regex.IsMatch(oit + "", mailReg)))
            {
                this.Result = "        ";
                return false;
            }
            if (_bcc != null && _bcc.Length > 0)
            {
                if (_bcc.Any(oit => !Regex.IsMatch(oit + "", mailReg)))
                {
                    this.Result = "        ";
                    return false;
                }
            }
            if (_cc != null && _cc.Length > 0)
            {
                if (_cc.Any(oit => !Regex.IsMatch(oit + "", mailReg)))
                {
                    this.Result = "        ";
                    return false;
                }
            }
            MailMessage message = new MailMessage();

            //         
            foreach (var r in file)
            {
                Attachment objMailAttachment;
                objMailAttachment = new Attachment(r);//       
                message.Attachments.Add(objMailAttachment);
            }
            message.From = new MailAddress(from, sender);
            message.Subject = subject;
            message.SubjectEncoding = this._encoding;
            message.Body = body;
            message.BodyEncoding = this._encoding;
            message.IsBodyHtml = this._isHtml;
            message.Priority = MailPriority.Normal;
            foreach (string str in to)
            {
                message.To.Add(str);
            }
            if (this._bcc != null && this._bcc.Length > 0)
            {
                foreach (string b in this._bcc)
                {
                    message.Bcc.Add(b);
                }
            }
            if (this._cc != null && this._cc.Length > 0)
            {
                foreach (string c in this._cc)
                {
                    message.CC.Add(c);
                }
            }

            try
            {
                this._smtp.Send(message);
                return true;
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }

            return false;
        }

        private SmtpClient _smtp = new SmtpClient();
        private Encoding _encoding { get; set; }
        private bool _isHtml { get; set; }
        private string[] _cc { get; set; }
        private string[] _bcc { get; set; }
        /// <summary>
        ///       ,     
        /// </summary>
        public string Result { get; private set; }
    }


}

좋은 웹페이지 즐겨찾기