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:

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. namespace ShareAttachment
  17. {
  18. public sealed partial class MainPage : Page
  19. {
  20. public MainPage()
  21. {
  22. this.InitializeComponent();
  23. var dtm = Windows.ApplicationModel.DataTransfer.DataTransferManager.GetForCurrentView();
  24. dtm.DataRequested += Dtm_DataRequested;
  25. }
  26. private async void Dtm_DataRequested(Windows.ApplicationModel.DataTransfer.DataTransferManager sender, Windows.ApplicationModel.DataTransfer.DataRequestedEventArgs args)
  27. {
  28. DataPackage dataToShare = args.Request.Data;
  29. dataToShare.Properties.Title = "Attachment Sharing Application";
  30. dataToShare.Properties.Description = "Attachment Sharing Application that share images";
  31. var storageFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Assets\\abc.jpg");
  32. var imageStream = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(storageFile);
  33. dataToShare.SetBitmap(imageStream);
  34. }
  35. private void btn_Share_Click(object sender, RoutedEventArgs e)
  36. {
  37. Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI();
  38. }
  39. }
  40. }
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: