473,378 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Outlook.ApplicationClass() Unread emails problem

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
1 6994
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

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

Similar topics

2
by: zee | last post by:
Hello there I would like to create an add-in for outlook which looks at all the new email (unread), if they contain particular text on the subject, then a link gets added to the bottom of the...
0
by: Joseph Kormann | last post by:
I'm trying to tie my application into the local running Outlook application through the Interop.Outlook.DLL. I'm looking for new / unread messages in the Inbox. No problem getting into the Inbox,...
13
by: Joseph Kormann | last post by:
I'm building an application to retrieve emails. Which is better to use: CDO or the Outlook library? Thanks.
4
by: Andy Bates | last post by:
Hi - I am developing an application that needs to automate Microsoft Outlook and I need to capture the ItemSend event. The PC I'm developing with has Outlook 2003 on; the problem I have is I...
6
by: LEBRUN Thomas | last post by:
Hello :) I would like to use Outlook 2003 in my application so for that, i've added the referece to Outlook 11.0 Object Model to my project. Then, I try this simple code : using System;...
1
by: Michele | last post by:
Hi, I need to send the same Email to different people. I'm using Outlook XP and VB.Net. I tryed with the following code: Dim oOutL As Outlook.Application Dim oMail As Outlook._MailItem oOutL...
2
by: JC | last post by:
Anybody knows what problem has this code? I think, in the Garbage Collector? You know the Solution? The program in the test's case, whit 350 contacts, run OK before number 86. The error is a...
2
by: obroie | last post by:
Hi all, I am trying to move emails from Outlook into an Access table, the code below worked for me before, but now I am having a problem as this error message comes up: Run-time error 438 'Object...
6
by: =?Utf-8?B?UmljaA==?= | last post by:
How to suppress the Outlook 2003 security prompts from VB2005. When I tried to look at the body of a message or any other part of an outlook mailItem -- I get these annoying security prompts. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.