Example #1
0
        static void Main(string[] args)
        {
            Invoker invoker = new Invoker();

            invoker.Enqueue(new HttpCommand(new GetHttp()), new string[] { "wp.pl" });
            invoker.Enqueue(new FtpCommand(new GetFtp()), new string[] { "8.8.8.8" });
            invoker.Enqueue(new CopyFileCommand(new CopyFile()), new string[] { "a.txt", "b.txt" });

            invoker.Dequeue();
            invoker.Dequeue();
            invoker.Dequeue();

            Console.WriteLine("=================");

            Console.WriteLine("\n> XML pasrse using a TEMPLATE pattern");

            DataAccessHandler xml = new XmlDataAccess();

            xml.Execute();

            // Requires a Posgress DB
            // DataAccessHandler db = new DbDataAccess();
            // db.Execute();
            Console.WriteLine("\n> XML pasrse using a STRATEGY pattern");
            XmlDataContext xml2 = new XmlDataContext(new XmlStrategy());

            xml2.Execute();
        }
Example #2
0
 //private HttpContext _context;
 public SendEmailCache(/*HttpContext context*/)
     : base(CACHE_KEY, null)
 {
     base.TimeOut = 1440;
     xmlCtx       = new XmlDataContext();//TODO:注入
     //filePath = ServerUtility.ContentRootMapPath("~/APP_Data/SendEmail.config");//context.Server.MapPath("~/APP_Data/SendEmail.config");
     //HttpContext.Current.Response.Write(filePath);
 }
Example #3
0
        /// <summary>
        /// 获取Email账户信息
        /// </summary>
        /// <returns></returns>
        private EmailUser GetEmailUser(EmailAccountType emailUserType)
        {
            //emailUserType = emailUserType == EmailUserType.Default ? EmailUserType._163 : emailUserType;

            XmlDataContext xmlCtx        = new XmlDataContext();
            var            emailUserList = xmlCtx.GetXmlList <EmailUser>();
            var            emailUser     = emailUserList.FirstOrDefault(z => z.Name == emailUserType.ToString());

            if (emailUser == null)
            {
                LogUtility.EmailLogger.ErrorFormat("EmailUserType类型错误。传入类型:{0}", emailUserType.ToString());
            }
            return(emailUser);
        }
Example #4
0
        public bool DoSendEmail(string to, string subject, string body, string username, bool useEmailTemplate, EmailAccountType emailUserType, bool fromCache)
        {
            bool   sendSuccess      = false;
            string smtpEmailAddress = null;

            //if (emailUserType == EmailUserType.Default && to.ToUpper().EndsWith("@QQ.COM"))
            //{
            //    //useBackupEmail = !useBackupEmail;//如果是qq邮箱,则切换
            //    emailUserType = EmailUserType._163;
            //}

            //if (emailUserType == EmailUserType.Default)
            //{
            //    emailUserType = EmailUserType.KJH8;//默认使用KJH8发送
            //}

            try
            {
                body = this.GetEmailTemplate(body, useEmailTemplate);

                EmailUser targetEmailUser = this.GetEmailUser(emailUserType);
                smtpEmailAddress = targetEmailUser.EmailAddress;

                MailMessage m_message = new MailMessage();
                m_message.From = new MailAddress(targetEmailUser.EmailAddress, targetEmailUser.DisplayName);
                m_message.To.Add(new MailAddress(to));
                m_message.Subject    = subject;
                m_message.IsBodyHtml = true;//允许使用html格式
                m_message.Body       = body;

                SmtpClient m_smtpClient = new SmtpClient();
                m_smtpClient.Host = targetEmailUser.SmtpHost;
                m_smtpClient.Port = targetEmailUser.SmtpPort;
                if (targetEmailUser.NeedCredentials)
                {
                    m_smtpClient.Credentials = new System.Net.NetworkCredential(targetEmailUser.EmailAddress, targetEmailUser.Password);
                }

                m_smtpClient.Send(m_message);

                sendSuccess = true;
            }
            catch (Exception e)
            {
                LogUtility.EmailLogger.ErrorFormat($"TO:{to},Subject:{subject},Username:{username},Message:{e.Message}", e);
                sendSuccess = false;
            }
            finally
            {
                string cacheUsed = fromCache ? "缓存" : "直接发送";
                if (sendSuccess)
                {
                    //记录到AutoSendEmail中进行备份
                    XmlDataContext   xmlCtx   = new XmlDataContext();
                    AutoSendEmailBak emailBak = new AutoSendEmailBak()
                    {
                        Address  = to,
                        Body     = body,
                        Subject  = subject,
                        SendTime = DateTime.Now,
                        UserName = username
                    };
                    xmlCtx.Insert(emailBak);

                    LogUtility.EmailLogger.Info($"发送Email成功({cacheUsed})。To:{to},Subject:{subject},UserName:{username}。by:{smtpEmailAddress}");
                }
                else
                {
                    LogUtility.EmailLogger.Error($"发送Email失败({cacheUsed})。To:{to},Subject:{subject},UserName:{username}。by:{smtpEmailAddress}");
                }
            }
            return(sendSuccess);
        }