Import msg C#

A

Andrih

hi,
i have a working code in vb, that imports msg-files (Outlook Messages,
no contacts or tasks) based on the sample from the redemption
homepage.
Now i need to translate this to C#, but it does not work for me.
I use OL 2003 and Win XP.
My Code is:
ApplicationClass oOLApp = ApplicationClass();
NameSpace oNameSpace = oOLApp.GetNamespace("MAPI");
PostItem oItem = (PostItem)oOLApp.CreateItem(OlItemType.olPostItem);
oItem.Save();
string strEntryID = oItem.EntryID;
oItem.Close( OlInspectorClose.olSave );
oItem = null;
oItem = (PostItem)oOLApp.Session.GetItemFromID( strEntryID,
oNameSpace.Folders["Andre"].StoreID );
oItem.MessageClass = "IPM.Note";
Redemption.SafeMailItem oRedemptionItem = new SafeMailItem();
oRedemptionItem.Item = oItem;
oRedemptionItem.Import( _strItemPath, 3 ); //SafeMail Item Class has
no save method in .net, in my VB Sample here i call
RedemptionItem.Save...

This code is working only half. The item is available in Outlook, but
it is shown as a discussion, as long as my application is started.
When i close the application, that has imported the item, it´s
available as a message, as it should be. When i watch the IMessage
Properties before and after i close my application, they are exactly
the same, once it is shown as a discussion, once normal.
What am i doing wrong?
Thanks in advance!
André
 
D

Dmitry Streblechenko \(MVP\)

..Net does not release COM objects immediately after they are set to null
(oItem = null), you must force the GC to do that explicitly
(Marshal.ReleaseCOMObject). Otherwise Outlook still caches that item and you
do not see the changes until you restart Outlook (or until the GC kicks in).

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

Andrih

Thanks for your reply,
i still heard of that and in one modification of my code i tested it
the following:
while (Marshal.ReleaseCOMObject(oItem) > 0);
I also did this with Namespace, application and all other objects, but
it also doesn´t work for me (same problem as before).
I wonder why i can use save method for SafeMailItem in VB and not with
..net, although it is not shown with intellisense, neither in .net, nor
in VB.
Is the problem, that i cannot save my changes to the SafeMailItem
Object?
Thanks in advance,
André

Dmitry Streblechenko \(MVP\) said:
.Net does not release COM objects immediately after they are set to null
(oItem = null), you must force the GC to do that explicitly
(Marshal.ReleaseCOMObject). Otherwise Outlook still caches that item and you
do not see the changes until you restart Outlook (or until the GC kicks in).

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


Andrih said:
hi,
i have a working code in vb, that imports msg-files (Outlook Messages,
no contacts or tasks) based on the sample from the redemption
homepage.
Now i need to translate this to C#, but it does not work for me.
I use OL 2003 and Win XP.
My Code is:
ApplicationClass oOLApp = ApplicationClass();
NameSpace oNameSpace = oOLApp.GetNamespace("MAPI");
PostItem oItem = (PostItem)oOLApp.CreateItem(OlItemType.olPostItem);
oItem.Save();
string strEntryID = oItem.EntryID;
oItem.Close( OlInspectorClose.olSave );
oItem = null;
oItem = (PostItem)oOLApp.Session.GetItemFromID( strEntryID,
oNameSpace.Folders["Andre"].StoreID );
oItem.MessageClass = "IPM.Note";
Redemption.SafeMailItem oRedemptionItem = new SafeMailItem();
oRedemptionItem.Item = oItem;
oRedemptionItem.Import( _strItemPath, 3 ); //SafeMail Item Class has
no save method in .net, in my VB Sample here i call
RedemptionItem.Save...

This code is working only half. The item is available in Outlook, but
it is shown as a discussion, as long as my application is started.
When i close the application, that has imported the item, it´s
available as a message, as it should be. When i watch the IMessage
Properties before and after i close my application, they are exactly
the same, once it is shown as a discussion, once normal.
What am i doing wrong?
Thanks in advance!
André
 
D

Dmitry Streblechenko \(MVP\)

Did you try to get rid of
oItem.Close( OlInspectorClose.olSave );

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


Andrih said:
Thanks for your reply,
i still heard of that and in one modification of my code i tested it
the following:
while (Marshal.ReleaseCOMObject(oItem) > 0);
I also did this with Namespace, application and all other objects, but
it also doesn´t work for me (same problem as before).
I wonder why i can use save method for SafeMailItem in VB and not with
.net, although it is not shown with intellisense, neither in .net, nor
in VB.
Is the problem, that i cannot save my changes to the SafeMailItem
Object?
Thanks in advance,
André

"Dmitry Streblechenko \(MVP\)" <[email protected]> wrote in message
.Net does not release COM objects immediately after they are set to null
(oItem = null), you must force the GC to do that explicitly
(Marshal.ReleaseCOMObject). Otherwise Outlook still caches that item and you
do not see the changes until you restart Outlook (or until the GC kicks in).

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


Andrih said:
hi,
i have a working code in vb, that imports msg-files (Outlook Messages,
no contacts or tasks) based on the sample from the redemption
homepage.
Now i need to translate this to C#, but it does not work for me.
I use OL 2003 and Win XP.
My Code is:
ApplicationClass oOLApp = ApplicationClass();
NameSpace oNameSpace = oOLApp.GetNamespace("MAPI");
PostItem oItem = (PostItem)oOLApp.CreateItem(OlItemType.olPostItem);
oItem.Save();
string strEntryID = oItem.EntryID;
oItem.Close( OlInspectorClose.olSave );
oItem = null;
oItem = (PostItem)oOLApp.Session.GetItemFromID( strEntryID,
oNameSpace.Folders["Andre"].StoreID );
oItem.MessageClass = "IPM.Note";
Redemption.SafeMailItem oRedemptionItem = new SafeMailItem();
oRedemptionItem.Item = oItem;
oRedemptionItem.Import( _strItemPath, 3 ); //SafeMail Item Class has
no save method in .net, in my VB Sample here i call
RedemptionItem.Save...

This code is working only half. The item is available in Outlook, but
it is shown as a discussion, as long as my application is started.
When i close the application, that has imported the item, it´s
available as a message, as it should be. When i watch the IMessage
Properties before and after i close my application, they are exactly
the same, once it is shown as a discussion, once normal.
What am i doing wrong?
Thanks in advance!
André
 
A

Andrih

Thanks again, but i also still tried this.
I tried also any combination of closing items or not with and without
releasecomobject, the only thing i could not do is calling "save" for
the safemailitem.
Tanks in advance, André

Dmitry Streblechenko \(MVP\) said:
Did you try to get rid of
oItem.Close( OlInspectorClose.olSave );

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


Andrih said:
Thanks for your reply,
i still heard of that and in one modification of my code i tested it
the following:
while (Marshal.ReleaseCOMObject(oItem) > 0);
I also did this with Namespace, application and all other objects, but
it also doesn´t work for me (same problem as before).
I wonder why i can use save method for SafeMailItem in VB and not with
.net, although it is not shown with intellisense, neither in .net, nor
in VB.
Is the problem, that i cannot save my changes to the SafeMailItem
Object?
Thanks in advance,
André

"Dmitry Streblechenko \(MVP\)" <[email protected]> wrote in message
.Net does not release COM objects immediately after they are set to null
(oItem = null), you must force the GC to do that explicitly
(Marshal.ReleaseCOMObject). Otherwise Outlook still caches that item and you
do not see the changes until you restart Outlook (or until the GC kicks in).

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


hi,
i have a working code in vb, that imports msg-files (Outlook Messages,
no contacts or tasks) based on the sample from the redemption
homepage.
Now i need to translate this to C#, but it does not work for me.
I use OL 2003 and Win XP.
My Code is:
ApplicationClass oOLApp = ApplicationClass();
NameSpace oNameSpace = oOLApp.GetNamespace("MAPI");
PostItem oItem = (PostItem)oOLApp.CreateItem(OlItemType.olPostItem);
oItem.Save();
string strEntryID = oItem.EntryID;
oItem.Close( OlInspectorClose.olSave );
oItem = null;
oItem = (PostItem)oOLApp.Session.GetItemFromID( strEntryID,
oNameSpace.Folders["Andre"].StoreID );
oItem.MessageClass = "IPM.Note";
Redemption.SafeMailItem oRedemptionItem = new SafeMailItem();
oRedemptionItem.Item = oItem;
oRedemptionItem.Import( _strItemPath, 3 ); //SafeMail Item Class has
no save method in .net, in my VB Sample here i call
RedemptionItem.Save...

This code is working only half. The item is available in Outlook, but
it is shown as a discussion, as long as my application is started.
When i close the application, that has imported the item, it´s
available as a message, as it should be. When i watch the IMessage
Properties before and after i close my application, they are exactly
the same, once it is shown as a discussion, once normal.
What am i doing wrong?
Thanks in advance!
André
 
Top