void sendButton_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(server.Text) || string.IsNullOrEmpty(from.Text) || string.IsNullOrEmpty(to.Text)) { MessageBox.Show("All the text boxes are required, fill them in please.", "Papercut", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if(!emailRegex.IsMatch(from.Text) || !emailRegex.IsMatch(to.Text)) { MessageBox.Show("You need to enter valid email addresses.", "Papercut", MessageBoxButton.OK, MessageBoxImage.Warning); return; } SmtpSession session = new SmtpSession(); session.Sender = server.Text.Trim(); session.MailFrom = from.Text; session.Recipients.Add(to.Text); session.Message = File.ReadAllBytes(messageFilename); worker = new BackgroundWorker(); worker.DoWork += delegate(object s, DoWorkEventArgs args) { SmtpSession _session = args.Argument as SmtpSession; SmtpClient client = new SmtpClient(_session); client.Send(); }; worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args) { // Save settings for the next time Settings.Default.ForwardServer = server.Text; Settings.Default.ForwardTo = to.Text; Settings.Default.ForwardFrom = from.Text; Settings.Default.Save(); working = false; sendingLabel.Visibility = Visibility.Hidden; DialogResult = true; }; working = true; sendButton.IsEnabled = false; sendingLabel.Visibility = Visibility.Visible; worker.RunWorkerAsync(session); }
/// <summary> /// The send button_ click. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void sendButton_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(this.server.Text) || string.IsNullOrEmpty([email protected]) || string.IsNullOrEmpty(this.to.Text)) { MessageBox.Show( "All the text boxes are required, fill them in please.", "Papercut", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (!emailRegex.IsMatch([email protected]) || !emailRegex.IsMatch(this.to.Text)) { MessageBox.Show( "You need to enter valid email addresses.", "Papercut", MessageBoxButton.OK, MessageBoxImage.Warning); return; } var session = new SmtpSession { Sender = this.server.Text.Trim(), MailFrom = [email protected] }; session.Recipients.Add(this.to.Text); session.Message = File.ReadAllBytes(this.messageFilename); this.worker = Task.Factory.StartNew( () => { using (var client = new SmtpClient(session)) { client.Send(); } }); this.worker.ContinueWith( (t) => { // Save settings for the next time Settings.Default.ForwardServer = this.server.Text; Settings.Default.ForwardTo = this.to.Text; Settings.Default.ForwardFrom = [email protected]; Settings.Default.Save(); this.working = false; this.sendingLabel.Visibility = Visibility.Hidden; this.DialogResult = true; }, TaskScheduler.FromCurrentSynchronizationContext()); this.working = true; this.sendButton.IsEnabled = false; this.sendingLabel.Visibility = Visibility.Visible; }