Windows Phone 8.1 and Windows Runtime Apps How to #2 – Send Emails with Attachment in WP 8.1

The Windows Phone 8 SDK allowed the developer to use the EmailComposerTask launcher to send emails from the application . One of the restrictions that it had was that the attaching file programmatically which was not possible.

The Windows Phone 8.1 SDK provides an option for the developer to send emails with attachment using the EmailMessage and EmailManager class .

Note that the Minimum supported phone version for the EmailMessage and the EmailManager is Windows Phone 8.1  .

1. EmailMessage

The EmailMessage class defines the actual email that will be sent. You can specify the recipients (To , CC , BC) , Subject and the Body of the email .

2. EmailManager

The EmailManager class is defined in the Windows.ApplicationModel.Email namespace . The EmailManager class provides a static method ShowComposeNewEmailAsync which accepts the EmailMessage as argument . The ShowComposeNewEmailAsync will launch the Compose email Screen with the EmailMessage which allows the users to send an email message.

How to Programatically Send Emails with attachment in Windows Phone 8.1 Apps ?

Step 1 : Lets create a file called “developerpublish.txt” within your application and return it . This will be the file that will be attached with the email.

Below is a sample code snippet on how to create a text file programmatically in Windows Phone 8.1.

// Creates a text file and returns it
private static async Task<StorageFile> GetTextFile()
{
           var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
           var file = await localFolder.CreateFileAsync("developerpublish.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting);
           await Windows.Storage.FileIO.WriteTextAsync(file, "This is a developerpublish File");
           return file;
}

Step 2 : The next step would be create the instance of the EmailMessage and prepopulate the necessary fields. Lets attach the file that we received from the GetTextFile().

  // Send an Email with attachment
  EmailMessage email = new EmailMessage();
  email.To.Add(new EmailRecipient("[email protected]"));
  email.Subject = "Blog post by @isenthil";
  var file = await GetTextFile();
  email.Attachments.Add(new EmailAttachment(file.Name, file));
  await EmailManager.ShowComposeNewEmailAsync(email);

Step 3 : When the EmailManager.ShowComposeNewEmailAsync method is called , You will be listed with the apps / email accounts that are configured on your phone . Select the email account from which you need to send email from .

1

Step 4 : This will display the Email Compose Dialog with the screen already filled with the EmailMessage data that was passed to the ShowComposeNewEmailAsync method. Clicking on the Send button in the application bar will send the email with the attachment .

2

    3 Comments

  1. Muhaymin
    August 24, 2014
    Reply

    How to share Text or Image to Whatsapp, Facebook, SMS, and other Social Network in Windows Phone 8.1 Runtime app?

  2. Sebastian
    October 13, 2015
    Reply

    and images?

  3. Aniruddha
    April 15, 2016
    Reply

    Is there anyway to find out whether the email was sent or not?

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you’ll learn about the Win32 Error “0x000019E5 – ERROR_COULD_NOT_RESIZE_LOG” that you get when debugging system erors in...
In this post, you’ll learn about the error “CO_E_DBERROR 0x8004E02B” that is returned when working with COM based APIs or...
In this post, you’ll learn about the Win32 Error “0x000019D0 – ERROR_LOG_BLOCK_VERSION” that you get when debugging system erors in...