Welcome again to Dotnetfunda. This is the last part of this series. The first part of the article , i demonstrated on how to create a wcf service that will be consumed by our silverlight application.
Introduction
Welcome again to Dotnetfunda. This is the last part of this series. The first part of the article , I demostrated on how to create a wcf service that will be consumed by our silverlight application.
Background
In this Article we are going explain how you can send an email from your silverlight appplication, with gmail or other email accounts that you know the smtp and port.
Using the code
We are going to user C# as our language and we will have some xaml to build our UI.
Start
If we go back to our previous article, you will remember that we had build a silverlight interface and now we are going to use it in this article. The first thing we need to create a service reference to our serive that we created in the previous article. Open your Project and let us get started.
The First part is to add a Service Reference to our Silverlight Project. Right click on your Silverlight Project as depicted below

and click Add Service Reference as I did in the above example. A dialog box that looks like this one will appear

Now normally you will find it empty, if you click on the Discover button, it will look for services that are hosted on your machine or services that are in your solution. As you can see in my solution I have one servce, the service that we created in the part one of this article. In the name space is the name of the namespace that you will use when you instatiate the service, give it a Proper name as I did , mine is called “Mailer” and Click ok and something in your solution explorer will change , you need notice that you will have the following

Now as you can see you will have a folder created and that will have your service reference in it. Now that we have referenced our service, let us call the function that we created elier , plase not that if you did not read the first article, you will get lost in this one, better go back and read the part one of this article here.

From the above example , you can see we have only two buttons, so our xaml had events linked to them , its better I share the xaml with you again so that you will not get lost. Your xaml will look like this
<UserControl x:Class="ContactUs.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="800">
<Grid x:Name="LayoutRoot" Margin="10">
<Canvas>
<Grid>
<TextBlock x:Name="lblname" Text="Name:" TextWrapping="Wrap" Foreground="Black" Margin="64,119,606,241" Height="20" Width="111" />
<TextBox x:Name="txtname" TextWrapping="Wrap" Margin="230,115,355,242" />
<TextBlock x:Name="lblemail" Text="Email:" TextWrapping="Wrap" Foreground="Black" Margin="64,158,610,182" Height="39" Width="106" />
<TextBox x:Name="txtemail" TextWrapping="Wrap" Width="197" Margin="231,154,352,204" />
<TextBlock x:Name="lblComment" Text="Comments:" TextWrapping="Wrap" Foreground="Black" Margin="64,197,605,146" Height="37" Width="111" />
<TextBox x:Name="txtComment" TextWrapping="Wrap" Margin="234,197,352,120" />
<Button Height="33" x:Name="btnSumit" Foreground="Black" Content="Submit" Width="80" Click="btnSumit_Click" ToolTipService.ToolTip="Send us your Message" Margin="231,281,469,66" />
<Button x:Name="btnCancel" Content="Close" Click="btnCancel_Click" d:LayoutOverrides="GridBox" ToolTipService.ToolTip="Close" Margin="334,283,0,67" HorizontalAlignment="Left" Width="91" />
</Grid>
</Canvas>
</Grid>
</UserControl>
As you can see the buttons have events wired to them , what you need is to make sure that those events exist on the server side code. Current my cs file looks like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace ContactUs
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
}
private void btnSumit_Click(object sender, RoutedEventArgs e)
{
}
}
}
Now let us add life to this code. Copy this code and paste it in your project and I will explain it briefly . The first thing that you must note is that Silverlight does not support synchronous network operations. You need to create a handler for your function in your service. Asynchronous operations allow for a client to send a request to the server and have the server callback to the client when the operation is complete. Below is your code.
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
namespace ContactUs
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
}
private void btnSumit_Click(object sender, RoutedEventArgs e)
{
Mailer.IemailClient obj = new ContactUs.Mailer.IemailClient();
obj.SendEmailAsync(txtname.Text, txtemail.Text, "0777777777", "vuyiswamb@gmail.com", 1, txtComment.Text, "WebSite Messages");
obj.OpenCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(obj_OpenCompleted);
}
void obj_OpenCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
MessageBox.Show("Email Sent");
}
}
}
When done, compile your code and run your application , you should see something like this

And when you click send it will send an email to the email configured to receive an email and on my case when I sent it , my gtalk did the trick again as depicted below

And the email I reiceved is this one

Conclusion
As you can see that our journey ends here. I promise to share more on silverlight when time goes on. I have not written a strong N-Tier Architecture article here on DNF , I promise to bring a good one in silverlight. Thank you again for supporting our site Dotnetfunda.
Thank you for visiting DotnetFunda.
Vuyiswa Maseko