By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,173 Members | 595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,173 IT Pros & Developers. It's quick & easy.

Outlook.ApplicationClass() Unread emails problem

P: 1
My C#.NET console app checks a public folder every 24 hours for incoming emails.
For each unread email in the folder, it copies any attachments to the network, then loads the contents of these files
into a SQL Server database, and marks the email as read.

My problem is that in the loop that checks for all the Unread emails in the folder,
as I spin through the loop, the number of unread items is being reset for every iteration.
So KItems.Count changes.
This has the effect of not getting all the unread emails.
Also they are not being correctly marked as read afterwards

I can see what is happening, but I can't think of a solution. Would be very grateful for any ideas.
Code below

public static void Temp()
{

Outlook._Application olApp;
Outlook._NameSpace olNS = null;
object collectionItem;

try
{
olApp = new Outlook.ApplicationClass();
olNS = olApp.GetNamespace("mapi");
olNS.Logon(@"myaccount","pwd" , false,true);
Outlook._Folders oFolders;
oFolders = olNS.Folders;
Outlook.MAPIFolder oPublicFolder = oFolders.Item("Public Folders");
oFolders = oPublicFolder.Folders;
Outlook.MAPIFolder oAllPFolder = oFolders.Item("All Public Folders");
oFolders = oAllPFolder.Folders;
Outlook.MAPIFolder oMyFolder = oFolders.Item("Client Applications");
oFolders = oMyFolder.Folders;
Outlook.MAPIFolder oPG = oFolders.Item("pgcsi");
oFolders = oPG.Folders;
Outlook.MAPIFolder oPGData = oFolders.Item("pgdata");
Outlook.MailItem mi;
Items KItems = oPGData.Items.Restrict("[UnRead]=true");
Console.WriteLine("No. of unread items = " + KItems.Count.ToString());
for(int n=1;n<=KItems.Count;n++)
{
collectionItem = KItems.Item(n);
mi = collectionItem as Outlook.MailItem;
mi.UnRead = false;

if (mi != null)
{
Int64 LoadID;
for(int o=1;o<=mi.Attachments.Count;o++)
{
mi.Attachments.Item(o).SaveAsFile(BasePath + mi.Attachments.Item(o).FileName);
int succ = ImportFile(BasePath + mi.Attachments.Item(o).FileName);
//Move the file to a new folder
if (succ == 0)
MoveFile(mi.Attachments.Item(o).FileName);

}
}
}

}
finally
{
olNS.Logoff();
olApp = null;
olNS = null;

}
}
Aug 30 '06 #1
Share this Question
Share on Google+
1 Reply

P: 1
Hi Phil,
I dont have proper solution, but a work around is:
I have used this logic in one of my project....

private static void markUnread ( Outlook.MAPIFolder oInbox )
{
Outlook.Items oItems;

_/* Why do you Required infinite loop ???
* --: No supporting doc available :--
* Every mark read is resets count of unread messages
* So alternate messages are skipped while marking read
* -- Solution : Iterate till all messages are marked as read
* */
while ( true )
{
oItems = oInbox.Items;
// Get unread e-mail messages.
oItems = oItems.Restrict ( "[Unread] = true" );

if ( oItems.Count == 0 ) return;
foreach ( Outlook.MailItem oMsg in oItems )
oMsg.UnRead = false;
}
}


Hope this will help....
Oct 9 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.