I am writing an application to monitor a mailbox and log tickets for a support ticketting system.
I have it working (mostly) however I just noticed that when the monitor programme first starts up, anything already in the mailbox is ignored and not processed.
This is the code I am using to retrieve items from the mailbox, can anyone assist please? (this is called one when the application first launches, and then whenever the subscription is notified of incoming mail, so I thought it would process all current emails in there as well).
The email is then processed and moved by the receiving code (a website).
Thanks in advance!
void CheckForItems()
{
// Track whether there are more items available for download on the server.
bool moreChangesAvailable = false;
do
{
// Get a list of all items in the Inbox by calling SyncFolderHierarchy repeatedly until no more changes are available.
// The folderId parameter must be set to the root folder to synchronize,
// and must be same folder ID as used in previous synchronization calls.
// The propertySet parameter is set to IdOnly to reduce calls to the Exchange database,
// because any additional properties result in additional calls to the Exchange database.
// The ignoredItemIds parameter is set to null, so that no items are ignored.
// The maxChangesReturned parameter is set to return a maximum of 10 items (512 is the maximum).
// The syncScope parameter is set to Normal items, so that associated items will not be returned.
// The syncState parameter is set to cSyncState, which should be null in the initial call,
// and should be set to the sync state returned by the
// previous SyncFolderItems call in subsequent calls.
ChangeCollection<ItemChange> icc = service.SyncFolderItems(new FolderId(WellKnownFolderName.Inbox), PropertySet.IdOnly, null, 10, SyncFolderItemsScope.NormalItems,"");
// If the count of changes is zero, there are no changes to synchronize.
if (icc.Count == 0)
{
}
else
{
foreach (ItemChange ic in icc)
{
PropertySet propSet = new PropertySet(BasePropertySet.FirstClassProperties);
Item item = Item.Bind(this.service, ic.ItemId, propSet);
//-------------------------------------------------
//SEND ALERT HERE
//-------------------------------------------------
if(this.OnNotifyEmail != null)
this.OnNotifyEmail(item);
WebRequest webRequest = WebRequest.Create(string.Format(this.ticketUrlHandler, System.Net.WebUtility.UrlEncode(item.Id.UniqueId)));
HttpWebResponse response = null;
webRequest.Timeout = 1200; //miliseconds
webRequest.Method = "HEAD";
webRequest.UseDefaultCredentials = true;
try
{
response = (HttpWebResponse)webRequest.GetResponse();
}
catch (WebException webException)
{
WebExceptionStatus status = webException.Status;
this.OnEmailError(item, webException.Message);
}
catch (Exception ex)
{
this.OnEmailError(item, ex.Message);
}
finally
{
if (response != null)
{
response.Close();
}
}
}
}
// Save the sync state for use in future SyncFolderContent requests.
// The sync state is used by the server to determine what changes to report
// to the client.
string sSyncState = icc.SyncState;
// Determine whether more changes are available on the server.
moreChangesAvailable = icc.MoreChangesAvailable;
}
while (moreChangesAvailable);
}