[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Send secret Santa emails in C#

[Send secret Santa emails in C#]

This example shows how you can automatically send secret Santa emails for a party. The example Pick a secret Santa assignment in C# shows how you can quickly make secret Santa assignments. The post Calculate the number of secret Santa permutations in C# explains how many possible assignments there are and shows that you won't have to try too many random assignments before you find a good one.

Those examples have a big drawback, however: They require a neutral person to give everyone their assignments. Someone needs to use the first example to randomly generate the secret Santa assignments and then hand them out to everyone who will attend the party. Unfortunately that person knows to whom each person is assigned, and that removes the anonymity.

In practice, this doesn't matter much. The party's host can just play that role and not worry about it. However, as programmers we should be able to come up with a solution.

This example generates a secret Santa assignment and then emails everyone their assignments. This program plays the role of the person who hands out the assignments. Here's the code.

// Send the message. private void btnSend_Click(object sender, EventArgs e) { lstMessages.Items.Clear(); Cursor = Cursors.WaitCursor; btnSend.Enabled = false; Refresh(); // Get email parameters. string from_name = txtFromName.Text; string from_email = txtFromEmail.Text; string host = txtHost.Text; int port = int.Parse(txtPort.Text); bool enable_ssl = chkEnableSSL.Checked; string password = txtPassword.Text; string subject = "Secret Santa assignment"; // Get the recipients. char[] separators = { '\n', '\r' }; string[] people = txtPeople.Text.Split(separators, StringSplitOptions.RemoveEmptyEntries); // Get the secret Santa assignments. int num_people = people.Length; int num_tries; int[] assignments = SecretSantaAssignment(num_people, out num_tries); // Send the emails. try { for (int i = 0; i < num_people; i++) { // Send people[i] his or her assignment. string to_name = people[i].Split(';')[0]; string to_email = people[i].Split(';')[1]; string body = "Your Secret Santa assignment is " + people[assignments[i]]; Console.WriteLine( "Sending message to " + to_name); lstMessages.Items.Add( "Sending message to " + to_name); lstMessages.Refresh(); Application.DoEvents(); SendEmail(to_name, to_email, from_name, from_email, host, port, enable_ssl, password, subject, body); } MessageBox.Show("Sent " + num_people + " messages"); } catch (Exception ex) { MessageBox.Show(ex.Message); } Cursor = Cursors.Default; }

The program first gets the email parameters: from name, from email, email host and port, SSL enable, and password. It sets the email message's subject to "Secret Santa assignment."

Next the program gets the list of recipients. This should be a list of names and emails separated by semi-colons with one name and email per line.

The program then calls the SecretSantaAssignment method to get a valid assignment. See Pick a secret Santa assignment in C# for information about how that works.

Next the code loops through the people and sends them their assignments. It finds each person's name and email address, and composes a message telling each person his or her assignment. It then calls the SendEmail method to send the email. For information about that method, see Send email in C#

Be sure you get the name/email list right! If you don't, you may end up sending several emails before the program fails. Then you'll have to start over and generate new assignments. Try to get it right the first time so you don't have to explain to people why some of them are getting multiple emails.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.