Sending a Simple Email Using SmtpClient in C#

Introduction

Sending an email is very easy in C#, this article provides an example of how to send email in WPF. This article uses the SmtpClient class, MailMessage class and MailAddress class. In the constructor of the MailAddress class an email address and display name of the user is provided. The MailMessage constructor take two objects of the MailAddress class as the sender address and receiver address. SmtpClient sends and receives email. This article uses the Send method of the SmptClient class.

There are only four steps to send a simple email using the SmtpClient class.

Procedure

The following is the procedure to send a simple email:

  1. Specify the name of the SMTP Server
  2. Provide specific credentials to SmptServer
  3. Create the e-mail message.
  4. Send the e-mail message


mail.jpg

Use the following controls in your WPF application. They will help you to send the message.

SendMail.jpg

Ensure your have imported the namespaces "System.Net.Mail" and "System.Net". If not imported in the Solution Explorer of Visual Studio then right-click on your project and click on "Add Reference".

SendMail1.bmp


The Add reference window will open, select "System.Net" than click the "OK" Button.

2.jpg


Import the following two namespaces:

using System.Net.Mail;
using System.Net;

Create an object of the SmtpClient class, set the server host name to the Host property and set the server port to the port property of Smtpclient. The following code uses 81 as the port as an example.

//created object of SmtpClient details and provides server details
SmtpClient MyServer = new SmtpClient();
MyServer.Host = "";
MyServer.Port = 81;

Create an object of the NetworkCredential class to provide the credential details to the object of SmtpClient class. Set the user name and password of the NetworkCredential class and provide this object to SmtpClient Credential.

//Server Credentials
NetworkCredential NC=new NetworkCredential();
NC.UserName="";
NC.Password="";
//assigned credetial details to server
MyServer.Credentials = NC;

Create two objects of the MailAddress class for the Sender address and Receiver address. MailAddress as a minimum takes two parameters to create an address, one is the email address and the other their display name, this the following sample provides two parameters, the sender address and their display name, again the same thing as done for the receiver address.

//create sender address
MailAddress from = new MailAddress("Sender Address", "Name want to display");

//create receiver address
MailAddress receiver = new MailAddress(txtToAddress.Text.Trim(), "Name want to display");

Now create an object of the MailMessage class. Here two parameters are provided to the consctructor of the MailMessage class, one is the sender's address and the second is the receiver's address. The Subject text from the txtSubject control is passed to to Subject and mail's content is passed to Body of the MailMessage class as in the following:

MailMessage Mymessage = new MailMessage(from, receiver);
Mymessage.Subject = txtSubject.Text.Trim();
Mymessage.Body = txtDocument.Text.Trim();


Finally the send method of the SmtpClinet class is called to send the email.

//sends the email
MyServer.Send(Mymessage);

Full code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
using System.Net.Mail;
using System.Net;

namespace MyRND_in_WPF
{
    /// <summary>
    /// Interaction logic for SendMail.xaml
    /// </summary>
    public partial class SendMail : Window
    {
        public SendMail()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            //created object of SmtpClient details and provides server details
            SmtpClient MyServer = new SmtpClient();
            MyServer.Host = "";
            MyServer.Port = 81;

            //Server Credentials
            NetworkCredential NC=new NetworkCredential();
            NC.UserName="";
            NC.Password="";
            //assigned credetial details to server
            MyServer.Credentials = NC;

            //create sender address
            MailAddress from = new MailAddress("Sender Address", "Name want to display");

            //create receiver address
            MailAddress receiver = new MailAddress(txtToAddress.Text.Trim(), "Name want to display");

            MailMessage Mymessage = new MailMessage(from, receiver);
            Mymessage.Subject = txtSubject.Text.Trim();
            Mymessage.Body = txtDocument.Text.Trim();
            //sends the email
            MyServer.Send(Mymessage);
        }
    }

}

Up Next
    Ebook Download
    View all
    Learn
    View all