SIGN UP MEMBER LOGIN:    
ARTICLE

Emailing With WPF Application

Posted by Shubham Srivastava Articles | WPF with C# February 08, 2012
Emailing is the one of the feature of .Net Framework. Previously Emailing is not possible in windows application instead of web application. But now .Net tools Visual studio 2010 contain two type of WPF application, one of them is supported browser based application.
Reader Level:

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:rrdev1987@gmail.com%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:rrdev1987@gmail.com%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

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Shubham. Its a very nice and good article for those who wants to learn how to perform emailing with WPF application.

Posted by Stephen Johnson Feb 09, 2012
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor