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.
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!
- Vangos Pterneas
- Alex Borghi
- Jochen Kalmbach
- PrimalZed
- Radu Mihai Enghis
- Based on code by Sebastien Pertus from Microsoft
Thank you so much for this! This really helped me with a project I was working on.
Thank you very much 🙂
I’m glad it helped you!
Your Content Very Useful, Thank You
Thank you 🙂
Thank you for this library.
Is there a way how to keep password in c# code (or another file) without security concerns?
Hello. You’ll need to set the SSL value of the SMTP client to “true”. Also, the email provider needs to support SSL.
Unfortunately it doesn’t work for Unity w/ HoloLens
I tried this one, using a Windows 10 Phone. As Soon as the program executes this line:
emailMessage.To.Add(new EmailRecipient(“someone1@anotherdomain.com”));
the rest of the lines are skipped and the program executes further from the calling point. I can see that in the debugger. No mail is sent obviously.
I’ve added a try/catch but it also doesn’t reach the catch (Exception ex).
I also tried var emailReceipient = new EmailRecipient(“someone1@anotherdomain.com”); followed by
emailMessage.To.Add(emailReceipient);
and then it stops executing from when reaching the second line.
Any idea what’s happening here?
Dick
Hello Dick. Is the same issue occurring on UWP PC apps?
Hello Vangos,
That is quick!
We have a small project from 2016 running on Windows 10 Phone which sends pictures to a website using the BackgroundTransfer service. The customer also wanted the app to mail these photo’s. As we have a Chilkat license and there is a UWP DLL we thought we could use that. But although it seems to start it keeps returning an error indicating that after a successful SMTP connection has been made, sending of further information like ehlo command or login info fails.
It means that I have not yet tried it on W10. However we did develop some UWP apps and they all seem to run on both the Phone as a Pc (as they are designed to do). Only mail seems to be a problem on this phone. I do realize that a W10 Phone isn’t anyone’s priority nowadays () but I’d say it should work nevertheless.
Also I was wondering why directly after executing that line it completely skips the rest of the code (checked in debug mode) – without it ending up in a exception Catch which normally happens when a faulty line would fail.
Any suggestions are greatly appreciated.
Dick
Probably, the exception is raised internally in the EmailRecipient class. It’s hard for me to debug without a WP10 device.
Try to specify a different domain (e.g. “gmail.com”) to ensure the service is working properly. Some hosts may block automated emails. This is why it’s recommended to have a cloud service to send the emails instead of the client app.
Hello Vangos,
I think you are right; an internal exception must be raised as soon as the program tries to execute
emailMessage.To.Add(emailReceipient);
(or emailMessage.To.Add(new EmailRecipient(“myadres@somewhere.com”)); which I tried first and then split it in 2 parts).
Not sure if I can see that this happens somewhere in Visual Studio.
I the meantime I can confirm that it also doesn’t work when I execute the code from a Windows 10 Pc. Everything in emailMessage object is assigned exept “To”->(System.__ComObject) m_ObjectToDataMap = null. Of course sending the mail won’t work without a ‘To” address.
So it has nothing to do with the client; the program didn’t even begin sending because it can’t assign. And it’s not a mass mail either, just intended for individual messages.
Dick