cancel
Showing results for 
Search instead for 
Did you mean: 

Exchange Web Services in BP using C#

ChristianJuhl
Level 4
Hi,

I am trying to develop a BP C# object that uses Exchange Web Services (EWS). I have written a working code in Visual Studio but I am struggling to make in work in Object Studio.

This is the code from Visual Studio:

using Microsoft.Exchange.WebServices.Data;
using System;
using System.Net;

namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
ExchangeService service = new ExchangeService();

service.Credentials = new NetworkCredential("userid", "password");

service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "userid");

Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);

Console.WriteLine($"Unread email in inbox: {inbox.UnreadCount}");

Console.ReadLine();
}
}
}

My code stage in Object Studio looks like this:

29318.png

I have copied the Microsoft.Exchange.WebServices DLL to C:\Program Files\Blue Prism Limited\Blue Prism Automate\, and added Microsoft.Exchange.WebServices.dll and System.Net.dll to External References and Microsoft.Exchange.WebServices.Data and System.Net to Namespace Imports.

I get the following errors:

29320.png

Can anyone see what I am doing wrong? I realize that the code stage errors are syntax related, but I won't be able to fix those until the namespace error is resolved, which I have no clue how to fix.

Thank you very much in advance!

------------------------------
Christian Juhl
Business Controller
Danske Commodities
Europe/Copenhagen
------------------------------
15 REPLIES 15

JerinJose
Level 10
​Hi Christian,

Try the namespaces given below.
29242.png


------------------------------
Jerin Jose
RPA Product SME
EY
Asia/Kolkata
------------------------------

Thank you, Jerin. I will try it out.

Have you developed any EWS objects i Blue Prism that you would be willing to share?

------------------------------
Christian Juhl
Business Controller
Danske Commodities
Europe/Copenhagen
------------------------------

​Here is basic EWS I made:
https://github.com/aikudinov/BlueprismVBO/blob/master/BPA%20Object%20-%20Exchange%20EWS%20API.xml

Search filter need to be redone - AQS searches are cached, but you can't reset it in managed api - not useable.
Using generic SearchFilter will be either limited or overcomplicated - I can just provide both options I guess.
There is only basic mail. Appointments etc are not hard, but don't have spare time to work on it.
Not sure if url autodiscovery will work with office365..
If you drop dlls in Automate folder instead of installing from msi - remove paths to dlls in the VBO.

Should get you started ...

p.s. About your code: you don't need program/main - code stage is basically a static function within a class (VBO). There is also no console. You also need to define your service in globals if you want to use multiple code stages.

------------------------------
Andrey Kudinov
Project Manager
MobileTelesystems PJSC
Europe/Moscow
------------------------------

Thank you, Andrey. My code stage is working now. Can you explain what you mean by defining my service in Global Code? Why is this necessary if I want to use multiple code stages?

------------------------------
Christian Juhl
Business Controller
Danske Commodities
Europe/Copenhagen
------------------------------

@Christian Juhl, basically if you are going to put all your logic into one code stage, you are good as it is.

However, if you want to create a generic VBO with multiple actions, you will either have to ​do initialization (ExchangeService service = new ExchangeService(); etc..) in each code stage, or you go in Initialization page (where you add dll references) - Global code and declare "ExchangeService service; " there​, then use "service = new ExchangeService();" when you actually do init in a code stage.
This way that service variable will be persistent across different code stages within VBO (during process lifetime) and you'd only have to do init once.

------------------------------
Andrey Kudinov
Project Manager
MobileTelesystems PJSC
Europe/Moscow
------------------------------

@Christian Juhl There is an existing asset in DX for Exchange Functions. Take a look and see if it serves​ your purpose. https://digitalexchange.blueprism.com/dx/entry/16835/solution/ms-exchange-functions---agilify​

------------------------------
Pratyush Garikapati
ROM Architect
Blue Prism
Asia/Kolkata
------------------------------

@AndreyKudinov Thank you for clarifying!

------------------------------
Christian Juhl
Business Controller
Danske Commodities
Europe/Copenhagen
------------------------------

@Pratyush Garikapati Thanks a lot! This is exactly what I need. ​

------------------------------
Christian Juhl
Business Controller
Danske Commodities
Europe/Copenhagen
------------------------------

Hi Christian,
I also just learned about the Exchange Functions VBO that Agilify made. It's mostly working for me as well, but did you find that the Search Folder action seems to ignore the Folder ID input?

I use the actions in this order:
1. Setup Service Office 365 -- works for me (not using Impersonated User ID)
2. Search Folder ID By Name -- seems to work since it returns a folder ID consistently and can even return folder IDs from shared mailboxes if I input the shared mailbox address in Alternate Mailbox Address.
3. Search Folder -- this seems to always return the Inbox, and I'm not sure but I think maybe it has something to do with the code seeming to bind arbitrarily to the inbox. However, this will throw an error if I provide no Folder ID, and it will still return the Inbox items if I provide a random value. So, it appears as though the code completely ignores the Folder ID.

Anyway, anyone else having the same result? I may attempt to edit the code, but it's annoying because it's code that is compiled into DLLs.

------------------------------
Dave Morris
3Ci @ Southern Company
Atlanta, GA
------------------------------

Dave Morris, 3Ci at Southern Company