how to add new contacts via COM

  • Thread starter Thread starter Sam Jost
  • Start date Start date
S

Sam Jost

Hi,

I'm writing a little c# program to import contacts into outlook. It is
simple enough to add a new contact like this:

Outlook.ContactItem newItem =
(Outlook.ContactItem)outlookApp.CreateItem(Outlook.OlItemType.olContactItem);
newItem.LastName= "Jost";
newItem.FirstName= "Sam";
newItem.Close(Outlook.OlInspectorClose.olSave);

this code snipped does work nicely, even for a few hundrets of contacts.

My problem is, I'd like to add these contacts not in the standard folder but
in some other. When I try using Move() to put the contacts into the
destination folder like this it does work for about 200 contacts before I
get an exception

Outlook.ContactItem newItem =
(Outlook.ContactItem)outlookApp.CreateItem(Outlook.OlItemType.olContactItem);
newItem.LastName= "Jost";
newItem.FirstName= "Sam";
newItem.Move(MyOutlookKontaktFolder);
newItem.Close(Outlook.OlInspectorClose.olDiscard);

Error : Client-Exception.
at Outlook._ContactItem.Move(MAPIFolder DestFldr)
at OutlookExporter.Class1.Sync() in
c:\pw\outlookexporter\outlookexporter.cs:line 197

And the last contact will be in the default folder instead of the
destination folder.

My guess is I'm using Move() incorrectly, or miss something like Dispose()
(that I cant find on these classes) so I got a memory leak or something.

Can anyboy help me here, or point me somewhere I can get help?

thanks,
Sam
 
Just to note: I tried the following code, and it does crash after adding 250
contact items, too.
The only difference it made is there was no contact created in the default
contact folder, so it is a tad bit better. But not much :(


Outlook.ContactItem newItem =
(Outlook.ContactItem)outlookKontaktFolder.Items.Add("IPM.Contact");
newItem.LastName= "Jost";
newItem.FirstName= "Sam";
newItem.Close(Outlook.OlInspectorClose.olSave);

Sam
 
Dang, found the cause of my problem:

After creating 250 Mapi objects I get the following log error:
Event ID: 9646
Type: Error
Source: MSExchangeIS
Description:
Closing Mapi session "/o=Organization/ou=Administrative
Group/cn=Recipients/cn=user" because it exceeded the maximum of 250 objects
of type "objMessage".

What can I do in my program to work around this 250 limitation? I might
close the connection every 250 items, but it seems wrong to me, ain't there
some better way to flush the items to the server and thus reduze the count
of objects?

Sam
 
Back
Top