Emailing With WPF Application


Introduction

Emailing is one of many features of the .Net Framework. Previously Emailing was not possible in Windows applications instead of web applications. But now in .Net tools Visual studio 2010 contains two types of WPFapplication, one of them is a supported browser based application. So now we are able to do emailing in a Windows application. First of all we have to import the namespace System.Net.Mail to run this application the proper way. To create this application we will use three classes MailMessage, System.Net.NetworkCredential, SmtpClient class.

Step 1: Open Visual Studio.

  • Select new -> project
  • Select your preferred language
  • Select a WPF application
  • Name the project
  • Now name of project is "WpfEmailing"

email0.gif

email1.gif

Step  2 : Open the Toolbox of WPF application.

  • Drag & Drop four Label control on design view.
  • Drag & Drop four TextBox control on design view.
  • Drag & Drop a Button control on design view.

email2.gif

email3.gif

Step 3 : Now, the final source code of the XAML page is given below:

Code

<Page x:Class="WpfEmailing.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      mc:Ignorable="d"
      d:DesignHeight="300" d:DesignWidth="300"
      Title="Page1">
    <Grid>
        <Button Content="Send" Height="23" HorizontalAlignment="Left" Margin="181,256,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <Label Content="To" Height="28" HorizontalAlignment="Left" Margin="24,16,0,0" Name="label1" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="110,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
        <Label Content="From" Height="28" HorizontalAlignment="Left" Margin="24,50,0,0" Name="label2" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="110,52,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
        <Label Content="Subject" Height="28" HorizontalAlignment="Left" Margin="28,89,0,0" Name="label3" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="110,94,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
        <Label Content="Body" Height="28" HorizontalAlignment="Left" Margin="28,123,0,0" Name="label4" VerticalAlignment="Top" />
        <TextBox Height="68" HorizontalAlignment="Left" Margin="71,154,0,0" Name="textBox4" VerticalAlignment="Top" Width="217" />
    </Grid>
</
Page>

Step 4 : Now, the send button click code is in the Page1.xaml.cs file.

Code

private void button1_Click(object sender, RoutedEventArgs e)
        {
            MailMessage mail = new MailMessage(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
            System.Net.NetworkCredential auth = new System.Net.NetworkCredential("mailto:[email protected]%22,%20%22srivastava1987, "srivastava1987");
            SmtpClient client = new SmtpClient("gmail.com", 587);
            client.EnableSsl = false;
            client.UseDefaultCredentials = false;
            mail.IsBodyHtml = true;
            client.Credentials = auth;
            client.Send(mail);

       }

Step 5  : Now, the final source code in Page1.xaml.cs file.

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.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Net.Mail;
 
namespace WpfEmailing
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page

    {
       
        public Page1()
        {
            InitializeComponent();
        }
 
      private void button1_Click(object sender, RoutedEventArgs e)
        {           
            MailMessage mail = new MailMessage(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
            System.Net.NetworkCredential auth = new System.Net.NetworkCredential("mailto:[email protected]%22,%20%22srivastava1987, "srivastava1987");
            SmtpClient client = new SmtpClient("gmail.com", 587);
            client.EnableSsl = false;
            client.UseDefaultCredentials = false;
            mail.IsBodyHtml = true;
            client.Credentials = auth;
            client.Send(mail);
        }
    }
}

Output

email4.gif

Resources