Share Attachments And Compose Email In Windows Runtime Apps

Here are the steps, 

Step 1:

Create a simple Windows Project. Click New Project, Visual C#, Windows 8, Windows, then click Blank App (Windows 8.1).

new

Step 2:

Lets add a button in MainPage.xaml:
  • A button with the Click event.

    Complete XAML code is:
    1. <Page  
    2.     x:Class="ShareAttachment.MainPage"  
    3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    5.     xmlns:local="using:ShareAttachment"  
    6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
    7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
    8.     mc:Ignorable="d">  
    9.   
    10.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
    11.         <Button x:Name="btn_Share" Content="Share Attachment" HorizontalAlignment="Left" Margin="377,231,0,0" VerticalAlignment="Top" Click="btn_Share_Click"/>  
    12.   
    13.     </Grid>  
    14. </Page>  

Step 3:

We put an image file ‘abc.jpg’ in Assets folder of solution explorer so that it can be used as an attachment in the mail.

assets

Step 4:

In the code behind: MainPage.xaml.cs,

Update your code with this:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Runtime.InteropServices.WindowsRuntime;  
  6. using Windows.ApplicationModel.DataTransfer;  
  7. using Windows.Foundation;  
  8. using Windows.Foundation.Collections;  
  9. using Windows.UI.Xaml;  
  10. using Windows.UI.Xaml.Controls;  
  11. using Windows.UI.Xaml.Controls.Primitives;  
  12. using Windows.UI.Xaml.Data;  
  13. using Windows.UI.Xaml.Input;  
  14. using Windows.UI.Xaml.Media;  
  15. using Windows.UI.Xaml.Navigation;  
  16.   
  17. namespace ShareAttachment  
  18. {  
  19.     public sealed partial class MainPage : Page  
  20.     {  
  21.         public MainPage()  
  22.         {  
  23.             this.InitializeComponent();  
  24.             var dtm = Windows.ApplicationModel.DataTransfer.DataTransferManager.GetForCurrentView();  
  25.             dtm.DataRequested += Dtm_DataRequested;  
  26.         }  
  27.   
  28.         private async void Dtm_DataRequested(Windows.ApplicationModel.DataTransfer.DataTransferManager sender, Windows.ApplicationModel.DataTransfer.DataRequestedEventArgs args)  
  29.         {  
  30.             DataPackage dataToShare = args.Request.Data;  
  31.             dataToShare.Properties.Title = "Attachment Sharing Application";  
  32.             dataToShare.Properties.Description = "Attachment Sharing Application that share images";  
  33.   
  34.             var storageFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Assets\\abc.jpg");  
  35.             var imageStream = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(storageFile);  
  36.             dataToShare.SetBitmap(imageStream);  
  37.         }  
  38.   
  39.         private void btn_Share_Click(object sender, RoutedEventArgs e)  
  40.         {  
  41.             Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI();  
  42.         }  
  43.     }  
  44. }  
Step 5:

Run the application, click the Share Attachment button and you see a Share UI open in the right side of app.

Click on Mail and you see an email compose with an image attached.

attachment

That’s it.

Thanks, Happy Coding!

Read more articles on Windows Runtime Apps:


Similar Articles