I am trying to upgrade en existing application that reads Exchange 2003 using WebDAV. The mail server is to be upgraded to Exchange 2013, so I am checking how I can use EWS.
I have a problem in that although I know the inbox has unread items with attachments, the query I am running against the FindItems
object is returning empty...
Here is my code snippet:
private static void GetAttachments(ExchangeService service)
{
// Return a single item.
ItemView view = new ItemView(100);
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);// .Exchange2007_SP1);
service.UseDefaultCredentials = true;
service.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback);
ItemView view = new ItemView(1);
string querystring = "HasAttachments:true Subject:'ATTACHMENT TEST' Kind:email";
// Find the first email message in the Inbox that has attachments.
// This results in a FindItem operation call to EWS.
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, querystring, view);
//FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, new ItemView(50));
if (results.TotalCount > 0)
{
// looping through all the emails
for (Int16 iDx = 0; iDx < results.TotalCount-1; iDx++)
{
EmailMessage email = results.Items[iDx] as EmailMessage;
if (email.IsRead == false) {
// Request all the attachments on the email message. This results in a GetItem operation call to EWS.
email.Load(new PropertySet(EmailMessageSchema.Attachments));
foreach (Attachment attachment in email.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
What I am supposed to be doing is reading all the unread emails in the target inbox (only one Exchange server) and taking the attachments on disk so I can then add them as attachments as new cases on SalesForce.
Where am I going wrong?
Also, this line:
ItemView view = new ItemView(100);
was:
ItemView view = new ItemView(1);
Surely that will only look for one email item, right?
I submitted the following XML and got the expected results. The easiest way to figure out what is going on is to look at the XML.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<soap:Header>
<t:RequestServerVersion Version="Exchange2010_SP2"/>
</soap:Header>
<soap:Body>
<FindItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
Traversal="Shallow">
<ItemShape>
<t:BaseShape>IdOnly</t:BaseShape>
</ItemShape>
<ParentFolderIds>
<t:DistinguishedFolderId Id="inbox"/>
</ParentFolderIds>
<QueryString>HasAttachments:true Subject:'ATTACHMENT TEST' Kind:email</QueryString>
</FindItem>
</soap:Body>
</soap:Envelope>