Silverlight 4 and COM: Sending Mails Using Outlook

In this post we will see how to access Microsoft Outlook for sending an Email using Silverlight and COM object. To start with, let’s create a Silverlight 4.0 application using Microsoft Visual Studio 2010.
Open Visual Studio 2010 and then create a new Silverlight application as shown below –
New Silverlight Project

Name the project as ‘OutLookExample’ and click on the ‘OK’ button. You will be asked to select a ‘Web Project type’ and Silverlight version. Let’s choose ‘ASP.NET web site’ from Web project type and choose Silverlight 4.0 from ‘Silverlight version’ option, as shown below –
Silverlight project Type

Now click the ‘OK’ button which will create our Silverlight application. Let’s design the screen as shown below –

Copy paste the following code in your MainPage.xaml -

<Grid x:Name="LayoutRoot" Background="Black">
            <RowDefinition Height="44*" />
            <RowDefinition Height="44*" />
            <RowDefinition Height="44*" />
            <RowDefinition Height="170*" />
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <TextBlock Text="TO - :" Foreground="Yellow" FontSize="14"
        HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBox x:Name="txtTO" Width="350" Height="30"/>
        <StackPanel Orientation="Horizontal" Grid.Row="1">
            <TextBlock Text="CC - :" Foreground="Yellow" FontSize="14"
        HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBox x:Name="txtCC" Width="350" Height="30"/>
        <StackPanel Orientation="Horizontal" Grid.Row="2">
            <TextBlock Text="Subject - :" Foreground="Yellow" FontSize="14"
        HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBox x:Name="txtsubject" Width="315" Height="30"/>
        <StackPanel Orientation="Vertical" Grid.Row="3">
            <TextBox x:Name="txtmessage" Width="400" Height="120"
        HorizontalAlignment="Left" VerticalAlignment="Top" />
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <Button x:Name="btnSend" Content="Send" Height="30" Width="100"
                <Button x:Name="btncancel" Content="Cancel" Height="30" Width="100"

First import a namespace  ‘using System.Runtime.InteropServices.Automation;’. Also add a reference to ‘Microsoft.CSharp.dll’ from the path ‘C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client’.
Now write the following code in the click event of the Send Button –
Silverlight Outlook

Now right click the Silverlight project and click on ‘Properties’. From the properties window check the Checkbox – ‘Enable running application out of browser’ and click on the button called ‘Out-Of-Browser settings’. This will show you a dialog box. Check the checkbox ‘Require evaluated trust when running outside the browser’ as shown below –

Silverlight Elevated Trust

Now hit ‘F5’, right click the Silverlight application on the browser and install the application as shown below –


You will see a security warning dialog box. Install the application and write the information to send the mail and click on the ‘Send’ button. This will open Outlook which will display our mail as shown below –

Silverlight Outlook Example

On the button click we have written a code which will take the address of the mail recipient, CC, Subject and body. We are also adding an attachment. I have written a fixed file path. Alternatively, you can use Open File dialog class to browse the files.
Download the source code

1 comment:

amit said...

mail is not going