邮件发送的帮助类

来源:互联网 发布:c语言中长整型变量 编辑:程序博客网 时间:2024/06/06 01:43

  public sealed class EmailHelper
    {
        private static readonly string smtp;
        private static readonly string fromAddress;
        private static readonly string accountName;
        private static readonly string password;

        static EmailHelper()
        {
            smtp = ConfigHelper.GetValueByAttribute("SystemConfig", "smtpMallAdd");
            fromAddress = ConfigHelper.GetValueByAttribute("SystemConfig", "smtpMall");
            accountName = ConfigHelper.GetValueByAttribute("SystemConfig", "smtpMallName");
            password = ConfigHelper.GetValueByAttribute("SystemConfig", "smtpMallPassword");
        }


        /// <summary>
        /// 发邮件
        /// </summary>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="toAddress">收件人地址</param>
        public static void SendMail(string subject, string body, string toAddress)
        {
            SendMail(subject, body, toAddress, "");
        }


        /// <summary>
        /// 发邮件
        /// </summary>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="toAddress">收件人地址</param>
        /// <param name="mailModel">邮件模版</param>
        public static void SendMail(string subject, string body, string toAddress, string mailModel)
        {
            SendMail(subject, body, toAddress, mailModel, "");
        }

        /// <summary>
        /// 发带附件邮件
        /// </summary>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="toAddress">发送地址</param>
        /// <param name="mailModel">邮件模版</param>
        /// <param name="fileName">除件名称</param>
        public static void SendMail(string subject, string body, string toAddress, string mailModel, string fileName)
        {
            var client = new SmtpClient
                             {
                                 DeliveryMethod = SmtpDeliveryMethod.Network,
                                 Host = smtp,
                                 Credentials = new NetworkCredential(accountName, password)
                             }; //实例化一个SmtpClient

            var msg = new MailMessage(fromAddress, toAddress);

            string model = mailModel != "" ? ConfigHelper.GetValueByAttribute("SystemMailModel", mailModel) : ConfigHelper.GetValueByAttribute("SystemMailModel", "mailModel");
            msg.Body = model.Replace("[description]", body);
            msg.Subject = subject;
            msg.IsBodyHtml = true;
            if (fileName != "")
            {
                msg.Attachments.Add(new Attachment(fileName));
            }

            try
            {
                client.Send(msg);
            }
            catch (Exception)
            {
            }
        }


        /// <summary>
        /// 发邮件
        /// </summary>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="toAddressList">收件人地址列表</param>
        public static void SendMail(string subject, string body, List<string> toAddressList)
        {
            foreach (var item in toAddressList)
            {
                SendMail(subject, body, item, "");
            }
        }


        /// <summary>
        /// 获取用户填写的Email所在的邮件服务器地址
        /// </summary>
        /// <param name="strEmail">email地址</param>
        /// <returns>如果EMIAL地址有效则返回地址,没有则返回null</returns>
        private static string GetMailServer(string strEmail)
        {
            if (!IsEmail(strEmail))
            {
                return null;
            }
            string strDomain = strEmail.Trim().ToLower().Split('@')[1];
            var PSinfo = new ProcessStartInfo
                             {
                                 UseShellExecute = false,
                                 RedirectStandardInput = true,
                                 RedirectStandardOutput = true,
                                 FileName = "nslookup",
                                 CreateNoWindow = true,
                                 Arguments = ("-type=mx " + strDomain)
                             };
            Process proc = Process.Start(PSinfo);
            if (proc != null)
            {
                StreamReader Sreader = proc.StandardOutput;
                var rgx = new Regex("mail exchanger = (?<mailServer>[^//s]+)");
                string strResponse;
                while ((strResponse = Sreader.ReadLine()) != null)
                {
                    Match aMatch = rgx.Match(strResponse);
                    if (rgx.Match(strResponse).Success)
                    {
                        string Gvalue = aMatch.Groups["mailServer"].Value;
                        return Gvalue;
                    }
                }
            }
            return null;
        }
        //正则表达式验证Email地址格式
        private static bool IsEmail(string str_Email)
        {
            return Regex.IsMatch(str_Email, @"^([/w-/.]+)+[a-zA-Z0-9]+@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$");
        }
        public static bool ValidateEmail(string strEmail)
        {
            string msg = GetMailServer(strEmail);
            return !string.IsNullOrEmpty(msg);

        }
    }

原创粉丝点击