[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 email in C#

[Send email in C#]

This example shows how you can send email from a C# program. In recent years this has become harder because there are few email providers willing to let you send anonymous emails. Now to send an email, you need to create a NetworkCredential object that specifies your email server, user name, and password. Basically you have to log in to your mail server so your provider knows you aren't sending out millions of spam messages.

This example uses the following SendEmail method to send an email.

using System.Net; using System.Net.Mail; // Send an email message. private void SendEmail(string to_name, string to_email, string from_name, string from_email, string host, int port, bool enable_ssl, string password, string subject, string body) { // Make the mail message. MailAddress from_address = new MailAddress(from_email, from_name); MailAddress to_address = new MailAddress(to_email, to_name); MailMessage message = new MailMessage(from_address, to_address); message.Subject = subject; message.Body = body; // Get the SMTP client. SmtpClient client = new SmtpClient() { Host = host, Port = port, EnableSsl = enable_ssl, UseDefaultCredentials = false, Credentials = new NetworkCredential( from_address.Address, password), }; // Send the message. client.Send(message); }

The method creates MailAddress objects to represent the From and To email addresses. You can use strings for these when you make the MailMessage but then you can't associate a human-friendly name (like "Rod Stephens") with the email addresses.

Next the program creates a MailMessage, passing its constructor the From and To addresses. The code then sets the MailMessage object's Subject and Body fields.

The method then creates an SmtpClient object. It sets the client's host and port so it knows where to send the email. The method sets the EnableSsl property according to the value it was passed as a parameter. If your email server uses SSL (Secure Sockets Layer--Gmail uses this), check the box on the form so this is set to true.

The code also sets UseDefaultCredentials to false and sets the client's Credentials property to a new NetworkCredential object containing your email user name and password.

Finally the method calls the SmtpClient object's Send method to send the email.

The System.Net.Mail.MailMessage class supports some other features that you might want to use. For example, it has CC and Bcc properties that let you send courtesy copies and blind courtesy copies. The version shown here is good enough for now.

The following code shows how the program calls the SendEmail method.

// Send the message. private void btnSend_Click(object sender, EventArgs e) { try { SendEmail(txtToName.Text, txtToEmail.Text, txtFromName.Text, txtFromEmail.Text, txtHost.Text, int.Parse(txtPort.Text), chkEnableSSL.Checked, txtPassword.Text, txtSubject.Text, txtBody.Text); MessageBox.Show("Message sent"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }

This code simply calls SendEmail passing it the values you entered on the form.

Note that the form's initial values for host and port (smtp.live.com and 587) work for Hotmail. If you want to use some other email host, you'll need to change those values. (For example, Gmail uses smtp.gmail.com and port 587, although it seems to have recently started requiring you to enable an application to use Gmail if you have two-step verification enabled. That's more secure, but a bit of a hassle so I haven't bothered.)

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

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