Question
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
David Taliga
AT
Last activity: 31 Oct 2019 13:54 EDT
Outlook Email - Find Email according to its Subject
Hi Guys,
I would like to find solution for task, where I am searching for specific email in outlook. (inbox folder). Currently I search for email acording to Exact Subject name and Folder where the email is located. If I dont look for exact subject name, then the email is not found.
How can I apply solution which will find me and email without telling exact subject name. For example, I want email with subject name: Report_XYZ_21.9.2018 , but I want to search without the date, like this: Report_XYZ*
attaching my code
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
Pegasystems Inc.
IN
Hi David,
Please take a look at the below articles for information :
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
Infosys
IN
Hope you can't apply any constraints like contains etc.. So, loop all emails and apply if condition with required subject.
![](https://accounts.pega.com/sites/default/files/styles/user_image/public/1689966000/8f6df811-ba11-4fa1-abb3-012b547c823d.jpg?itok=lHCJKbmt)
![](https://accounts.pega.com/sites/default/files/styles/user_image/public/1689966000/8f6df811-ba11-4fa1-abb3-012b547c823d.jpg?itok=lHCJKbmt)
Pegasystems Inc.
US
Hi, DavidT,
I would solve it with type extension for Outlook connector. Here is how to do it:
1. Make right click on Outlook connector that is placed in Global Container and select "Create Type Extension":
This opens new window containing template for extension code like this:
2. Add new method that fits your needs. Here is code sample with method FindEmails that returns e-mails where subject starts with provided string:
Hi, DavidT,
I would solve it with type extension for Outlook connector. Here is how to do it:
1. Make right click on Outlook connector that is placed in Global Container and select "Create Type Extension":
This opens new window containing template for extension code like this:
2. Add new method that fits your needs. Here is code sample with method FindEmails that returns e-mails where subject starts with provided string:
private MicrosoftOutlook Connector
{
get { return (MicrosoftOutlook)this.Component; }
}
public List<MicrosoftOutlookMail> FindEmails(string folder, string from, string to, string subjectStartsWith)
{
Diagnostic.TraceVerbose("OutlookExtension", string.Format("Starting execution"));
if (string.IsNullOrEmpty(folder))
throw new ArgumentNullException(nameof(folder));
if (string.IsNullOrEmpty(from) && string.IsNullOrEmpty(to) && string.IsNullOrEmpty(subjectStartsWith))
throw new ArgumentException("Either 'from', 'to' or 'subject' parameter must be set.");
MAPIFolder mapiFolder = OfficeHelper.GetMAPIFolder(this.Connector.OutlookApplication.Session, folder);
List<MicrosoftOutlookMail> mails = new List<MicrosoftOutlookMail>();
Items items = mapiFolder.Items;
Diagnostic.TraceVerbose("OutlookExtension", string.Format("Total items count: {0}", items.Count));
if (!string.IsNullOrEmpty(to))
{
string Filter = "[To] = '" + to + "'";
items = items.Restrict(Filter);
}
if (!string.IsNullOrEmpty(from))
{
string Filter = "[From] = '" + from + "'";
items = items.Restrict(Filter);
}
bool flag = !string.IsNullOrEmpty(subjectStartsWith);
if (items != null)
{
foreach (object obj in items)
{
if (obj != null && obj is MailItem)
{
MailItem mailItem = (MailItem)obj;
if (!flag || mailItem.Subject.StartsWith(subjectStartsWith, StringComparison.CurrentCultureIgnoreCase))
{
mails.Add(new MicrosoftOutlookMail(mailItem, true));
}
}
}
}
return mails;
}
3. Build project and new method will be available for Outlook connector. ListLoop can be used to iterate by search results:
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
Statefarm Insurance
US
Thanks you for your example. I tried several different ones to try to get your code to compile but it's throwing an error. Please can paste exact code you have used or screenshot
![](https://accounts.pega.com/sites/default/files/styles/user_image/public/1689966000/8f6df811-ba11-4fa1-abb3-012b547c823d.jpg?itok=lHCJKbmt)
![](https://accounts.pega.com/sites/default/files/styles/user_image/public/1689966000/8f6df811-ba11-4fa1-abb3-012b547c823d.jpg?itok=lHCJKbmt)
Pegasystems Inc.
US
Most likely you are missing references and "using" statements. Please see the reply below on this page with details. In the newest versions of Runtime/Studio some libraries containing "OpenSpan" may be renamed to contain "Pega" instead.
If it still doesn't work - please post compilation errors here.
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
Statefarm Insurance
US
Thanks for response. Added libraries, using statements and Issue was resolved.
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
![](/profiles/pega_profile/modules/pega_user_image/assets/user-icon.png)
Monster Pixel
US
Hi Konstantin Serditov:
Thanks you for your example. I'm new to learning Pega and Openspan and it has been a while since I wrote sofware in Java so my question may be silly. However, I tried your code example and there's a missing namepace declearation. I tried several different ones to try to get your code to compile and I also tried adding different assemblies to the IDE. Could you please tell me what is missing in the namespaces to get your software to compile and run. This is exactly the kind of thing I'm trying to use to learn Pega and see how our team may use the system more.
Regards,
Al
![](https://accounts.pega.com/sites/default/files/styles/user_image/public/1689966000/8f6df811-ba11-4fa1-abb3-012b547c823d.jpg?itok=lHCJKbmt)
![](https://accounts.pega.com/sites/default/files/styles/user_image/public/1689966000/8f6df811-ba11-4fa1-abb3-012b547c823d.jpg?itok=lHCJKbmt)
Pegasystems Inc.
US