Microsoft provides us functionality to send email using C# code. We can use System.Net and System.Net.Sockets namespace to send email. Basically these namespace is responsible for sending the data and receiving the data over internet. SMTP protocol is used for sending the emails. SMTP stands for Simple Mail Transfer Protocol.

Today, I am going to show, how to create an WPF application to send emails using gmail. Gmail always uses SSL/TLS authentication, so to send email you have to enable SSL property for the port 587 or 465.

I have app.config to store the secure data and fetch when required it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
using System.Configuration;
using System.Net.Mail;
using System.Net;

namespace EmailSendExample
    public partial class MainWindow : Window
        public MainWindow()
        private void btnSend_Click(object sender, RoutedEventArgs e)
            int flag = 0;

            if (txtTo.Text.Trim().Length == 0)
                flag = 0;
                txtbTo.Text = "Required";
            else if (!Regex.IsMatch(txtTo.Text, @"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}"))
                flag = 0;
                txtbTo.Text = "Invalid";
                flag = 1;
                txtbTo.Text = "";                
            if (txtSubject.Text.Trim().Length == 0)
                flag = 0;
                txtbSubject.Text = "Required";
            if (txtContent.Text.Trim().Length == 0)
                flag = 0;
                txtbContent.Text = "Required";
            if (flag == 1)
                var smtpServerName = ConfigurationManager.AppSettings["SmtpServer"];
                var port = ConfigurationManager.AppSettings["Port"];
                var senderEmailId = ConfigurationManager.AppSettings["SenderEmailId"];
                var senderPassword = ConfigurationManager.AppSettings["SenderPassword"];                

                var smptClient = new SmtpClient(smtpServerName, Convert.ToInt32(port))
                    Credentials = new NetworkCredential(senderEmailId, senderPassword),
                    EnableSsl = true
                smptClient.Send(senderEmailId, txtTo.Text.Trim(), txtSubject.Text, txtContent.Text);
                MessageBox.Show("Message Sent Successfully");
                txtTo.Text = "";
                txtSubject.Text = "";
                txtContent.Text = "";
        private void btnReset_Click(object sender, RoutedEventArgs e)
            txtTo.Text = "";
            txtSubject.Text = "";
            txtContent.Text = "";

            txtbTo.Text = "";
            txtbSubject.Text = "";
            txtbContent.Text = "";
<Window x:Class="EmailSendExample.MainWindow"
        Title="Email Send Example" Height="378.947" Width="559.211">
    <Grid Margin="-2,10,-28,-51">
            <ColumnDefinition Width="41*"/>
            <ColumnDefinition Width="540*"/>
        <Label Content="Enter details to send email" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="1" Margin="146,0,0,0"/>
        <Label Content="To" HorizontalAlignment="Left" Margin="30,38,0,0" VerticalAlignment="Top" FontWeight="Bold" Grid.ColumnSpan="2"/>
        <TextBox HorizontalAlignment="Left" Name="txtTo"  Height="23" Margin="67.579,41,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="350" Grid.Column="1"/>
        <Label Content="Subject" HorizontalAlignment="Left" Margin="30,79,0,0" VerticalAlignment="Top" FontWeight="Bold" Grid.ColumnSpan="2"/>
        <TextBox HorizontalAlignment="Left" x:Name="txtSubject"  Height="23" Margin="67.579,82,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="350" Grid.Column="1"/>
        <Label Content="Content" HorizontalAlignment="Left" Margin="30,124,0,0" VerticalAlignment="Top" FontWeight="Bold" Grid.ColumnSpan="2"/>
        <TextBox HorizontalAlignment="Left" x:Name="txtContent"  Height="124" Margin="67.579,127,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="350" Grid.Column="1"/>
        <Button Content="Send" HorizontalAlignment="Left" Margin="91.579,276,0,0" VerticalAlignment="Top" Width="75" Name="btnSend" Click="btnSend_Click" Grid.Column="1"/>
        <Button Content="Reset" HorizontalAlignment="Left" Margin="191.579,276,0,0" VerticalAlignment="Top" Width="75" Name="btnReset" Click="btnReset_Click" Grid.Column="1"/>
        <TextBlock HorizontalAlignment="Left" Name="txtbTo" Foreground="red" Margin="422.579,43,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" RenderTransformOrigin="1.207,3.224" Grid.Column="1"/>
        <TextBlock HorizontalAlignment="Left" x:Name="txtbSubject" Foreground="red" Margin="422.579,84,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" RenderTransformOrigin="1.207,3.224" Grid.Column="1"/>
        <TextBlock HorizontalAlignment="Left" x:Name="txtbContent" Foreground="red" Margin="422.579,137,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" RenderTransformOrigin="1.207,3.224" Grid.Column="1"/>

<?xml version="1.0" encoding="utf-8" ?>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    <add key="SmtpServer" value=""/>
    <add key="Port" value="587"/>
    <add key="SenderEmailId" value=""/>
    <add key="SenderPassword" value="xxxxxx"/>    

So, today we learned how to create a WPF application and using this how to send email. We also learned how to validate a field if required.

I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks