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

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

{

publicpartialclassMainPage : UserControl

{

public MainPage()

{

InitializeComponent();

}

privatevoid btnCancel_Click(object sender, RoutedEventArgs e)

{

}

privatevoid 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.

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

About the Author

Full Name: Vuyiswa MasekoMember Level: NotApplicable Member Status: Member,MVP,AdministratorMember Since: 7/6/2008 11:50:44 PMCountry: South Africa Thank you for posting at Dotnetfunda
[Administrator] http://www.Dotnetfunda.com
Vuyiswa Junius Maseko is a Founder of Vimalsoft (Pty) Ltd (http://www.vimalsoft.com/) and a forum moderator at www.DotnetFunda. Vuyiswa has been developing for 16 years now. his major strength are C# 1.1,2.0,3.0,3.5,4.0,4.5 and vb.net and sql and his interest were in asp.net, c#, Silverlight,wpf,wcf, wwf and now his interests are in Kinect for Windows,Unity 3D. He has been using .net since the beta version of it. Vuyiswa believes that Kinect and Hololen is the next generation of computing.Thanks to people like Chris Maunder (codeproject), Colin Angus Mackay (codeproject), Dave Kreskowiak (Codeproject), Sheo Narayan (.Netfunda),Rajesh Kumar(Microsoft) They have made vuyiswa what he is today.