Ejemplo n.º 1
0
        public void NewsmailSend(DBGuid IDVal, bool activityOther,
            bool activityPrevention,
            bool activityRehabilitation,
            bool activityResearch,
            bool  activityNDI,
            bool activityAll)
        {
            // Check permission: admin
              PrincipalPermission permAdmin = new PrincipalPermission(Thread.CurrentPrincipal.Identity.Name, "Administrator");
              permAdmin.Demand();

              TraceCallEnterEvent.Raise();
              try
              {
            // Logical checks
            Email selected = base.EmailSelect(IDVal);
            if (selected == null)
              throw new ApplicationException("A megadott azonosítóval nem létezik hírlevél.");
            if (!selected.Sent.IsNull)
              throw new ApplicationException("Ez a levél már el lett küldve.");
            if (!selected.Category.Equals(EmailCategory.Newsmail))
              throw new ApplicationException("Csak hírlevél kategóriájú levél küldhetõ ki.");

            // Get recipients
            string sTo = "";
            //sTo += "*****@*****.**";
            UserService srv = new UserService(m_DataContext);
            UserContainer allData =
              srv.UserSelectOfEmailRecipient(activityOther, activityPrevention, activityRehabilitation, activityResearch, activityNDI,
                                         activityAll);
            for (int i = 0; i < allData.All.Count; i++)
            {
              string sEmail = ((User) allData.All.Items[i]).Email;
              if (sEmail.Length > 0)
              {
            sTo += sEmail;
            sTo += ";";
              }
            }

            if (sTo.Length == 0)
              throw new ApplicationException("Nincs megadható címzett.");
            selected.To = sTo;

            base.EmailUpdate(selected);

            //Itt még az attachementeket is fel kell olvasni
            selected.EmailAttachments = base.SelectChildrenByAttachmentOfEmail(selected.ID);
            // Send mail
            Send(selected);
            selected.Sent = DBDateTime.Now;
            base.EmailUpdate(selected);

            BusinessAuditEvent.Success(
              new EventParameter("EmailID", IDVal),
              new EventParameter("EmailSubject", selected.Subject)
              );
            TraceCallReturnEvent.Raise();
            return;
              }
              catch (Exception ex)
              {
            ExceptionManager.Publish(ex);
            BusinessAuditEvent.Fail(
              new EventParameter("Exception", ex.ToString()),
              new EventParameter("EmailID", IDVal)
              );
            TraceCallReturnEvent.Raise(false);
            throw;
              }
        }
        /// Megállítja az órát amíg lekérdezi az adatbázisból a leveleket, kiválogatja közülük az el nem
        /// küldötteket és meghívja rájuk a SendMail metodust, majd újraindítja az órát.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Timers.ElapsedEventArgs"/> instance containing the event data.</param>
        private void NewsMailServiceTimerTick(object sender, ElapsedEventArgs e)
        {
            if (_firstNews)
              {

            #region időzítő beállítása a mail funkcióhoz
            try
            {
              string intervallString = ConfigurationManager.AppSettings["RssNewsMailTimerIntervall"];
              double intervall = 150000;
              try
              {
            intervall = Convert.ToDouble(intervallString);
            _newsMailTimer.Interval = intervall;
              }
              catch (Exception)
              {

              }
              _newsMailTimer.AutoReset = true;
              _newsMailTimer.Enabled = true;
              EventLog.WriteEntry("Ndi.HelpDesk.HostingServiceNewsMailServiceTimerTick started!");
              _firstNews = false;
            }

            catch (Exception ex)
            {
              EventLog.WriteEntry("Ndi.HelpDesk.HostingService_NewsMailServiceTimerTick", ex.ToString(), EventLogEntryType.Error, 2001);
            }
            #endregion

              }
              try
              {
            _newsMailTimer.Stop();
            //meg kell nézni, hogy kell-e most küldeni
            string intervallString = ConfigurationManager.AppSettings["RssNewsMailSendingDay"];
            DayOfWeek dayOfweek = DayOfWeek.Monday;
            switch (intervallString)
            {
              case "H":
            dayOfweek = DayOfWeek.Monday;
            break;
              case "K":
            dayOfweek = DayOfWeek.Tuesday;
            break;
              case "SZE":
            dayOfweek = DayOfWeek.Wednesday;
            break;
              case "CS":
            dayOfweek = DayOfWeek.Thursday;
            break;
              case "P":
            dayOfweek = DayOfWeek.Friday;
            break;
              case "SZO":
            dayOfweek = DayOfWeek.Saturday;
            break;
              case "V":
            dayOfweek = DayOfWeek.Sunday;
            break;
            }
            //ha ma az a nap van
            if (DateTime.Today.DayOfWeek == dayOfweek)
            {
              NameValueCollection collServFact = (NameValueCollection)ConfigurationManager.GetSection("PafiCompetition");
              if (collServFact != null)
              {
            string rssFeddUrl = collServFact["RssUrl"];
            string template = collServFact["RssTemplate"];
            string itemTemplate = collServFact["RssItemTemplate"];
            IIdentity identity = new GenericIdentity("NDIService");
            string[] roles = { "Administrator" };
            NdiPrincipal principal =
              new NdiPrincipal(identity, roles, "NDIService");
            Thread.CurrentPrincipal = principal;

            //megnézzük, hogy mamár próbáltunk-e küldeni
            IEmailService srvEmail = ServiceFactory.GetEmailService();
            Email filter = new Email(Guid.Empty);
            filter.Category = EmailCategory.NewsMailRSS;
            DateTime dateFrom = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day - 1, 23, 59, 59);
            DateTime dateTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
            filter.FilterOnSentFrom = dateFrom;
            filter.FilterOnSentTo = dateTo;
            EmailContainer emails = srvEmail.EmailSelectFiltered(filter);
            if (emails.AllCount == 0) // ma még nem küldtünk
            {
              RssFeedPafi feed = RssReaderPafi.GetFeed(rssFeddUrl);
              if (feed.ErrorMessage == null || feed.ErrorMessage == "")
              {
                string html = RssReaderPafi.CreateHtml(feed, template, itemTemplate, "", 5);
                html = html.Replace("\\n", "\n");
                string body = "";
                string subject = "";
                IEmailTemplateService srvTemplate = ServiceFactory.GetEmailTemplateService();
                srvTemplate.GetEmailTemplateByCode(ref subject, ref body, EmailCategory.NewsMailRSS);
                UserService srvUser = new UserService();
                UserContainer users = srvUser.UserSelectOfEmailRecipient(true, true, true, true, true, true);
                string to = "";
                for (int i = 0; i < users.All.Count; i++)
                {
                  string sEmail = ((User)users.All.Items[i]).Email;
                  if (sEmail.Length > 0)
                  {
                    if (to.IndexOf(sEmail) == -1)
                    {
                      to += sEmail;
                      to += ";";
                    }
                  }
                }
                if (to.Length > 0)
                {
                  //set mail:
                  Email mail = new Email(Guid.NewGuid());

                  mail.Category = EmailCategory.NewsMailRSS;
                  mail.Subject = subject;
                  mail.To = to;
                  mail.MailBody = html;

                  if (mail.MailBody != null && mail.To != null && mail.Subject != null)
                  {
                    // Save data to database
                    IEmailService emailSrv = ServiceFactory.GetEmailService();
                    emailSrv.EmailInsert(mail);
                    try
                    {
                      // Sending mail:
                      emailSrv.EmailSend(mail.ID);
                    }
                    catch (Exception)
                    {

                    }

                  }
                }
              }
            }
              }
            }
              }
              catch (Exception ex)
              {
            EventLog.WriteEntry("Ndi.HelpDesk.HostingService_NewsMailServiceTimerTick", ex.ToString(),
                            EventLogEntryType.Error, 2001);
              }
              finally
              {
            _newsMailTimer.Start();
              }
        }