cancel
Showing results for 
Search instead for 
Did you mean: 

EWS Read Email using Blue Prism Code Stage

SatheeshKumarT
Level 2
Hi Team, I am using C# code for EWS Read Email Process in Code stage using Blue Prism. I am getting the mail data from Email Input folder to FindItemsResults based on the Subject. Now I want to add the data (Subject, Body, From, To, etc.), to Collection. But I am not able to do so as it throws an error. So is there any other way to get the unread EWS Email (office 365) data to collection? Please revert as soon as possible.   C# Code : ExchangeService service = new ExchangeService();  service.Credentials = new NetworkCredential(UserName, Password); service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"); EmailMessage message = new EmailMessage(service); Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox); SearchFilter.SearchFilterCollection sf = new SearchFilter.SearchFilterCollection(LogicalOperator.Or); sf.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Test Subject"));            ItemView view = new ItemView(100); FindItemsResults findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view); Console.WriteLine(findResults.Count().ToString()); OutputCount =findResults.Count().ToString(); foreach (var item in findResults) {     Console.WriteLine(item.Subject);     //OutputCount = item.Subject;    }
4 REPLIES 4

VijayDodamani
Level 5
Hello, FindItemsResults findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view); You need to get the ServiceResponseCollection  please find the below code, I have modified a bit: static void Main(string[] args)         {             MailItem[] mailItems = new MailItem[] { };             ServiceResponseCollection items = GetUnreadMailFromInbox();             mailItems = items.Select(item => {                 return new MailItem()                 {                     From = ((EmailAddress)item.Item[EmailMessageSchema.From]).Address,                     Recipients = ((EmailAddressCollection)item.Item[EmailMessageSchema.ToRecipients]).Select(recipient => recipient.Address).ToArray(),                     Subject = item.Item.Subject,                     Body = item.Item.TextBody.ToString(),                 };             }).ToArray();             foreach(MailItem mailItem in mailItems)             {                    Console.WriteLine(mailItem.From);                 Console.WriteLine(mailItem.Recipients);                 Console.WriteLine(mailItem.Subject);                 Console.WriteLine(mailItem.Body);             }             Console.ReadKey();         }         public static ServiceResponseCollection GetUnreadMailFromInbox()         {             ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);             service.Credentials = new NetworkCredential(""MEA\\Vijay.dodamani"", ""Vj@ey110688"");             service.Url = new Uri(""https://mail-xi.ey.net/ews/exchange.asmx"");             EmailMessage message = new EmailMessage(service);             Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);             SearchFilter.SearchFilterCollection sf = new SearchFilter.SearchFilterCollection(LogicalOperator.Or);             sf.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, ""RE: Go Live for 4 Processes tomorrow. 1 Nov""));             ItemView view = new ItemView(100);             FindItemsResults findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(128));                          return service.BindToItems(findResults.Select(item => item.Id),                      new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients));                       }

SatheeshKumarT
Level 2
Hi vijay.dodamani, Thank you for the code. I tried using the code you have given but i am getting some errors. So can you please share Namespace and dll to be used.  And is it possible to write multiple functions in the code stage? And can you share a sample xml for the same? Thanks in Advance.

VijayDodamani
Level 5
namespace:  using Microsoft.Exchange.WebServices.Data dll: c:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll And is it possible to write multiple functions in the code stage?  : Not sure,  Previous code is implemented in studio, Modified code as per code stage which you can just copy and paste and use the action:             DataTable table = new DataTable();             table.Columns.AddRange(new DataColumn[] {   new DataColumn(""Item ID"", Type.GetType(""System.String"")),                                                             new DataColumn(""Sender"", Type.GetType(""System.String"")),                                                             new DataColumn(""Subject"", Type.GetType(""System.String"")),                                                             new DataColumn(""CC"", Type.GetType(""System.String"")),                                                             new DataColumn(""TO"", Type.GetType(""System.String"")),                                                             new DataColumn(""Body"", Type.GetType(""System.String"")),                                                             new DataColumn(""DateReceived"", Type.GetType(""System.String""))});             ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);             service.Credentials = new NetworkCredential(Username, Password);             service.Url = new Uri(ServiceURL);             Mailbox mb = new Mailbox(EmailID);             FolderId fid1 = new FolderId(WellKnownFolderName.Inbox, mb);             PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties);             itempropertyset.RequestedBodyType = BodyType.Text;             ItemView itemview = new ItemView(1000);             itemview.PropertySet = itempropertyset;             FindItemsResults findResults = service.FindItems(fid1, new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, ReadAllMails)), new ItemView(1000));             service.LoadPropertiesForItems(findResults, PropertySet.FirstClassProperties);             foreach (Item item in findResults.Items)             {                 EmailMessage mes = (EmailMessage)item;                 ItemId itemID = item.Id;                 item.Load(itempropertyset);                 DataRow row = table.NewRow();                 row[""Subject""] = item.Subject;                 row[""Sender""] = mes.Sender.Name;                 row[""CC""] = String.Join("","", mes.CcRecipients);                 row[""TO""] = String.Join("","", mes.ToRecipients); ;                // row[""Sender Address""] = mes.From.Address;                 row[""Body""] = item.Body.Text;                 row[""Item ID""] = itemID;               //  row[""Attachment""] = (item.HasAttachments) ? ""True"" : ""false"";                // row[""DateReceived""] = mes.DateTimeReceived;                 table.Rows.Add(row);             } Create output variable of code stage named collection_out finally, collection_out = table In the code i have done the bold text, wherever you find that should be input to code stage.   Thank you.

SatheeshKumarT
Level 2
Hi vijay.dodamani, Thanks You , i got the solution for your support. Do you have EWS voting Email code ? , if it possible to send voting option Email on EWS Email? Kind Regards, SATHEESH