Send Email From C# WPF Application Using HTML Email Templates

Introduction

This article explains how to send email from a C# WPF Windows Application using HTML Email Templates.

Using:

  • HTML Email Templates
  • MySQL Database
  • WPF ApplicationFrom

Step 1: Design the HTML page with the following markup: “Event.Html”.

Design the Html Page

Integrated run HTML previewing pages “Event.Html”.

HTML Previewing pages

Step 2:
Open MySQLAdmin Page then select "Create A New Table”  -> "View" -> ”Table Structure for Table `Event`”.

CREATETABLEIFNOTEXISTS`Event`(

  `Firstname` Varchar(100)NOTNULL,

  `Lastname` Varchar(100)NOTNULL,

  `Email` Varchar(25)NOTNULL,

  `Mobile` Varchar(12)NOTNULL

)ENGINE=InnodbDEFAULTCHARSET=Latin1;

Step 3: View Table > “Event”.

Table

Step 4: Create a new project using "File" > "New" > "Project..." > "WPF Application".

Create A New Project

Step 5: Now -> ”Toolbox” -> “Select” .

  • 1 Button
  • 2 Grid
  • 5 Text block
  • 4 TextBox

Toolbox 

Step 6: Now design your MainWindow.xaml View design part; use the following code:
 

<Grid Height="350"Width="500"Background="#FFD1F9EE" >

<TextblockHeight="32"Horizontalalignment="Left"Margin="29,15,0,0"

Name="Textblockheading"
Text="Registration And Send Email"Verticalalignment="Top"Width="310"
Fontsize="20"Fontstretch="Normal"/>

<Grid Margin="31,0,29,95"Background="White"Height="180"

Verticalalignment="Bottom">

<Grid.Rowdefinitions>

<RowdefinitionHeight="250*" />

</Grid.Rowdefinitions>

<TextblockHeight="25"Horizontalalignment="Left"Margin="67,20,0,0"

Name="Textblockfirstname"Text="First Name:"Verticalalignment="Top"Width="110" />

<TextblockHeight="25"Horizontalalignment="Left"Margin="67,50,0,0"

Name="Textblocklastname"Text="Last Name:"Verticalalignment="Top"Width="110" />

<TextblockHeight="25"Horizontalalignment="Left"Margin="67,80,0,0"

Name="Textblockemailid"Text="Emailid"Verticalalignment="Top"Width="110" />

<TextblockHeight="25"Horizontalalignment="Left"Margin="67,110,0,0"

Name="Textblockmobile"Text="Mobile"Verticalalignment="Top"Width="110" />

 

<TextboxHeight="25"Horizontalalignment="Left"Margin="183,20,0,0"

Name="Txtfirstname"Verticalalignment="Top"Width="222" />

<TextboxHeight="25"Horizontalalignment="Left"Margin="183,50,0,0"

Name="Txtlastname"Verticalalignment="Top"Width="222" />

<TextboxHeight="25"Horizontalalignment="Left"Margin="183,80,0,0"

Name="Txtemail"Verticalalignment="Top"Width="222" />

<TextboxHeight="25"Horizontalalignment="Left"Margin="183,110,0,0"

Name="Txtmobile"Verticalalignment="Top"Width="222" />

<Button Content="Registration"Height="25"Horizontalalignment="Left"Margin="183,145,0,0"

Name="Btnregistration"Verticalalignment="Top"Width="100"Click="Btnregistration_Click" />

</Grid>

<TextblockHeight="20"Horizontalalignment="Left"Margin="215,46,0,0"

X:Name="Textblockmessage"Verticalalignment="Top"Width="247"
Opacitymask="Red"Foreground="#FFE5572C" />

</Grid>

Step 7:
You will get the MainWindow.xaml window form.

window form

Step 8: Use the following namespaces:

UsingSystem.Data;

UsingSystem.Drawing;

UsingSystem.Net;

UsingSystem.Configuration;

UsingSystem.Net.Mail;

UsingSystem.Text.Regularexpressions;

UsingMysql.Data.Mysqlclient;

Step 9: Use the following code for the Email:

Send Email code

Step 10:
Use the following for “btnregistration_Click":

Send Email
code

Step 11: Run the application and select “Registration Window Show”.

Run the Application

Step 12:
“Registered Successfully”:

Registered Successfully

Step 13: Now seelct "Gmail Account Open" - > "View Email".

View Email on Gmail acount

Now design the HTML PageEmail Templates, Create a MySQL table and send email from the C# WPF Windows Application using HTML Email Templates. I hope you enjoyed this article.

Next Recommended Readings