Answered by:
[UWP] [Desktop Bridge] Consuming UWP mail api from WPF (UWP desktop app)

Question
-
I'm porting my application to UWP, and some APIs are not permitted in the UWP - one of these is classic mail API (MAPI).
What I found is that:
- UWP exposes new mail API (as the classic MAPI is not allowed anymore)
- Any application referencing some WinRT DLLs (my app is WPF Windows Desktop application) can use the new UWP APIs (including the new mail API). (Btw. UWP and WPF UI is not compatibile - but this is different story and we are not dealing with this (cross platform UI usage) here)
I've already made some successfull tests with light sensor (UWP APIs consumed from Windows console application), and hoped the mail api will work as well. However, when I run this simple code - it works when compiled in UWP project, however it does not, when run from within WPF application - also having packaged it and installed as Windows Desktop UWP application.
EmailMessage emailMessage = new EmailMessage(); emailMessage.To.Add(new EmailRecipient("someone@gmail.com")); emailMessage.Body = "message body"; await EmailManager.ShowComposeNewEmailAsync(emailMessage);
When run it ends up with communicate: The request is not supported Exception from hresult (0x80070032)
The question is: Is this a known limitation, that mail api exposed by UWP will not work from WPF (or any non-UWP host) ? Can anyone tell whether it is general behavior and not specific to my environment ? (in order to compile in WPF project, need to add reference to two DLLs: System.runtime.windowsruntime.dll and Windows.winmd)
Only when run from UWP application the code shows mail client
Thanks in advance, Michal
Friday, September 22, 2017 3:57 PM
Answers
-
This is known and documented.
Windows.ApplicationModel.Email is not supported in desktop apps and is not on the list of API supported in a packaged desktop app. See https://docs.microsoft.com/en-us/windows/uwp/porting/desktop-to-uwp-supported-api
Your best bet is probably to include both a UWP and a desktop app in your package and call the email API from the UWP side. See the App Service Bridge sample to get started: https://github.com/Microsoft/DesktopBridgeToUWP-Samples/tree/master/Samples/AppServiceBridgeSample
- Proposed as answer by Andre MarschalekMVP Friday, September 22, 2017 11:40 PM
- Marked as answer by Michal.Jan Saturday, September 23, 2017 8:08 AM
Friday, September 22, 2017 9:40 PM
All replies
-
hello
it works when compiled in UWP project, however it does not, when run from within WPF application
could you clarify what you mean because UWP and WPF are 2 different kind of stories, 2 different .NET Architectures and 2 different API's
what are you trying to achieve ?
br
AndreFriday, September 22, 2017 6:26 PM -
This is known and documented.
Windows.ApplicationModel.Email is not supported in desktop apps and is not on the list of API supported in a packaged desktop app. See https://docs.microsoft.com/en-us/windows/uwp/porting/desktop-to-uwp-supported-api
Your best bet is probably to include both a UWP and a desktop app in your package and call the email API from the UWP side. See the App Service Bridge sample to get started: https://github.com/Microsoft/DesktopBridgeToUWP-Samples/tree/master/Samples/AppServiceBridgeSample
- Proposed as answer by Andre MarschalekMVP Friday, September 22, 2017 11:40 PM
- Marked as answer by Michal.Jan Saturday, September 23, 2017 8:08 AM
Friday, September 22, 2017 9:40 PM -
Hi Andre, I'm trying to consume UWP from desktop application. With regard to the UI - this is not possible at all, but when not dealing with UI, yes it is possible. See the Rob's reply.
Thanks, Regards
MichalSaturday, September 23, 2017 8:11 AM -
Hi Rob,
I've googled a bit on this and downloaded some samples. Now I think it may be hard for me to follow the scenario.
In my case I need to *expose service from UWP process*, and activate/consume it from the desktop (.net) part (so, the opposite way as in virtually all examples/tutorials). This means that it is the .net part that needs to call UWP APIs to activate the service (hosted on UWP side), and similarly as with UWP mail APIs - these for service activation/communication APIs are not enlisted on the supported API list you provided.
I also tried to port the code (e.g. Windows.ApplicationModel.FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync(); ) but even the definitions are not visible from .net project (WinRT libraries are referenced - mail apis are visible).
I have one additional question on this:
On the list of the constraints that the a desktop app needs to adhere to, when being compliant with UPW - the MAPI (referred to as Outlook MAPI) is not permitted. This may be a dumb question, but is this [Outlook MAPI] the same thing that I use (MAPI32.dll) ?
https://docs.microsoft.com/en-us/windows/uwp/porting/desktop-to-uwp-prepare (the last bullet point)Maybe I was wrong, thinking that this refers to general classic Windows Mail API (MAPI32.DLL) - and I will still be able to send mails the way I do now (of course workstation will need to have MAPI application installed)
Saturday, September 23, 2017 9:44 AM -
Update:
Following this tutorial (https://docs.microsoft.com/en-us/windows/uwp/launch-resume/how-to-create-and-consume-an-app-service) I've managed to:
- expose a service from native UWP host
- consume the service both from native UWP application, and from Desktop UWP Application (packaged .Net WPF app as UWP app)
Thanks for help
Best regards,
MichalSaturday, September 23, 2017 12:23 PM