Пример #1
0
        public void MailSetup()
        {
            try
            {
                var vm = PreferencesViewModel.Create();

                MailPreferencesCollection myPreferences = new MailPreferencesCollection();
                myPreferences.LoadAll();
                if (myPreferences.HasData)
                {
                    vm.EmailAddress = myPreferences[0].EmailAddress;
                    vm.Password     = myPreferences[0].Password;
                }

                var result = this.DialogService.ShowDialog(
                    dialogCommands: vm.DialogCommands,
                    title: "Email Setup",
                    documentType: "PreferencesView",
                    parameter: null,
                    parentViewModel: this,
                    viewModel: vm);

                if (result != null && result.IsDefault)
                {
                    if (myPreferences.HasData)
                    {
                        myPreferences[0].EmailAddress = vm.EmailAddress;
                        myPreferences[0].Password     = vm.Password;
                        myPreferences[0].Save();
                    }
                    else
                    {
                        MailPreferences pref = new MailPreferences();
                        pref.EmailAddress = vm.EmailAddress;
                        pref.Password     = vm.Password;
                        pref.Save();
                    }
                }
            }
            catch (Exception ex)
            {
                this.SplashScreenService.HideSplashScreen();
                this.MessageBoxService.ShowMessage(ex.Message, "Error");
            }
        }
Пример #2
0
        public async void SendEmails()
        {
            MailPreferencesCollection myPrefs = new MailPreferencesCollection();

            myPrefs.LoadAll();
            if (!myPrefs.HasData)
            {
                this.MessageBoxService.ShowMessage("You must setup your email first!", "Mail Not Setup", MessageButton.OK, MessageIcon.Stop);
                return;
            }

            this.SplashScreenService.ShowSplashScreen();
            this.SplashScreenService.SetSplashScreenState("Refreshing next showings...");
            string body = "";

            DataTable shows = new DataTable();

            shows.Columns.Add("Name", typeof(string));
            shows.Columns.Add("Time", typeof(DateTime));
            shows.Columns.Add("Summary", typeof(string));

            DataTable shows2 = new DataTable();

            shows2.Columns.Add("Name", typeof(string));
            shows2.Columns.Add("Time", typeof(DateTime));
            shows2.Columns.Add("Summary", typeof(string));

            foreach (TVShows show in TVShowsCollection)
            {
                var series = await TVMaze.TVMaze.GetSeries(Convert.ToUInt32(show.MazeId), true, false);

                this.SplashScreenService.SetSplashScreenState("Looking up show times for " + series.name + "...");

                foreach (var episode in series.Episodes)
                {
                    if (Convert.ToDateTime(episode.airdate).Date == DateTime.Today.Date)
                    {
                        var convertedDate = DateTime.SpecifyKind(
                            DateTime.Parse(episode.airstamp.ToString()),
                            DateTimeKind.Utc);

                        DataRow dr = shows.NewRow();
                        if (series.network != null && series.network.name != "")
                        {
                            dr[0] = series.name + "(" + series.network.name + ")";
                        }
                        else if (series.webChannel != null && series.webChannel.name != "")
                        {
                            dr[0] = series.name + "(" + series.webChannel.name + ")";
                        }
                        else
                        {
                            dr[0] = series.name;
                        }
                        dr[1] = convertedDate;
                        if (!string.IsNullOrEmpty(episode.summary))
                        {
                            dr[2] = episode.summary;
                        }
                        else
                        {
                            dr[2] = episode.name;
                        }
                        shows.Rows.Add(dr);
                    }
                    else if (Convert.ToDateTime(episode.airdate).Date == DateTime.Today.Date.AddDays(1))
                    {
                        var convertedDate = DateTime.SpecifyKind(
                            DateTime.Parse(episode.airstamp.ToString()),
                            DateTimeKind.Utc);

                        DataRow dr = shows2.NewRow();
                        if (series.network != null && series.network.name != "")
                        {
                            dr[0] = series.name + "(" + series.network.name + ")";
                        }
                        else if (series.webChannel != null && series.webChannel.name != "")
                        {
                            dr[0] = series.name + "(" + series.webChannel.name + ")";
                        }
                        else
                        {
                            dr[0] = series.name;
                        }
                        dr[1] = convertedDate;
                        if (!string.IsNullOrEmpty(episode.summary))
                        {
                            dr[2] = episode.summary;
                        }
                        else
                        {
                            dr[2] = episode.name;
                        }
                        shows2.Rows.Add(dr);
                    }
                }
            }

            if (shows.Rows.Count > 0)
            {
                var orderedRows = from row in shows.AsEnumerable()
                                  orderby row.Field <DateTime>("Time")
                                  select row;

                DataTable tblOrdered = orderedRows.CopyToDataTable();

                body = "The following shows are scheduled to air today: " + Environment.NewLine + Environment.NewLine;

                foreach (var row in tblOrdered.Rows)
                {
                    body += Convert.ToDateTime(((System.Data.DataRow)row).ItemArray[1]).ToShortTimeString() + ": " + ((System.Data.DataRow)row).ItemArray[0] + Environment.NewLine + ((System.Data.DataRow)row).ItemArray[2] + Environment.NewLine + Environment.NewLine;
                }
            }

            if (shows2.Rows.Count > 0)
            {
                var orderedRows2 = from row in shows2.AsEnumerable()
                                   orderby row.Field <DateTime>("Time")
                                   select row;

                DataTable tblOrdered2 = orderedRows2.CopyToDataTable();

                body += "The following shows are scheduled to air tomorrow: " + Environment.NewLine + Environment.NewLine;

                foreach (var row in tblOrdered2.Rows)
                {
                    body += Convert.ToDateTime(((System.Data.DataRow)row).ItemArray[1]).ToShortTimeString() + ": " + ((System.Data.DataRow)row).ItemArray[0] + Environment.NewLine + ((System.Data.DataRow)row).ItemArray[2] + Environment.NewLine + Environment.NewLine;
                }
            }

            if (shows.Rows.Count > 0 || shows2.Rows.Count > 0)
            {
                this.SplashScreenService.SetSplashScreenState("Creating and sending email...");

                //SmtpClient client = new SmtpClient();
                //client.Port = 587;
                //client.Host = "smtp.gmail.com";
                //client.EnableSsl = true;
                //client.Timeout = 10000;
                //client.DeliveryMethod = SmtpDeliveryMethod.Network;
                //client.UseDefaultCredentials = false;
                //client.Credentials = new System.Net.NetworkCredential(myPrefs[0].EmailAddress, myPrefs[0].Password);

                RecipientsCollection myCol = new RecipientsCollection();
                myCol.LoadAll();

                foreach (Recipients r in myCol)
                {
                    var client = new SmtpClient("smtp.gmail.com", 587)
                    {
                        Credentials = new System.Net.NetworkCredential(myPrefs[0].EmailAddress.ToString(), myPrefs[0].Password.ToString()),
                        EnableSsl   = true
                    };
                    client.Send(myPrefs[0].EmailAddress.ToString(), r.EmailAddress.ToString(), "Today's TV Shows for " + DateTime.Today.ToShortDateString(), body);
                }

                this.SplashScreenService.HideSplashScreen();
            }
        }