Development of a messaging application with Silverlight 2 Beta 2

0

Sending email notifications from a contact or a website comment page has become a standard feature of most websites.

Today I’m going to show you how to create an ASP.NET Silverlight application that sends emails. The main objective of this tutorial is to accomplish this goal by using an ASP.NET web service that hosts a Silverlight client. I’m assuming you have some basic C # knowledge and are familiar with the Visual Studio environment. You will need the following tools to be installed:

You will first need to create an interface on how you want your app to look. Since our objective today is more towards development (coding aspect) and not on design, you will therefore have to design an interface for your application yourself. You can design your interface in Expression Blend or you can just use the designer in Visual Studio 2008. The designer in Visual Studio 2008 is not good at all, so I highly recommend you design it using Blend. Create an interface that looks like this:

Now run VS08 Visual Studio 2008 to create a new Silverlight application and name it “SendEmail”. Select “Add a new website to the hosting solution” when prompted. Now right click on the ASP.NET “SendEmailWeb” project and click “Add New Item”. After that just add a new web service and name it “MailServ.asmx”

Now open MailServ.cs from the App_Code folder included in the ASP.NET “SendEmailWeb” project and write the following code in it.

   2:      public bool Send(string fromEmail, string toEmail, string subject,
           string body)
   6:              MailMessage message = new MailMessage();
   7:              message.From = new MailAddress(fromEmail);
   8:              message.To.Add(new MailAddress(toEmail));
   9:              message.Subject = subject;
  11:              message.IsBodyHtml = false;
  13:              SmtpClient smtp = new SmtpClient();
  14:              smtp.EnableSsl = true;
 

Note: Please include the System.Net.Mail namespace and modify the WebService namespace to match your domain name.

After that open web.config and configure your SMTP server, port, username and password. I created an “admin” account and configured it on the “redmondpie” server, you can modify this configuration according to your account.

   4:          "smtp.redmondpie.com" port="587" userName="admin"
                password="1234"/>

So when you’re done, add a service reference to a Silverlight (SendEmail) project by right-clicking on it.

Finally, just write the following code in Page.xaml.cs in order to call the web service from Silverlight.

   1:         void sendButton_Click(object sender, RoutedEventArgs e)
   3:              BasicHttpBinding bind = new BasicHttpBinding();
   4:              EndpointAddress endpoint = new EndpointAddress(
                          "http://localhost:51639/SendEmailWeb/MailServ.asmx");
   6:              Proxy.MailServSoapClient mailService = new 
                            SendEmail.Proxy.MailServSoapClient(bind, endpoint);
   8:              mailService.SendAsync(fromEmailTextBox.Text, 
                    toEmailTextbox.Text, subjectTextbox.Text, bodyTextbox.Text);
   9:              mailService.SendCompleted += new EventHandler                      .Proxy.SendCompletedEventArgs>(mailService_SendCompleted);

  13:          void mailService_SendCompleted(object sender, SendEmail.Proxy

                                                   .SendCompletedEventArgs e)

  17:                  resultTextBlock.Foreground = new SolidColorBrush(Colors

                                                                         .Blue);

  18:                  resultTextBlock.Text = "Your email has been sent 

successfully!"

;
  19:                  resultTextBlock.Visibility = Visibility.Visible;
  23:                  resultTextBlock.Text = "Sending failed.";
  24:                  resultTextBlock.Foreground = new SolidColorBrush(Colors.Red);
  25:                  resultTextBlock.Visibility = Visibility.Visible;

That’s it! you can now compile the application and run it in your browser to check the results. Good coding !!! ??

Related stories

Like this post on Facebook


Source link

Share.

Leave A Reply