I have been developing Windows Store apps since Microsoft released Windows 8.0, back in 2012. Windows Store apps where initially based on the WinRT framework. Now, they are based on the newer UWP framework. UWP is the go-to way when it comes to building modern Windows apps. Why? Because a single UWP app can run on your PC, smartphone, XBOX, and even HoloLens.

All this potential comes with a limitation, though. Windows Store apps run in a sandbox environment. As a result, not all of the good old features are available. One of the unavailable features is the ability to send an email from within a Windows Store app. As of today, there are 2 ways to send an email from a WinRT or UWP app:

Method #1 – Use an online server

The best method to send an email is by making a web request to your online server infrastructure. The client app will simply provide the parameters (name, subject, content, etc) as part of the web request. The server would receive the request and send the email to the recipient. In terms of flexibility and security, this is the best way to send emails from a client app. This is what LightBuzz has done for the Kinetisense project.

Drawbacks:

  • It’s expensive, since a remote server is required.
  • Needs a backend infrastructure.

Method #2 – Use the UWP email dialog

Using the email dialog API, the developer only provides the desired content and the Operating System tries to find an app that is capable to send emails, such as the built-in Mail app.

Drawbacks:

  • You cannot send automated emails (e.g. on crash reports).
  • Most people are using online email clients, such as gmail.com or outlook.com.
  • The user may not have installed or configured an email app.
  • The email dialog window should be always visible.

So, what if you need your app to act like an SMTP client? What if you need your app to send emails programmatically? Thankfully, there is a third option.

Introducing LightBuzz SMTP for Windows Store Apps

We developed a native SMTP email client exclusively for Windows Store apps. No external servers required. All is done programmatically and the details are stored into the application package. The SMTP client is an open-source project, hosted on GitHub.

Installation

You can download and build this project directly from GitHub. However, the easiest way is to install it via NuGet:

PM> Install-Package lightbuzz-smtp

This requires the Internet (Client) Capability to be enabled in the Package.appxmanifest of the universal app.

AppxManifest Capabilities - Internet (Client)

How to use

To use the SMTP library, you need to import the assembly and its namespace to your project:

using LightBuzz.SMTP;

Then, you need to specify some details about your SMTP server. The SMTP server could be a server you or your company owns, or even a public service like Gmail, Outlook, or Yahoo. Five parameters are required to send the email:

  • Server name (e.g. “smtp.example.com”)
  • Server port (e.g. 465)
  • SSL (true or false)
  • Username (e.g. “mail@example.com” or “vangos”, depending on the server)
  • Password (the password of the account — please encrypt it before storing into your app!)

If you are not sure about the exact values, contact your server administrator.

After you have specified these parameters, create an EmailMessage object and specify the email contents:

  • To – a list of recipients
  • CC – a list of recipients that will be CC’ed in the email
  • Bcc – a list of recipients that will be Bcc’ed in the email
  • Subject – the subject line of your message
  • Body – the main content of your message
  • Attachments – a list of attachments, in binary form

Finally, call the SendMailAsync method to deliver your email! The SendMailAsync method will return a success or failure message. Here is the complete sample code:

var server = "smtp.example.com";
var port = 465;
var ssl = false;
var username = "mail@example.com";
var password = "Pa$$w0rd"; // Encrypt this!

using (var client = new SmtpClient(server, port, ssl, username, password))
{
        var emailMessage = new EmailMessage();

        emailMessage.To.Add(new EmailRecipient("someone1@anotherdomain.com"));
        emailMessage.CC.Add(new EmailRecipient("someone2@anotherdomain.com"));
        emailMessage.Bcc.Add(new EmailRecipient("someone3@anotherdomain.com"));

        emailMessage.Subject = "Subject line of your message";
        emailMessage.Body = "This is an email sent from a WinRT app!";

        await client.SendMailAsync(emailMessage);
}

The example above assumes you are using a custom SMTP server (yourdomain.com). In case you don’t have your own domain, here is how to use Gmail or Outlook, instead:

Credentials for Gmail

Server: smtp.gmail.com
Port: 465
SSL: True

Important Note for Gmail: Since this does not use OAUTH2, Gmail considers this a “less secure app”. To use this with Gmail, the “Access for less secure apps” setting on the account will have to be changed to “Enable”.

Credentials for Outlook

Server: smtp-mail.outlook.com
Port: 587
SSL: False (upgarde SSL after STARTTLS)

Contributors

The following people have contributed to the development of the project. Thank you, guys!

Author Vangos Pterneas

Vangos Pterneas is an award-winning Microsoft Most Valuable Professional. He is helping companies from all over the world grow their revenue by creating profitable software products. He loves Motion Technology and Mixed Reality. Vangos is the CEO of LightBuzz Inc and author of The Dark Art Of Freelancing.

More posts by Vangos Pterneas

Leave a Reply