How to Send Email in Windows Store Apps

Introduction

This article explains how to send an email in Windows Store apps using the IMAP component. The IMAP component is the first email component for Windows Store apps that allows reception of emails and processes email messages in Windows Store Applications. Includes the SMTP component for sending emails from Windows Store Applications.

Step 1

First download the IMAP component ( MailForWindowsStore.dll ) from the following link:

Mail for Windows Store apps

Step 2

Now open Visual Studio 2012 and start a new Windows Store apps project.

Step 3

In this step add a reference to "MailForWindowsStore.dll" in your project.

Add-Reference-Windows-Store-Apps.png

Step 4

Now go to the "MainPage.xaml" page and add the following code.

<Page

    x:Class="MailApp.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:MailApp"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

        <TextBox HorizontalAlignment="Left" TextWrapping="Wrap" x:Name="FromText" VerticalAlignment="Top" Margin="586,68,0,0" Width="302"/>

        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="From" FontSize="20" VerticalAlignment="Top" Margin="510,76,0,0"/>

        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="To" FontSize="20" VerticalAlignment="Top" Margin="510,136,0,0"/>

        <TextBox HorizontalAlignment="Left" TextWrapping="Wrap" x:Name="ToText" VerticalAlignment="Top" Margin="586,136,0,0" Width="302"/>

        <TextBox HorizontalAlignment="Left" TextWrapping="Wrap" x:Name="SubText" VerticalAlignment="Top" Margin="586,217,0,0" Width="302"/>

        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Subject" FontSize="20" VerticalAlignment="Top" Margin="490,217,0,0"/>

        <TextBox HorizontalAlignment="Left" TextWrapping="Wrap" x:Name="MsgText" VerticalAlignment="Top" Margin="586,296,0,0" Width="302" Height="175"/>

        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Messag" FontSize="20" VerticalAlignment="Top" Margin="490,304,0,0"/>

        <Button Content="Attach File" x:Name="AttachFileBtn" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="586,502,0,0" Click="AttachFileBtn_Click"/>

        <Button Content="Send" x:Name="SendBtn" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="783,502,0,0" Width="105" Click="SendBtn_Click"/>

    </Grid>

</Page>

Step 5

Go to the "MainPage.xaml.cs" page and add the following namespace.

using Limilabs.Mail;

using Windows.Storage;

using Windows.UI.Popups;

using Limilabs.Client.SMTP;

using Limilabs.Mail.Headers;

using Windows.Storage.Pickers;

Step 6

Here is the complete "MainPage.xaml.cs" code:

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Limilabs.Client.SMTP;

using Limilabs.Mail;

using Limilabs.Mail.Headers;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.Storage;

using Windows.Storage.Pickers;

using Windows.UI.Popups;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

 

namespace MailApp

{

    public sealed partial class MainPage : Page

    {

        FileOpenPicker FilePick;

        StorageFile AttachFile;

 

        public MainPage()

        {

            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

 

        }

        private async void AttachFileBtn_Click(object sender, RoutedEventArgs e)

        {

            FilePick = new FileOpenPicker();

            FilePick.FileTypeFilter.Clear();

            FilePick.FileTypeFilter.Add(".doc");

            FilePick.FileTypeFilter.Add(".png");

            FilePick.FileTypeFilter.Add(".txt");

            FilePick.FileTypeFilter.Add(".jpg");

            AttachFile = await FilePick.PickSingleFileAsync();

        }

        private async void SendBtn_Click(object sender, RoutedEventArgs e)

        {

            MailBuilder myMail = new MailBuilder();

            myMail.Html = MsgText.Text;

            myMail.Subject = SubText.Text;

            await myMail.AddAttachment(AttachFile);

            myMail.To.Add(new MailBox(ToText.Text));

            myMail.From.Add(new MailBox(FromText.Text));

           

            IMail email = myMail.Create();

 

            try

            {

                using (Smtp smtp = new Smtp())

                {

                    await smtp.Connect("smtp.gmail.com", 587);

                    await smtp.UseBestLoginAsync("Yoy Email ID", "Password");

                    await smtp.SendMessageAsync(email);

                    await smtp.CloseAsync();

                    MessageDialog msg = new MessageDialog("Mail has been sucessfully sent");

                    msg.ShowAsync();

                }

            }

            catch (Exception ex)

            {

                MessageDialog msg = new MessageDialog("Error in mail send");

                msg.ShowAsync();

            }

        }    

    }

}

Step 7

Finally run your project and try to send an Email.

Result-Windows-Store-Apps.png


Similar Articles