private void btnGo_Click(object sender, RoutedEventArgs e) { SantaSackSerializer.Serialize(santaSack); if (santaSack.Participants.Count < 3) { MessageBox.Show("You Haven't Got Enough People!"); return; } if (string.IsNullOrWhiteSpace(santaSack.Template.FromAddress)) { MessageBox.Show("You Haven't Entered A From Address!"); return; } if (string.IsNullOrWhiteSpace(santaSack.Template.Content)) { MessageBox.Show("You Haven't Got Any Email Content!"); return; } if (string.IsNullOrWhiteSpace(santaSack.Template.Subject)) { MessageBox.Show("You Haven't Got An Email Subject!"); return; } if (string.IsNullOrWhiteSpace(santaSack.Template.Host) || string.IsNullOrWhiteSpace(santaSack.Template.Port)) { MessageBox.Show("You Haven't Got A Host Or Port For Sending!"); return; } var emailHelper = new RegexUtilities(); if (!emailHelper.IsValidEmail(santaSack.Template.FromAddress)) { MessageBox.Show("You Haven't Entered A Valid From Address!"); return; } foreach (var participant in santaSack.Participants) { if (!emailHelper.IsValidEmail(participant.EmailAddress)) { MessageBox.Show(participant.Name + " Does Not Have A Valid Email Address!"); return; } } var rsltMessageBox = MessageBox.Show("Are You Sure You Want To Send Now?", "Are You Sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (rsltMessageBox == System.Windows.Forms.DialogResult.No) { MessageBox.Show("Sending Cancelled!"); return; } var participantsToPair = (from p in santaSack.Participants select p).ToList(); var participantsToAssign = (from p in santaSack.Participants select p).ToList(); var participantsToSend = new List <PairedParticipant>(); var randomGenerator = new Random((int)DateTime.UtcNow.Ticks); while (participantsToPair.Count > 0) { if (participantsToAssign.Count == 1 && participantsToPair.Count == 1 && participantsToAssign.First().EmailAddress == participantsToPair.First().EmailAddress) { participantsToPair = (from p in santaSack.Participants select p).ToList(); participantsToAssign = (from p in santaSack.Participants select p).ToList(); participantsToSend = new List <PairedParticipant>(); } Participant pairParticipant = null; var assignIndex = randomGenerator.Next(0, participantsToAssign.Count); var assignParticipant = participantsToAssign[assignIndex]; participantsToAssign.Remove(assignParticipant); var participantPairing = new PairedParticipant() { EmailAddress = assignParticipant.EmailAddress, Name = assignParticipant.Name }; while (pairParticipant == null) { var pairIndex = randomGenerator.Next(0, participantsToPair.Count); pairParticipant = participantsToPair[pairIndex]; if (assignParticipant.EmailAddress == pairParticipant.EmailAddress) { pairParticipant = null; } else { participantsToPair.Remove(pairParticipant); participantPairing.PairedWith = pairParticipant; participantsToSend.Add(participantPairing); } } var alreadyPaired = participantsToSend.FirstOrDefault(p => p.PairedWith.EmailAddress == participantPairing.EmailAddress); if (alreadyPaired != null) { if (alreadyPaired.EmailAddress == participantPairing.PairedWith.EmailAddress) { participantsToPair = (from p in santaSack.Participants select p).ToList(); participantsToAssign = (from p in santaSack.Participants select p).ToList(); participantsToSend = new List <PairedParticipant>(); } } } while (participantsToSend.Any(x => x.Sent == false)) { if (participantsToSend.Any(x => x.SendAttempts > 5)) { foreach (var tooManyAttemptParticipant in participantsToSend.Where(x => x.SendAttempts > 5)) { MessageBox.Show( string.Format("Couldn't Send Email To {0}, They Drew {1}", tooManyAttemptParticipant.EmailAddress, tooManyAttemptParticipant.PairedWith.EmailAddress), "Couldn't Send", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } foreach (var pairedParticipant in participantsToSend.Where(x => x.Sent == false)) { var message = new MailMessage(); message.From = new MailAddress(santaSack.Template.FromAddress); message.To.Add(!string.IsNullOrWhiteSpace(santaSack.Template.DiagnosticDeliveryAddress) ? new MailAddress(santaSack.Template.DiagnosticDeliveryAddress) : new MailAddress(pairedParticipant.EmailAddress)); // send to a bcc if no diagnostic delivery is set if (!string.IsNullOrWhiteSpace(santaSack.Template.BlindCarbonCopy) && string.IsNullOrWhiteSpace(santaSack.Template.DiagnosticDeliveryAddress)) { message.Bcc.Add(new MailAddress(santaSack.Template.BlindCarbonCopy)); } message.BodyEncoding = Encoding.UTF8; message.Subject = santaSack.Template.Subject; message.Body = santaSack.Template.Content .Replace("{{GiftFromName}}", pairedParticipant.Name) .Replace("{{GiftFromEmail}}", pairedParticipant.EmailAddress) .Replace("{{GiftForName}}", pairedParticipant.PairedWith.Name) .Replace("{{GiftForEmail}}", pairedParticipant.PairedWith.EmailAddress); var client = new SmtpClient { EnableSsl = santaSack.Template.EnableSsl }; if (!string.IsNullOrWhiteSpace(santaSack.Template.FromPassword)) { client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(santaSack.Template.FromAddress, santaSack.Template.FromPassword); client.DeliveryMethod = SmtpDeliveryMethod.Network; message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; } client.Host = santaSack.Template.Host; int port; if (!int.TryParse(santaSack.Template.Port, out port)) { port = 25; } client.Port = port; try { client.Send(message); pairedParticipant.Sent = true; } catch (Exception) { pairedParticipant.Sent = false; pairedParticipant.SendAttempts++; } } } MessageBox.Show("All Emails Have Been Sent!"); }
private void btnGo_Click(object sender, RoutedEventArgs e) { SantaSackSerializer.Serialize(santaSack); if (santaSack.Participants.Count < 3) { MessageBox.Show("You Haven't Got Enough People!"); return; } if (string.IsNullOrWhiteSpace(santaSack.Template.FromAddress)) { MessageBox.Show("You Haven't Entered A From Address!"); return; } if (string.IsNullOrWhiteSpace(santaSack.Template.Content)) { MessageBox.Show("You Haven't Got Any Email Content!"); return; } if (string.IsNullOrWhiteSpace(santaSack.Template.Subject)) { MessageBox.Show("You Haven't Got An Email Subject!"); return; } if (string.IsNullOrWhiteSpace(santaSack.Template.Host) || string.IsNullOrWhiteSpace(santaSack.Template.Port)) { MessageBox.Show("You Haven't Got A Host Or Port For Sending!"); return; } var emailHelper = new RegexUtilities(); if (!emailHelper.IsValidEmail(santaSack.Template.FromAddress)) { MessageBox.Show("You Haven't Entered A Valid From Address!"); return; } foreach (var participant in santaSack.Participants) { if (!emailHelper.IsValidEmail(participant.EmailAddress)) { MessageBox.Show(participant.Name + " Does Not Have A Valid Email Address!"); return; } } var rsltMessageBox = MessageBox.Show("Are You Sure You Want To Send Now?", "Are You Sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (rsltMessageBox == System.Windows.Forms.DialogResult.No) { MessageBox.Show("Sending Cancelled!"); return; } var participantsToPair = (from p in santaSack.Participants select p).ToList(); var participantsToAssign = (from p in santaSack.Participants select p).ToList(); var participantsToSend = new List<PairedParticipant>(); var randomGenerator = new Random((int)DateTime.UtcNow.Ticks); while (participantsToPair.Count > 0) { if (participantsToAssign.Count == 1 && participantsToPair.Count == 1 && participantsToAssign.First().EmailAddress == participantsToPair.First().EmailAddress) { participantsToPair = (from p in santaSack.Participants select p).ToList(); participantsToAssign = (from p in santaSack.Participants select p).ToList(); participantsToSend = new List<PairedParticipant>(); } Participant pairParticipant = null; var assignIndex = randomGenerator.Next(0, participantsToAssign.Count); var assignParticipant = participantsToAssign[assignIndex]; participantsToAssign.Remove(assignParticipant); var participantPairing = new PairedParticipant() { EmailAddress = assignParticipant.EmailAddress, Name = assignParticipant.Name }; while (pairParticipant == null) { var pairIndex = randomGenerator.Next(0, participantsToPair.Count); pairParticipant = participantsToPair[pairIndex]; if (assignParticipant.EmailAddress == pairParticipant.EmailAddress) { pairParticipant = null; } else { participantsToPair.Remove(pairParticipant); participantPairing.PairedWith = pairParticipant; participantsToSend.Add(participantPairing); } } var alreadyPaired = participantsToSend.FirstOrDefault(p => p.PairedWith.EmailAddress == participantPairing.EmailAddress); if (alreadyPaired != null) { if (alreadyPaired.EmailAddress == participantPairing.PairedWith.EmailAddress) { participantsToPair = (from p in santaSack.Participants select p).ToList(); participantsToAssign = (from p in santaSack.Participants select p).ToList(); participantsToSend = new List<PairedParticipant>(); } } } while (participantsToSend.Any(x => x.Sent == false)) { if (participantsToSend.Any(x => x.SendAttempts > 5)) { foreach (var tooManyAttemptParticipant in participantsToSend.Where(x => x.SendAttempts > 5)) { MessageBox.Show( string.Format("Couldn't Send Email To {0}, They Drew {1}", tooManyAttemptParticipant.EmailAddress, tooManyAttemptParticipant.PairedWith.EmailAddress), "Couldn't Send", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } foreach (var pairedParticipant in participantsToSend.Where(x => x.Sent == false)) { var message = new MailMessage(); message.From = new MailAddress(santaSack.Template.FromAddress); message.To.Add(!string.IsNullOrWhiteSpace(santaSack.Template.DiagnosticDeliveryAddress) ? new MailAddress(santaSack.Template.DiagnosticDeliveryAddress) : new MailAddress(pairedParticipant.EmailAddress)); // send to a bcc if no diagnostic delivery is set if (!string.IsNullOrWhiteSpace(santaSack.Template.BlindCarbonCopy) && string.IsNullOrWhiteSpace(santaSack.Template.DiagnosticDeliveryAddress)) { message.Bcc.Add(new MailAddress(santaSack.Template.BlindCarbonCopy)); } message.BodyEncoding = Encoding.UTF8; message.Subject = santaSack.Template.Subject; message.Body = santaSack.Template.Content .Replace("{{GiftFromName}}", pairedParticipant.Name) .Replace("{{GiftFromEmail}}", pairedParticipant.EmailAddress) .Replace("{{GiftForName}}", pairedParticipant.PairedWith.Name) .Replace("{{GiftForEmail}}", pairedParticipant.PairedWith.EmailAddress); var client = new SmtpClient { EnableSsl = santaSack.Template.EnableSsl }; if (!string.IsNullOrWhiteSpace(santaSack.Template.FromPassword)) { client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(santaSack.Template.FromAddress, santaSack.Template.FromPassword); client.DeliveryMethod = SmtpDeliveryMethod.Network; message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; } client.Host = santaSack.Template.Host; int port; if (!int.TryParse(santaSack.Template.Port, out port)) { port = 25; } client.Port = port; try { client.Send(message); pairedParticipant.Sent = true; } catch (Exception) { pairedParticipant.Sent = false; pairedParticipant.SendAttempts++; } } } MessageBox.Show("All Emails Have Been Sent!"); }