Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Mail;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using HtmlAgilityPack;
- namespace Emails
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- ///
- public static class Paths
- {
- public static string path = null;
- public static string emailPath = null;
- public static string emailBody = null;
- }
- public partial class MainWindow : Window
- {
- DateTime lastRead = DateTime.MinValue;
- FileSystemWatcher contactsWatcher = new FileSystemWatcher();
- FileSystemWatcher templateWatcher = new FileSystemWatcher();
- ObservableCollection<string> contacts = new ObservableCollection<string>();
- public MainWindow()
- {
- InitializeComponent();
- Main.Visibility = Visibility.Collapsed;
- contacts.CollectionChanged += Contacts_CollectionChanged;
- }
- private void Contacts_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
- {
- bool add = true;
- listBox.Items.Clear();
- this.Dispatcher.Invoke(() =>
- {
- foreach (var email in contacts)
- {
- foreach(var used in listBox.Items)
- {
- if (used.ToString() == email)
- add = false;
- }
- if (add)
- listBox.Items.Add(email);
- add = true;
- }
- });
- }
- private void Template_Changed(object sender, FileSystemEventArgs e)
- {
- var templateBuilder = new StringBuilder();
- using (var reader = new StreamReader(File.OpenRead(Paths.path)))
- {
- while (!reader.EndOfStream)
- {
- templateBuilder.AppendLine(reader.ReadLine());
- }
- reader.Close();
- }
- Paths.emailBody = templateBuilder.ToString();
- }
- private async void SignOn(object sender, RoutedEventArgs e)
- {
- await Task.Run((Action)(() =>
- {
- this.Dispatcher.Invoke((Action)(() =>
- {
- if (userBox1.Text != "" && passwordBox.Password != "")
- {
- this.Login.Visibility = Visibility.Collapsed;
- Main.Visibility = Visibility.Visible;
- }
- else
- {
- MessageBoxResult result = MessageBox.Show("Please fill in email and password box", "Alert", MessageBoxButton.OK, MessageBoxImage.Question);
- }
- }));
- }));
- }
- private async void LoadDatabase(object sender, RoutedEventArgs e)
- {
- await Task.Run(() =>
- {
- this.Dispatcher.Invoke(() =>
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- if (openFileDialog.ShowDialog() == true)
- {
- MessageBoxResult result = MessageBox.Show("From now program will be monitoring your database", "Alert", MessageBoxButton.OK, MessageBoxImage.Question);
- textBox.Text = openFileDialog.FileName;
- Paths.path = openFileDialog.FileName;
- richTextBox.AppendText($"\nLoaded database {DateTime.Now}");
- contactsWatcher.Path = System.IO.Path.GetDirectoryName(Paths.path);
- contactsWatcher.NotifyFilter = NotifyFilters.LastWrite;
- contactsWatcher.Filter = "*.*";
- contactsWatcher.Changed += new FileSystemEventHandler(DatabaseChange);
- contactsWatcher.EnableRaisingEvents = true;
- using (var reader = new StreamReader(File.OpenRead(openFileDialog.FileName)))
- {
- while (!reader.EndOfStream)
- {
- contacts.Add(reader.ReadLine());
- }
- reader.Close();
- }
- }
- });
- });
- }
- private async void DatabaseChange(object sender, FileSystemEventArgs e)
- {
- DateTime lastWriteTime = File.GetLastWriteTime(Paths.path);
- await Task.Run(() =>
- {
- this.Dispatcher.Invoke(() =>
- {
- if (lastWriteTime != lastRead)
- {
- richTextBox.AppendText($"\nDatabase change detected {DateTime.Now}");
- MessageBoxResult result = MessageBox.Show("Database change detected update list?", "Alert", MessageBoxButton.YesNo, MessageBoxImage.Question);
- if (result == MessageBoxResult.Yes)
- {
- richTextBox.AppendText($"\nContact list updated {DateTime.Now}");
- contacts.Clear();
- using (var reader = new StreamReader(File.OpenRead(Paths.path)))
- {
- while (!reader.EndOfStream)
- {
- contacts.Add(reader.ReadLine());
- }
- reader.Close();
- }
- }
- lastRead = lastWriteTime;
- }
- });
- });
- }
- private async void MailToSelected(object sender, RoutedEventArgs e)
- {
- await Task.Run(() =>
- {
- this.Dispatcher.Invoke(() =>
- {
- if (Paths.emailBody != null && Paths.path != null)
- {
- var client = new SmtpClient("smtp.gmail.com", 587)
- {
- EnableSsl = true,
- DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
- UseDefaultCredentials = false,
- Credentials = new NetworkCredential(userBox1.Text, passwordBox.Password),
- };
- var mail = new MailMessage();
- mail.From = new MailAddress(userBox1.Text);
- mail.To.Add(new MailAddress(listBox.SelectedItem.ToString()));
- mail.Subject = ("Wesołych świąt");
- mail.Body = ("Wesołych świat i dużo zdrowia !");
- mail.IsBodyHtml = true;
- mail.Body = Paths.emailBody;
- try
- {
- client.Send(mail);
- richTextBox.AppendText($"\nEmail to {listBox.SelectedItem.ToString()} was send {DateTime.Now}");
- listBox.Items.Remove(listBox.SelectedItem);
- }
- catch
- {
- richTextBox.AppendText($"\nEmail to {listBox.SelectedItem.ToString()} was not send {DateTime.Now}");
- }
- }
- else
- {
- MessageBoxResult result = MessageBox.Show("Please load database and template to send an email", "Alert", MessageBoxButton.OK, MessageBoxImage.Question);
- }
- });
- });
- }
- private async void MailToAll(object sender, RoutedEventArgs e)
- {
- await Task.Run(() =>
- {
- this.Dispatcher.Invoke(() =>
- {
- if(Paths.emailBody!=null && Paths.path!=null)
- {
- var client = new SmtpClient("smtp.gmail.com", 587)
- {
- EnableSsl = true,
- DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
- UseDefaultCredentials = false,
- Credentials = new NetworkCredential(userBox1.Text, passwordBox.Password),
- };
- var mail = new MailMessage();
- mail.From = new MailAddress(userBox1.Text);
- mail.To.Add(new MailAddress(listBox.SelectedItem.ToString()));
- mail.Subject = ("Wesołych świąt");
- mail.Body = ("Wesołych świat i dużo zdrowia !");
- mail.IsBodyHtml = true;
- mail.Body = Paths.emailBody;
- foreach (var CONTACT in contacts)
- {
- mail.To.Add(CONTACT.ToString());
- }
- try
- {
- client.Send(mail);
- richTextBox.AppendText($"\nEmails to all contacts were send {DateTime.Now}");
- listBox.Items.Clear();
- }
- catch
- {
- richTextBox.AppendText($"\nEmails to all contacts were not send {DateTime.Now}");
- }
- }
- else
- {
- MessageBoxResult result = MessageBox.Show("Please load database and template to send an email", "Alert", MessageBoxButton.OK, MessageBoxImage.Question);
- }
- });
- });
- }
- private async void RemoveContact(object sender, RoutedEventArgs e)
- {
- await Task.Run(() =>
- {
- this.Dispatcher.Invoke(() =>
- {
- listBox.Items.Remove(listBox.SelectedItem);
- });
- });
- }
- private async void AddContact(object sender, RoutedEventArgs e)
- {
- await Task.Run(() =>
- {
- this.Dispatcher.Invoke(() =>
- {
- if (textBox1.Text != "")
- {
- listBox.Items.Add(textBox1.Text);
- textBox1.Text = "";
- }
- });
- });
- }
- private async void LoadTemplate(object sender, RoutedEventArgs e)
- {
- await Task.Run(() =>
- {
- this.Dispatcher.Invoke(() =>
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- if (openFileDialog.ShowDialog() == true)
- {
- MessageBoxResult result = MessageBox.Show("From now program will be monitoring your template", "Alert", MessageBoxButton.OK, MessageBoxImage.Question);
- textBox2.Text = openFileDialog.FileName;
- Paths.emailPath = openFileDialog.FileName;
- richTextBox.AppendText($"\nLoaded template {DateTime.Now}");
- templateWatcher.Path = System.IO.Path.GetDirectoryName(Paths.path);
- templateWatcher.NotifyFilter = NotifyFilters.LastWrite;
- templateWatcher.Filter = "*.*";
- templateWatcher.Changed += Template_Changed; ;
- templateWatcher.EnableRaisingEvents = true;
- var templateBuilder = new StringBuilder();
- using (var reader = new StreamReader(File.OpenRead(openFileDialog.FileName)))
- {
- while (!reader.EndOfStream)
- {
- templateBuilder.AppendLine(reader.ReadLine());
- }
- reader.Close();
- }
- Paths.emailBody = templateBuilder.ToString();
- }
- });
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement