Re: problems deleting email messages in c#
- From: "Ken Slovak - [MVP - Outlook]" <kenslovak@xxxxxxxx>
- Date: Wed, 6 Dec 2006 09:07:36 -0500
Deleting items from a collection requires either a count down for loop or another looping construct that tests for the collection being empty. Using a foreach or other count up loop will do what you see. That's because as you delete items from the collection the loop counter is changing so what was item 2 becomes item 1 and so on. So you only get 1/2 the items in each pass. BTW, foreach is a slow loop. A for loop that counts down (blah; blah; i--) is faster.
Hard-deleting an item (bypassing Deleted Items) is only possible using CDO 1.21 or Extended MAPI (C++ or Delphi only) or a COM wrapper for MAPI such as Redemption (www.dimastr.com/redemption). Both CDO and Extended MAPI are unsupported for .NET code.
An alternative is to handle ItemAdd on the Items collection of the Deleted Items folder. After you delete the item the event will fire and you can delete the item that was just added to Deleted Items.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm
"Socketwiz" <Socketwiz@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:2E6F3FFF-D94D-428D-A5D0-F3C19C24CD63@xxxxxxxxxxxxxxxx
I hope this is the right place to post, if not then please direct me to the
correct group.
I am putting together a personal project to help me remove spam out of my
spam folder. Here is the problem I am trying to solve. I use SPAMFighter
exchange edition to filter my spam. It puts the spam into a folder called
SPAMFighter. There is no way to change this with the exchange edition. I
have also not found a way to change the Outlook "Junk E-Mail" folder to the
SPAMFighter folder. If I could do that then I could use the "empty junk
e-mail folder" option and life would be good. The problem is that my profile
contains multiple mailboxes. SPAMFighter provides a way to flush the spam
out of the SPAMFighter folder without putting it into the "deleted items"
folder but it only does that for the default mailbox. I would like to flush
the spam out of all of the mailbox folders without putting the messages into
the "deleted items" folder. So I started to put together a c# application to
help me do this. I'm almost there but I have two problems that I can't seem
to get past. One is when I delete the messages they go to the "deleted
items" folder. Second is that only half of the messages get deleted each
time I push the "flush spam" button that I made. I am using vs2005
professional and I have outlook 2007 installed. My entire project can be
found here: http://software.blizzard-media.com/spamFolderFlush.zip but here
is the loop that is giving me grief:
foreach (object Item in folder.Items)
{
// Check the Item Type
if (Item is Outlook.MailItem)
{
// Cast to MailItem
Outlook.MailItem myMailItem = (Outlook.MailItem)Item;
if (SHOWITEMS == iAction)
{
if (null != myMailItem.Subject)
{
lstSpamItems.Items.Add(myMailItem.Subject);
}
else
{
lstSpamItems.Items.Add("EMPTY SUBJECT");
}
}
else if (DELITEMS == iAction)
{
txtLogging.AppendText("DELETING: " +
myMailItem.Subject + "\r\n");
lblDeleted.Text = "Deleted: " + iItems++;
myMailItem.Delete();
}
}
}
Does anyone have any ideas why only half the messages are deleted when I
push the "flush spam" button? And what other API could I use to delete a
mail item without sending it to the "deleted items" folder?
.
- Follow-Ups:
- Re: problems deleting email messages in c#
- From: Socketwiz
- Re: problems deleting email messages in c#
- References:
- problems deleting email messages in c#
- From: Socketwiz
- problems deleting email messages in c#
- Prev by Date: Re: OutlookBarGroups in Navigation Bar (OL 2003)
- Next by Date: Re: OutlookBarGroups in Navigation Bar (OL 2003)
- Previous by thread: problems deleting email messages in c#
- Next by thread: Re: problems deleting email messages in c#
- Index(es):