More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

Login

login
password
Remember me

You will need to register and log in if you want to download the source code for the Microsoft Outlook Programming book. The forums and code sharing areas are open to both registered and non-registered visitors.

forum 09-Sep-2010 06:24

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

 

    Page [ 1 ]  
 Outlook and .NET Printer Friendly Version
Writing code with C# and VB.NET to create Outlook add-ins and other projects
Topic
Send Mails from a specific folder sends only some mails at a time - Please help, Urgent
Dear All,

I have a simple requirement -

1. Add a command bar button on a toolbar.
2. On click of the button send all mails from a particular folder.

What I am doing -
---------------
1. I am using a VSTO addin (VS2008); This specific requirement is part of a huge application
2. Added all command bar button on the toolbar
3. Getting the folder by using - namepace.PickFolder()
3. Looping through all mails in the folder and using mailItem.Send();

Problem -
-------
If there are 10 or 10 plus mails only 5 get sent at a time. Then I have to click the button again and the remaining 5 are released.
If there are 3 mails then 2 mails get sent and one is still remaining the folder.
I have tried everything under the sun, maybe I am missing something. Please do let me know. Thanks in advance.

Code-
-----
CustomButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ButtonClick);

private void ButtonClick(Office.CommandBarButton ButtonContrl, ref bool CancelOption)
{
    Outlook.NameSpace mapiNameSpace = this.Application.GetNamespace("MAPI");
    Outlook.MAPIFolder sendFromFolder = mapiNameSpace.PickFolder();
    //MessageBox.Show(sendFromFolder.Items.Count.ToString()); - This gives me the total count of mails in the specific folder
            
    foreach (Outlook._MailItem mailItem in sendFromFolder.Items)
    {
       mailItem.Send();
    }
}

I have no premission issues or anything. Is this a problem with the server specifications or something instead of the code?
Any help will be appreciated. Thanks in advance.
 

  06-Nov-2009  02:34
  06-Nov-2009  18:04   
Never delete, move, or send items from within a foreach loop. The index resets each time. Instead, use a down-counting loop, counting from sendFromFolder.Items.Count down to 1 and getting each item with the Items.Item method.
    Page [ 1 ]  
Post a new message to this topic.



name        email