コード例 #1
0
        /// <summary>
        /// 值转换为枚举
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="val"></param>
        /// <returns></returns>
        public static T ToEnum <T>(this string val)
        {
            object realValue = null;

            //判断值是否为整形
            if (InPutValidate.IsNumber(val))
            {
                realValue = val.ToInt();
            }
            else
            {
                realValue = val;
            }
            if (!Enum.IsDefined(typeof(T), realValue))
            {
                throw new Exception("非法的枚举值!");
            }

            var enObj = Enum.Parse(typeof(T), val);

            return((T)enObj);
        }
コード例 #2
0
        public static void MailSend(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList <string> mailTo, IList <string> mailCC, IList <string> mailBCC, string mailTitle, string mailContent, IList <string> mailAttachments, System.Text.Encoding encoding, bool isBodyHtml)
        {
            MailMessage message = new MailMessage();

            if (string.IsNullOrEmpty(mailFrom.Trim()))
            {
                throw new Exception("发送邮件不可以为空");
            }
            if (!InPutValidate.IsEmail(mailFrom.Trim()))
            {
                throw new Exception("发送邮件地址不合法!");
            }
            message.From = new MailAddress(mailFrom);
            if (mailTo.Count <= 0)
            {
                throw new Exception("接收邮件不可以为空");
            }
            foreach (var item in mailTo)
            {
                if (!InPutValidate.IsEmail(item))
                {
                    throw new Exception(string.Format("接收邮件:{0} 为非法邮件地址!", item));
                }
            }



            foreach (string s in mailTo)
            {
                message.To.Add(new MailAddress(s));
            }
            if (mailCC.Count > 0)
            {
                foreach (string s in mailCC)
                {
                    message.CC.Add(new MailAddress(s));
                }
            }
            if (mailBCC.Count > 0)
            {
                foreach (string s in mailBCC)
                {
                    message.Bcc.Add(new MailAddress(s));
                }
            }
            message.Subject      = mailTitle;
            message.Body         = mailContent;
            message.BodyEncoding = encoding;          //邮件编码
            message.IsBodyHtml   = isBodyHtml;        //内容格式是否是html
            message.Priority     = MailPriority.High; //设置发送的优先集
            //附件
            foreach (string att in mailAttachments)
            {
                message.Attachments.Add(new Attachment(att));
            }
            SmtpClient smtpClient = new SmtpClient();

            smtpClient.Host        = mailSmtpServer;
            smtpClient.Credentials = new NetworkCredential(maiFromlAccount, mailFromPwd);
            smtpClient.Timeout     = 1000;
            smtpClient.EnableSsl   = false;      //不使用ssl连接
            smtpClient.Send(message);
        }
コード例 #3
0
        /// <summary>
        /// 创建cef,并 打开制定的网址
        /// </summary>
        /// <param name="url"></param>
        /// <param name="handlerRequest"></param>
        /// <param name="timeOut"></param>
        /// <returns></returns>
        public static Task <CookiedCefBrowser> CreateNewWebBrowser(string url, EventHandler <LoadEndEventArgs> handlerRequest, int timeOut = 5000)
        {
            //验证是否是合法的URL
            var isUrl = InPutValidate.IsUrl(url);

            if (!isUrl)
            {
                return(Task.FromResult <CookiedCefBrowser>(null));
            }
            //使用任务 锁保证事件变为同步
            var tcs = new TaskCompletionSource <CookiedCefBrowser>();


            // Instruct CEF to not render to a window at all.
            CefWindowInfo cefWindowInfo = CefWindowInfo.Create();

            cefWindowInfo.SetAsWindowless(IntPtr.Zero, true);

            // Settings for the browser window itself (e.g. should JavaScript be enabled?).
            var cefBrowserSettings = new CefBrowserSettings();

            // Initialize some the cust interactions with the browser process.
            // The browser window will be 1280 x 720 (pixels).
            var cefClient = new HeadLessCefClient(1, 1);
            var loader    = cefClient.GetCurrentLoadHandler();

            loader.BrowserCreated += (s, e) =>
            {
                //事件通知 当cef  browser 创建完毕
                //创建完毕后 保存 browser 对象的实例
                var brw         = e.Browser;
                var etaoBrowser = new CookiedCefBrowser {
                    CefBrowser = brw, CefLoader = loader, CefClient = cefClient
                };

                tcs.TrySetResult(etaoBrowser);
            };
            if (null != handlerRequest)
            {
                loader.LoadEnd += handlerRequest;
            }
            ////注册  加载完毕事件handler
            //loader.LoadEnd += this.OnWebBrowserLoadEnd;
            // Start up the browser instance.
            // string url = "about:blank";
            CefBrowserHost.CreateBrowser(cefWindowInfo, cefClient, cefBrowserSettings, url);

            //设定超时
            //超时监听

            int timeoutMs = timeOut;
            var ctoken    = new CancellationTokenSource(timeoutMs);

            ctoken.Token.Register(() =>
            {
                var brw         = loader.Browser;
                var etaoBrowser = new CookiedCefBrowser {
                    CefBrowser = brw, CefLoader = loader, CefClient = cefClient
                };

                //超时结果返回空
                tcs.TrySetResult(etaoBrowser);

                //tcs.TrySetCanceled();
            }, useSynchronizationContext: false);

            return(tcs.Task);
        }