Question about ItemAdd() Method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am doing a Add-In for OUTLOOK by C Sharp, but i am getting trouble with a
quite weird problem.
actually, i wish to save the mail details when one mail sent.. but when i
make use of the save methods, which just execute once. Therefore, i use
messageBox to test it. It is quite amazing that sometimes it just execute
twice(Testing CASE1, see following code), sometimes it run three
times(Testing CASE2). However, if i just use messageBox, do not make use of
any other functions and methods, it's right..
I do not where is wrong??? and how to solve it???? I really need your help,
due to this is my final year project, which is so crucial for me!!! MANY
THANKS!!!

I really appreciate any help if anyone give to me....
ROBIN

the following are my codes:

public void OnStartupComplete(ref System.Array custom)
{
sentFolder = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
sentFolder.Items.ItemAdd +=new
ItemsEvents_ItemAddEventHandler(sentFolder_ItemAdd);
}

private void sentFolder_ItemAdd(object Ctrl)
{
MessageBox.Show("Sent Folder Testing1");
Microsoft.Office.Interop.Outlook.MailItem mailItem =
(Microsoft.Office.Interop.Outlook.MailItem) Ctrl;
MessageBox.Show("Sent Folder Testing2");

// Testing CASE1: which just execute twice to disply the testing messages,
in the third time, there is nothing to show
/*
mail_table.Add(mailItem, mailItem);
IDictionaryEnumerator enumerator = mail_table.GetEnumerator();

while(enumerator.MoveNext())
{
Microsoft.Office.Interop.Outlook.MailItem item =
(Microsoft.Office.Interop.Outlook.MailItem) enumerator.Key;
MessageBox.Show(item.SentOn.ToString()+ item.Recipients.Count.ToString() +
item.Subject);
}
*/


//Testing CASE2: the following execute three times to disply the testing
message, there is nothing to show in the fourth time.
//MessageBox.Show(mailItem.SentOn.ToString() + mailItem.Subject);
//MessageBox.Show("Sent Folder Testing3");
}
 
GC removes your event sink when it kicks in since the Items collection is
not referenced. Instead of

sentFolder.Items.ItemAdd +=new
ItemsEvents_ItemAddEventHandler(sentFolder_ItemAdd);

use

m_Items = sentFolder.Items;
m_Items.ItemAdd +=new ItemsEvents_ItemAddEventHandler(sentFolder_ItemAdd);

and make m_Items a class variable to make sure it is always referenced.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Thank you so much for your help. But I have one more question...
I wish to save the details of the mail when user just sent it, so i need
individual mail item, not the collection of mail items, so what should i do
in the sentFolder_ItemAdd()???

Thank you
 
You need to use Application.ItemSend event instead - it will pass the item
in question to your handler.
Also note that a user can drag a message to the Sent Items folder, which
does not necessarily mean that the item was sent. Plus a message can be
saved to a folder other than Sent Items after the submission.
Do use Application.ItemSend.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Back
Top