Advertisement
isotonicq

Christmas Mails

Jan 6th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.90 KB | None | 0 0
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Mail;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. using HtmlAgilityPack;
  21.  
  22. namespace Emails
  23. {
  24.     /// <summary>
  25.     /// Interaction logic for MainWindow.xaml
  26.     /// </summary>
  27.     ///
  28.     public static class Paths
  29.     {
  30.        public static string path = null;
  31.        public static string emailPath = null;
  32.        public static string emailBody = null;
  33.     }
  34.  
  35.     public partial class MainWindow : Window
  36.     {
  37.         DateTime lastRead = DateTime.MinValue;
  38.         FileSystemWatcher contactsWatcher = new FileSystemWatcher();
  39.         FileSystemWatcher templateWatcher = new FileSystemWatcher();
  40.  
  41.         ObservableCollection<string> contacts = new ObservableCollection<string>();
  42.  
  43.         public MainWindow()
  44.         {
  45.             InitializeComponent();
  46.             Main.Visibility = Visibility.Collapsed;
  47.             contacts.CollectionChanged += Contacts_CollectionChanged;
  48.         }
  49.  
  50.         private void Contacts_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  51.         {
  52.             bool add = true;
  53.             listBox.Items.Clear();
  54.  
  55.             this.Dispatcher.Invoke(() =>
  56.             {                
  57.                 foreach (var email in contacts)
  58.                 {
  59.                     foreach(var used in listBox.Items)
  60.                     {
  61.                         if (used.ToString() == email)
  62.                             add = false;
  63.                     }
  64.  
  65.                     if (add)
  66.                         listBox.Items.Add(email);
  67.  
  68.                     add = true;
  69.                 }
  70.             });
  71.         }
  72.  
  73.         private void Template_Changed(object sender, FileSystemEventArgs e)
  74.         {
  75.             var templateBuilder = new StringBuilder();
  76.             using (var reader = new StreamReader(File.OpenRead(Paths.path)))
  77.             {
  78.                 while (!reader.EndOfStream)
  79.                 {
  80.                     templateBuilder.AppendLine(reader.ReadLine());
  81.                 }
  82.                 reader.Close();
  83.             }
  84.             Paths.emailBody = templateBuilder.ToString();
  85.         }
  86.  
  87.         private async void SignOn(object sender, RoutedEventArgs e)
  88.         {
  89.             await Task.Run((Action)(() =>
  90.             {
  91.                 this.Dispatcher.Invoke((Action)(() =>
  92.                 {
  93.                     if (userBox1.Text != "" && passwordBox.Password != "")
  94.                     {
  95.                         this.Login.Visibility = Visibility.Collapsed;
  96.                         Main.Visibility = Visibility.Visible;
  97.                     }
  98.                     else
  99.                     {
  100.                         MessageBoxResult result = MessageBox.Show("Please fill in email and password box", "Alert", MessageBoxButton.OK, MessageBoxImage.Question);
  101.                     }
  102.                 }));
  103.             }));
  104.         }
  105.  
  106.         private async void LoadDatabase(object sender, RoutedEventArgs e)
  107.         {
  108.             await Task.Run(() =>
  109.             {
  110.                 this.Dispatcher.Invoke(() =>
  111.                 {
  112.                     OpenFileDialog openFileDialog = new OpenFileDialog();
  113.                     if (openFileDialog.ShowDialog() == true)
  114.                     {
  115.                         MessageBoxResult result = MessageBox.Show("From now program will be monitoring your database", "Alert", MessageBoxButton.OK, MessageBoxImage.Question);
  116.                         textBox.Text = openFileDialog.FileName;
  117.                         Paths.path = openFileDialog.FileName;
  118.                         richTextBox.AppendText($"\nLoaded database {DateTime.Now}");
  119.  
  120.                         contactsWatcher.Path = System.IO.Path.GetDirectoryName(Paths.path);
  121.                         contactsWatcher.NotifyFilter = NotifyFilters.LastWrite;
  122.                         contactsWatcher.Filter = "*.*";
  123.                         contactsWatcher.Changed += new FileSystemEventHandler(DatabaseChange);
  124.                         contactsWatcher.EnableRaisingEvents = true;
  125.  
  126.                         using (var reader = new StreamReader(File.OpenRead(openFileDialog.FileName)))
  127.                         {
  128.                             while (!reader.EndOfStream)
  129.                             {
  130.                                 contacts.Add(reader.ReadLine());
  131.                             }
  132.                             reader.Close();
  133.                         }
  134.                    
  135.                     }
  136.                 });
  137.             });
  138.         }
  139.  
  140.         private async void DatabaseChange(object sender, FileSystemEventArgs e)
  141.         {
  142.             DateTime lastWriteTime = File.GetLastWriteTime(Paths.path);
  143.  
  144.             await Task.Run(() =>
  145.             {
  146.                 this.Dispatcher.Invoke(() =>
  147.                 {
  148.  
  149.                     if (lastWriteTime != lastRead)
  150.                     {
  151.                         richTextBox.AppendText($"\nDatabase change detected {DateTime.Now}");
  152.  
  153.                         MessageBoxResult result = MessageBox.Show("Database change detected update list?", "Alert", MessageBoxButton.YesNo, MessageBoxImage.Question);
  154.                         if (result == MessageBoxResult.Yes)
  155.                         {
  156.                             richTextBox.AppendText($"\nContact list updated {DateTime.Now}");
  157.                             contacts.Clear();
  158.                             using (var reader = new StreamReader(File.OpenRead(Paths.path)))
  159.                             {
  160.                                 while (!reader.EndOfStream)
  161.                                 {
  162.                                     contacts.Add(reader.ReadLine());
  163.                                 }
  164.                                 reader.Close();
  165.                             }                
  166.                         }
  167.                         lastRead = lastWriteTime;
  168.                     }
  169.                 });
  170.             });
  171.         }
  172.  
  173.         private async void MailToSelected(object sender, RoutedEventArgs e)
  174.         {
  175.             await Task.Run(() =>
  176.             {
  177.                 this.Dispatcher.Invoke(() =>
  178.                 {
  179.                     if (Paths.emailBody != null && Paths.path != null)
  180.                     {
  181.                         var client = new SmtpClient("smtp.gmail.com", 587)
  182.                         {
  183.                             EnableSsl = true,
  184.                             DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
  185.                             UseDefaultCredentials = false,
  186.                             Credentials = new NetworkCredential(userBox1.Text, passwordBox.Password),
  187.                         };
  188.  
  189.                         var mail = new MailMessage();
  190.  
  191.                         mail.From = new MailAddress(userBox1.Text);
  192.                         mail.To.Add(new MailAddress(listBox.SelectedItem.ToString()));
  193.                         mail.Subject = ("Wesołych świąt");
  194.                         mail.Body = ("Wesołych świat i dużo zdrowia !");
  195.                         mail.IsBodyHtml = true;
  196.                         mail.Body = Paths.emailBody;
  197.  
  198.                         try
  199.                         {
  200.                             client.Send(mail);
  201.                             richTextBox.AppendText($"\nEmail to {listBox.SelectedItem.ToString()} was send {DateTime.Now}");
  202.                             listBox.Items.Remove(listBox.SelectedItem);
  203.                         }
  204.                         catch
  205.                         {
  206.                             richTextBox.AppendText($"\nEmail to {listBox.SelectedItem.ToString()} was not send {DateTime.Now}");
  207.                         }
  208.                     }
  209.                     else
  210.                     {
  211.                         MessageBoxResult result = MessageBox.Show("Please load database and template to send an email", "Alert", MessageBoxButton.OK, MessageBoxImage.Question);
  212.                     }
  213.                 });
  214.             });
  215.         }
  216.  
  217.         private async void MailToAll(object sender, RoutedEventArgs e)
  218.         {
  219.             await Task.Run(() =>
  220.             {
  221.                 this.Dispatcher.Invoke(() =>
  222.                 {
  223.                     if(Paths.emailBody!=null && Paths.path!=null)
  224.                     {
  225.                         var client = new SmtpClient("smtp.gmail.com", 587)
  226.                         {
  227.                             EnableSsl = true,
  228.                             DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
  229.                             UseDefaultCredentials = false,
  230.                             Credentials = new NetworkCredential(userBox1.Text, passwordBox.Password),
  231.                         };
  232.  
  233.                         var mail = new MailMessage();
  234.  
  235.                         mail.From = new MailAddress(userBox1.Text);
  236.                         mail.To.Add(new MailAddress(listBox.SelectedItem.ToString()));
  237.                         mail.Subject = ("Wesołych świąt");
  238.                         mail.Body = ("Wesołych świat i dużo zdrowia !");
  239.                         mail.IsBodyHtml = true;
  240.                         mail.Body = Paths.emailBody;
  241.  
  242.                         foreach (var CONTACT in contacts)
  243.                         {
  244.                             mail.To.Add(CONTACT.ToString());
  245.                         }
  246.  
  247.                         try
  248.                         {
  249.                             client.Send(mail);
  250.                             richTextBox.AppendText($"\nEmails to all contacts were send {DateTime.Now}");
  251.                             listBox.Items.Clear();
  252.                         }
  253.                         catch
  254.                         {
  255.                             richTextBox.AppendText($"\nEmails to all contacts were not send {DateTime.Now}");
  256.                         }
  257.                     }
  258.                     else
  259.                     {
  260.                         MessageBoxResult result = MessageBox.Show("Please load database and template to send an email", "Alert", MessageBoxButton.OK, MessageBoxImage.Question);
  261.                     }
  262.                 });
  263.             });
  264.         }
  265.  
  266.         private async void RemoveContact(object sender, RoutedEventArgs e)
  267.         {
  268.             await Task.Run(() =>
  269.             {
  270.                 this.Dispatcher.Invoke(() =>
  271.                 {
  272.                     listBox.Items.Remove(listBox.SelectedItem);
  273.                 });
  274.             });
  275.         }
  276.  
  277.         private async void AddContact(object sender, RoutedEventArgs e)
  278.         {
  279.             await Task.Run(() =>
  280.             {
  281.                 this.Dispatcher.Invoke(() =>
  282.                 {
  283.                     if (textBox1.Text != "")
  284.                     {
  285.                         listBox.Items.Add(textBox1.Text);
  286.                         textBox1.Text = "";
  287.                     }
  288.                 });
  289.             });
  290.         }
  291.  
  292.         private async void LoadTemplate(object sender, RoutedEventArgs e)
  293.         {
  294.             await Task.Run(() =>
  295.             {
  296.                 this.Dispatcher.Invoke(() =>
  297.                 {
  298.  
  299.                     OpenFileDialog openFileDialog = new OpenFileDialog();
  300.                     if (openFileDialog.ShowDialog() == true)
  301.                     {
  302.                         MessageBoxResult result = MessageBox.Show("From now program will be monitoring your template", "Alert", MessageBoxButton.OK, MessageBoxImage.Question);
  303.  
  304.                         textBox2.Text = openFileDialog.FileName;
  305.                         Paths.emailPath = openFileDialog.FileName;
  306.                         richTextBox.AppendText($"\nLoaded template {DateTime.Now}");
  307.  
  308.                         templateWatcher.Path = System.IO.Path.GetDirectoryName(Paths.path);
  309.                         templateWatcher.NotifyFilter = NotifyFilters.LastWrite;
  310.                         templateWatcher.Filter = "*.*";
  311.                         templateWatcher.Changed += Template_Changed; ;
  312.                         templateWatcher.EnableRaisingEvents = true;
  313.  
  314.                         var templateBuilder = new StringBuilder();
  315.                         using (var reader = new StreamReader(File.OpenRead(openFileDialog.FileName)))
  316.                         {
  317.                             while (!reader.EndOfStream)
  318.                             {
  319.                                 templateBuilder.AppendLine(reader.ReadLine());
  320.                             }
  321.                             reader.Close();
  322.                         }                                
  323.                         Paths.emailBody = templateBuilder.ToString();
  324.                     }
  325.                 });
  326.             });
  327.         }
  328.     }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement