RDO objects and C#

D

dskwarek

I'm testing Outlook Redemption component (4.4.0.714) with C# project.

I have problem with creating of RDO instances.

I'm using Visual Studio 2005.
I added Redemption component to my project using
AddReference -> COM -> Redemption Outlook Library 4.4.

I tried to create instance of RDOContactItemClass.

RDOSessionClass session = new RDOSessionClass();
session.Logon("", "", false, false, null, false);
RDOFolder folder =
session.GetDefaultFolder(rdoDefaultFolders.olFolderContacts);
IRDOMail item = folder.Items.Add("IPM.Contact");

RDOContactItem contact = item as RDOContactItem; // NULL !!!


Method Items.Add returns instance which implements RDOMail interface.
I've found in Outlook Redemption documentation that RDOContactItem is
derived from the RDOMail object.

There isn't true for interop assembly created by Visual Studio.

RDOContactItem is interface and implements IRDOContactItem which
implements IRDOMail.

Visual Studio creates RDOContactItemClass which implements
RDOContactItem and IRDOContactItem.


There is no RDOMail in inheritance hierarchy of RDOContactItem.

How to add new instance of RDOContactItemClass to RDOItems collection
?

Using Items.Add method I can create only instance of RDOMailClass.

What am I doing wrong ?
 
K

Ken Slovak - [MVP - Outlook]

I've had no problems doing it this way:

RDOSession session = new RDOSession();
session.Logon("", "", false, false, null, false);

RDOFolder folder =
session.GetDefaultFolder(rdoDefaultFolders.olFolderContacts);

RDOMail item = folder.Items.Add("IPM.Contact");

RDOContactItem contact = (RDOContactItem)item;

More often I'd do it this way:

RDOContactItem contact =
(RDOContactItem)folder.Items.Add("IPM.Contact");
 
D

dskwarek

Thank you for response.

I've posted wrong example.
The cast which doesn't work is RDOContactItemClass contact = item as
RDOContactItemClass;
This line returns null, but RDOContactItem contact = item as
RDOContactItem works and your example also.

What is amazing that method Items.Add("IPM.Contacts") return instance
of RDOMailClass which
doesn't implement RDOContactItem.

I think when tlbimp.exe generates interop assembly
than overrides cast operators.
Maybe this is workaround for C++ multi-inheritance which is not
present in C#?
 
K

Ken Slovak - [MVP - Outlook]

I don't know. I've always generally used a RDOMail item instead of the class
version except where I need to add event handlers that aren't in the
non-class version. Offhand I don't recall if RDOMail has any of those but
the same applies to other various objects.

At least you now know of a method that does work.
 
D

Dmitry Streblechenko

This is fairly standard and is not limited to Redemption at all.
Most COM objects do not support QueryInterface on the corresponding
CLASS_xyz GUID (since it is really an id and does not correspond to an
interface).
On the .Net level, avoid declaring and casting COM objects to the XYZClass
(e.g. RDOContactItemClass).

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

AK

Hi,

I'm also getting the same kind of exception as below.

"Unable to cast COM object of type 'System.__ComObject' to interface type
'Redemption.RDOContactItem'. This operation failed because the QueryInterface
call on the COM component for the interface with IID
'{DA18E730-205D-4CED-9E61-4DBBC35D2EE5}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))."

I'm very new to the Redemption and it's object model.

What I'm trying to do is as follows.

"To retrieve all mailbox accounts from the domain (example xyz.com), once
retrieved a configurable number of worker threads will be spawned. Each
worker thread will take a single mailbox account and query Active Directory
to see which Exchange Server the mailbox account resides on. The worker
thread will then trawl through each message in its given mailbox’s account
checking its Read, Received Date and the Mileage properties. If the mail
message meets the given criteria (mail item has been read, is older than x
months etc) the email item will be logged and if in the correct mode deleted.
Once a mailbox has been fully processes it will go on for next mailbox".

I've few queries as below.

1. How to retrieve all mailbox accounts from the domain (e.g. xyz.com)?
2. Can you please let me know what objects in Redemption object model will
be helpful for this?
3. Do I have to use ProfMan library?

Please help me. As mentioned I'm very new to Redemption or for that matter
exchange or outlook programming.

Many Thanks in advance.
Amit Kathane
 
D

Dmitry Streblechenko

Where does the item come from and why are you querying it for
RDOContactItem? Does it come from the Contacts folder? Most likely you
encounter a Distribution list, which is represented by the RDODistListItem
object.

As for what you are actually trying to do, the standard way to do things
like that is to

1. Run your code under the Windows user identity who can access all of the
required mailboxes
2. Call RDOSession.Logon if you have an existing profilee pointing to that
user's mailbox or call RDOSession.LogonExchangeMailbox
3. Loop through the entries in the RDOSession.AddressBook.GAL;
4. For each RDOAddressEntry with DisplayType property equal to DT_MAILUSER
(= 0)
5. Call RDOSession.GetSharedMailbox passing that RDOAddressEntry object (get
back RDOExchangeMailboxStore)
6. Loop through all subfolder and messages in tha tmailbox store stating
with the RDOStore.IPMRootFolder collection

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

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

AK

Hi,

Thanks you very much for your reply.

I'm understanding bit of Redemeption library now.

One issue is when I'm trying to do login using
RDOSession.LogonExchangeMailbox

it's popping up the UI.

According to my requirement, I've to traverse through all mailbox which I
guess will require login into each mailbox and a pop up UI could be an issue?

This UI does not come when using RDOSession.Logon method, is there anything
I'm missing while using RDOSession.LogonExchangeMailbox or is there any
workaround to my requirement.

Also what exctly is the usage of ProfMan library? Is it only needed when we
don't have a user profile to logon so that one can create a new profile?

Once again many thanks for your help.
 
D

Dmitry Streblechenko

Once again, see steps 1 and 2. You need to call LogonExchangeMailbox *once*
for the current Windows user who must have the right to access othe
rmailboxes.
To access other mailboxess call RDOSession.GetSharedMailbox. Do *not* call
LogonExchangeMailbox for the other mailboxes.

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

AK

Hello Dmitry,

Thank you very much for your reply.

According to your solution mentioned in one of the above responses i.e.

1. Run your code under the Windows user identity who can access all of the
required mailboxes
2. Call RDOSession.Logon if you have an existing profilee pointing to that
user's mailbox or call RDOSession.LogonExchangeMailbox
3. Loop through the entries in the RDOSession.AddressBook.GAL;
4. For each RDOAddressEntry with DisplayType property equal to DT_MAILUSER
(= 0)
5. Call RDOSession.GetSharedMailbox passing that RDOAddressEntry object (get
back RDOExchangeMailboxStore)
6. Loop through all subfolder and messages in tha tmailbox store stating
with the RDOStore.IPMRootFolder collection

Will there be problem in step 3 and 4 if there is a multiple exchange server
box in the domain and various users are configured with different exchange
box. Will I still be able to get all user's RDOAddressEntry object in
RDOSession.AddressBook.GAL or will it give only those users which are on the
same exchange box as of the Windows user under which the application is
running?

Also I assume that RDOSession.Logon using the existing profile of using
RDOSession.LogonExchangeMailbox are exclusive. I guess I don't have to create
the profile if it's not there and can use the RDOSession.LogonExchangeMailbox
method instead. Is that right?

Many Thanks for your help.
Amit Kathane
 
D

Dmitry Streblechenko

1. Exchange transparently manages the locatiton of the mailboxes. It will
list all mailboxes in the organization. As an extra check, you might want to
read the PR_EMS_AB_HOME_MDB property using RDOAddressEntry.Fields[] (the
property returns the name of the server where teh user has a mailbox) -
there can be entries that have no mailboxes.
2. Yes, use either (but not both) Logon or LogonExchangeMailbox. Or set the
MAPIOBJECT property if you already have a running instance of Outlook (use
Namespace.MAPIOBJECT) or CDO 1.21 session (Session.MAPIOBJECT).

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

AK

Hello Dmitry,

Thank you very much for your assistance.

I'm getting following error randomly.

"Error in IMAPIFolder.GetContentsTable: MAPI_E_CALL_FAILED"

Please find below my code

======================================
private static void ProcessUserMailbox(string userName)
{
RDOSession rdoSession = new RDOSessionClass();
try
{
// get the admin user name from the configuration file
string systemUserName = Properties.Settings.Default.userName;
string exchangeSrvrName =
Properties.Settings.Default.UserMailboxServer;

if (string.IsNullOrEmpty(systemUserName))
systemUserName =
WindowsIdentity.GetCurrent().Name.ToString();

rdoSession.LogonExchangeMailbox(systemUserName,
exchangeSrvrName);
IRDOFolder rootFolder =
rdoSession.GetSharedMailbox(userName).RootFolder;

// iterate through all folders and subfolders
RDOFolders rdoFolders = rootFolder.Folders;
if (rdoFolders != null)
{
RDOFolder rdoFolder = rdoFolders.GetFirst();
if (rdoFolder != null)
{
// iterate folder list
for (int i = 0; i < rdoFolders.Count; i++)
{
// process the individual folder to search for
emails that can be deleted
ProcessFolder(userName, rootFolder.Name + " > ",
rdoFolder);
rdoFolder = rdoFolders.GetNext();
}
}
}
// release the COM object for the root folder.

System.Runtime.InteropServices.Marshal.ReleaseComObject(rootFolder);
rootFolder = null;
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{0}, Failed to process mailbox for user : {0}",
userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
strInfo.AppendFormat(ex.Message + Environment.NewLine +
Util.GetInnerException(ex));
throw new ApplicationException(strInfo.ToString());
}
finally
{
rdoSession.Logoff();

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoSession);
rdoSession = null;
GC.Collect();
}
}

private static void ProcessFolder(string userName, string
folderPath, RDOFolder rdoFolder)
{
RDOFolders subFolders;
RDOFolder subFolder;

try
{
subFolders = rdoFolder.Folders;
if (subFolders != null)
{
subFolder = subFolders.GetFirst();
if (subFolder != null)
{
// iterate sub folder list
for (int i = 0; i < subFolders.Count; i++)
{
// process the subfolder
ProcessFolder(userName, rdoFolder.Name + " >
", subFolder);
subFolder = subFolders.GetNext();
}
}
}
// if found an individual folder (rdoFolder) without the
subfolder then release the subfolder and subfolders COM objects

//System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolder);
subFolder = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolders);
subFolders = null;

// if there are no sub folders then process the
rdoFolder and iterate through all items
// process the individual items/emails in the folder
for (int i = 0; i < rdoFolder.Items.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead,
rdoMail.ReceivedTime, rdoMail.Mileage))
{
Util.LogMessage(string.Format("{0}, Email
with subject '{1}' Read : '{2}' Received Time : '{3}' Mileage : '{4}' is
deleted.", userName, rdoMail.Subject, !(rdoMail.UnRead),
rdoMail.ReceivedTime, rdoMail.Mileage));
if (DeleteMail)
rdoMail.Delete(0); //dfSoftDelete (0) -
default. Deletes the item. Can still be recoverable if retention policy is
set on Exchange Server.
//dfMoveToDeletedItems
(1) - the item is moved to the Deleted Items folder
//dfHardDelete (2) -
Exchange only. Permanently deletes the item; will not be recoverable
}
}
catch (Exception ex)
{
// in case of exception for processing
individual mail log the exception and process next email
Util.LogException(ex);
}
finally
{

//System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
}
}
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{1}, Failed to process folder : {0} for user :
{1}", rdoFolder.Name, userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoFolder);
rdoFolder = null;
}
}
==================================================

The above error is coming for some of the emails for user. Can you please
let me know what could be the reason of this error.

Thanks and Regards,
Amit Kathane

Dmitry Streblechenko said:
1. Exchange transparently manages the locatiton of the mailboxes. It will
list all mailboxes in the organization. As an extra check, you might want to
read the PR_EMS_AB_HOME_MDB property using RDOAddressEntry.Fields[] (the
property returns the name of the server where teh user has a mailbox) -
there can be entries that have no mailboxes.
2. Yes, use either (but not both) Logon or LogonExchangeMailbox. Or set the
MAPIOBJECT property if you already have a running instance of Outlook (use
Namespace.MAPIOBJECT) or CDO 1.21 session (Session.MAPIOBJECT).

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

AK said:
Hello Dmitry,

Thank you very much for your reply.

According to your solution mentioned in one of the above responses i.e.

1. Run your code under the Windows user identity who can access all of the
required mailboxes
2. Call RDOSession.Logon if you have an existing profilee pointing to that
user's mailbox or call RDOSession.LogonExchangeMailbox
3. Loop through the entries in the RDOSession.AddressBook.GAL;
4. For each RDOAddressEntry with DisplayType property equal to DT_MAILUSER
(= 0)
5. Call RDOSession.GetSharedMailbox passing that RDOAddressEntry object
(get
back RDOExchangeMailboxStore)
6. Loop through all subfolder and messages in tha tmailbox store stating
with the RDOStore.IPMRootFolder collection

Will there be problem in step 3 and 4 if there is a multiple exchange
server
box in the domain and various users are configured with different exchange
box. Will I still be able to get all user's RDOAddressEntry object in
RDOSession.AddressBook.GAL or will it give only those users which are on
the
same exchange box as of the Windows user under which the application is
running?

Also I assume that RDOSession.Logon using the existing profile of using
RDOSession.LogonExchangeMailbox are exclusive. I guess I don't have to
create
the profile if it's not there and can use the
RDOSession.LogonExchangeMailbox
method instead. Is that right?

Many Thanks for your help.
Amit Kathane
 
D

Dmitry Streblechenko

You are most likely running out of the RPC channel limit
(http://support.microsoft.com/kb/830829) - since .Net does not release COM
objects immediately, you can easily go over the limit even if you explicitly
release COM objects using Marshal.ReleaseCOMObject.
Avoid using multiple dot notation (e.g. rdoFolder.Items.Count) to make sure
the compiler does not create implicit variables that you cannot reference
and release.
GC.Collect() might help.
Next version of Redemption (send me an e-mail if you want a beta version)
will keep track of the open MAPI objeects and transparently release oldest
unmodified MAPI objects that can be easily reopened on demand (Redemption
objects will stay alive of course) when the number of open objects of a
particular kind approaches the limit.

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

AK said:
Hello Dmitry,

Thank you very much for your assistance.

I'm getting following error randomly.

"Error in IMAPIFolder.GetContentsTable: MAPI_E_CALL_FAILED"

Please find below my code

======================================
private static void ProcessUserMailbox(string userName)
{
RDOSession rdoSession = new RDOSessionClass();
try
{
// get the admin user name from the configuration file
string systemUserName =
Properties.Settings.Default.userName;
string exchangeSrvrName =
Properties.Settings.Default.UserMailboxServer;

if (string.IsNullOrEmpty(systemUserName))
systemUserName =
WindowsIdentity.GetCurrent().Name.ToString();

rdoSession.LogonExchangeMailbox(systemUserName,
exchangeSrvrName);
IRDOFolder rootFolder =
rdoSession.GetSharedMailbox(userName).RootFolder;

// iterate through all folders and subfolders
RDOFolders rdoFolders = rootFolder.Folders;
if (rdoFolders != null)
{
RDOFolder rdoFolder = rdoFolders.GetFirst();
if (rdoFolder != null)
{
// iterate folder list
for (int i = 0; i < rdoFolders.Count; i++)
{
// process the individual folder to search for
emails that can be deleted
ProcessFolder(userName, rootFolder.Name + " >
",
rdoFolder);
rdoFolder = rdoFolders.GetNext();
}
}
}
// release the COM object for the root folder.

System.Runtime.InteropServices.Marshal.ReleaseComObject(rootFolder);
rootFolder = null;
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{0}, Failed to process mailbox for user :
{0}",
userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
strInfo.AppendFormat(ex.Message + Environment.NewLine +
Util.GetInnerException(ex));
throw new ApplicationException(strInfo.ToString());
}
finally
{
rdoSession.Logoff();

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoSession);
rdoSession = null;
GC.Collect();
}
}

private static void ProcessFolder(string userName, string
folderPath, RDOFolder rdoFolder)
{
RDOFolders subFolders;
RDOFolder subFolder;

try
{
subFolders = rdoFolder.Folders;
if (subFolders != null)
{
subFolder = subFolders.GetFirst();
if (subFolder != null)
{
// iterate sub folder list
for (int i = 0; i < subFolders.Count; i++)
{
// process the subfolder
ProcessFolder(userName, rdoFolder.Name + "", subFolder);
subFolder = subFolders.GetNext();
}
}
}
// if found an individual folder (rdoFolder) without
the
subfolder then release the subfolder and subfolders COM objects

//System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolder);
subFolder = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolders);
subFolders = null;

// if there are no sub folders then process the
rdoFolder and iterate through all items
// process the individual items/emails in the folder
for (int i = 0; i < rdoFolder.Items.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead,
rdoMail.ReceivedTime, rdoMail.Mileage))
{
Util.LogMessage(string.Format("{0}, Email
with subject '{1}' Read : '{2}' Received Time : '{3}' Mileage : '{4}' is
deleted.", userName, rdoMail.Subject, !(rdoMail.UnRead),
rdoMail.ReceivedTime, rdoMail.Mileage));
if (DeleteMail)
rdoMail.Delete(0); //dfSoftDelete (0) -
default. Deletes the item. Can still be recoverable if retention policy is
set on Exchange Server.

//dfMoveToDeletedItems
(1) - the item is moved to the Deleted Items folder
//dfHardDelete (2) -
Exchange only. Permanently deletes the item; will not be recoverable
}
}
catch (Exception ex)
{
// in case of exception for processing
individual mail log the exception and process next email
Util.LogException(ex);
}
finally
{

//System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
}
}
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{1}, Failed to process folder : {0} for user
:
{1}", rdoFolder.Name, userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoFolder);
rdoFolder = null;
}
}
==================================================

The above error is coming for some of the emails for user. Can you please
let me know what could be the reason of this error.

Thanks and Regards,
Amit Kathane

Dmitry Streblechenko said:
1. Exchange transparently manages the locatiton of the mailboxes. It will
list all mailboxes in the organization. As an extra check, you might want
to
read the PR_EMS_AB_HOME_MDB property using RDOAddressEntry.Fields[] (the
property returns the name of the server where teh user has a mailbox) -
there can be entries that have no mailboxes.
2. Yes, use either (but not both) Logon or LogonExchangeMailbox. Or set
the
MAPIOBJECT property if you already have a running instance of Outlook
(use
Namespace.MAPIOBJECT) or CDO 1.21 session (Session.MAPIOBJECT).

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

AK said:
Hello Dmitry,

Thank you very much for your reply.

According to your solution mentioned in one of the above responses i.e.

1. Run your code under the Windows user identity who can access all of
the
required mailboxes
2. Call RDOSession.Logon if you have an existing profilee pointing to
that
user's mailbox or call RDOSession.LogonExchangeMailbox
3. Loop through the entries in the RDOSession.AddressBook.GAL;
4. For each RDOAddressEntry with DisplayType property equal to
DT_MAILUSER
(= 0)
5. Call RDOSession.GetSharedMailbox passing that RDOAddressEntry object
(get
back RDOExchangeMailboxStore)
6. Loop through all subfolder and messages in tha tmailbox store
stating
with the RDOStore.IPMRootFolder collection

Will there be problem in step 3 and 4 if there is a multiple exchange
server
box in the domain and various users are configured with different
exchange
box. Will I still be able to get all user's RDOAddressEntry object in
RDOSession.AddressBook.GAL or will it give only those users which are
on
the
same exchange box as of the Windows user under which the application is
running?

Also I assume that RDOSession.Logon using the existing profile of using
RDOSession.LogonExchangeMailbox are exclusive. I guess I don't have to
create
the profile if it's not there and can use the
RDOSession.LogonExchangeMailbox
method instead. Is that right?

Many Thanks for your help.
Amit Kathane

:

Once again, see steps 1 and 2. You need to call LogonExchangeMailbox
*once*
for the current Windows user who must have the right to access othe
rmailboxes.
To access other mailboxess call RDOSession.GetSharedMailbox. Do *not*
call
LogonExchangeMailbox for the other mailboxes.

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

Hi,

Thanks you very much for your reply.

I'm understanding bit of Redemeption library now.

One issue is when I'm trying to do login using
RDOSession.LogonExchangeMailbox

it's popping up the UI.

According to my requirement, I've to traverse through all mailbox
which
I
guess will require login into each mailbox and a pop up UI could be
an
issue?

This UI does not come when using RDOSession.Logon method, is there
anything
I'm missing while using RDOSession.LogonExchangeMailbox or is there
any
workaround to my requirement.

Also what exctly is the usage of ProfMan library? Is it only needed
when
we
don't have a user profile to logon so that one can create a new
profile?

Once again many thanks for your help.




:

Where does the item come from and why are you querying it for
RDOContactItem? Does it come from the Contacts folder? Most likely
you
encounter a Distribution list, which is represented by the
RDODistListItem
object.

As for what you are actually trying to do, the standard way to do
things
like that is to

1. Run your code under the Windows user identity who can access all
of
the
required mailboxes
2. Call RDOSession.Logon if you have an existing profilee pointing
to
that
user's mailbox or call RDOSession.LogonExchangeMailbox
3. Loop through the entries in the RDOSession.AddressBook.GAL;
4. For each RDOAddressEntry with DisplayType property equal to
DT_MAILUSER
(= 0)
5. Call RDOSession.GetSharedMailbox passing that RDOAddressEntry
object
(get
back RDOExchangeMailboxStore)
6. Loop through all subfolder and messages in tha tmailbox store
stating
with the RDOStore.IPMRootFolder collection

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

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

Hi,

I'm also getting the same kind of exception as below.

"Unable to cast COM object of type 'System.__ComObject' to
interface
type
'Redemption.RDOContactItem'. This operation failed because the
QueryInterface
call on the COM component for the interface with IID
'{DA18E730-205D-4CED-9E61-4DBBC35D2EE5}' failed due to the
following
error:
No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))."

I'm very new to the Redemption and it's object model.

What I'm trying to do is as follows.

"To retrieve all mailbox accounts from the domain (example
xyz.com),
once
retrieved a configurable number of worker threads will be
spawned.
Each
worker thread will take a single mailbox account and query Active
Directory
to see which Exchange Server the mailbox account resides on. The
worker
thread will then trawl through each message in its given
mailbox's
account
checking its Read, Received Date and the Mileage properties. If
the
mail
message meets the given criteria (mail item has been read, is
older
than x
months etc) the email item will be logged and if in the correct
mode
deleted.
Once a mailbox has been fully processes it will go on for next
mailbox".

I've few queries as below.

1. How to retrieve all mailbox accounts from the domain (e.g.
xyz.com)?
2. Can you please let me know what objects in Redemption object
model
will
be helpful for this?
3. Do I have to use ProfMan library?

Please help me. As mentioned I'm very new to Redemption or for
that
matter
exchange or outlook programming.

Many Thanks in advance.
Amit Kathane



:

This is fairly standard and is not limited to Redemption at all.
Most COM objects do not support QueryInterface on the
corresponding
CLASS_xyz GUID (since it is really an id and does not correspond
to
an
interface).
On the .Net level, avoid declaring and casting COM objects to
the
XYZClass
(e.g. RDOContactItemClass).

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

Thank you for response.

I've posted wrong example.
The cast which doesn't work is RDOContactItemClass contact =
item
as
RDOContactItemClass;
This line returns null, but RDOContactItem contact = item as
RDOContactItem works and your example also.

What is amazing that method Items.Add("IPM.Contacts") return
instance
of RDOMailClass which
doesn't implement RDOContactItem.

I think when tlbimp.exe generates interop assembly
than overrides cast operators.
Maybe this is workaround for C++ multi-inheritance which is
not
present in C#?
 
A

AK

Hello Dmitry,

I'm using GC.Collect and modified my code to remove multiple dot notations
(e.g. rdoFolder.Items.count) but still I'm getting an error for
"MAPI_E_CALL_FAILED".

This is happening especially for RDOMail object.

My code for it reads as

====================================================
Queue DeleteEMailQueue = new Queue();
RDOItems rdoItems = rdoFolder.Items;
for (int i = 0; i < rdoItems.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead, rdoMail.ReceivedTime,
rdoMail.Mileage))
{
rdoMail.Delete(0);
}
}
catch (Exception ex)
{
Util.LogException(ex);
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
GC.Collect();
}
====================================================

The error occurs after reading say around 700/800 emails in one folder.

Can you please suggest any workaround for this?

Thanks and Regards,
Amit Kathane

Dmitry Streblechenko said:
You are most likely running out of the RPC channel limit
(http://support.microsoft.com/kb/830829) - since .Net does not release COM
objects immediately, you can easily go over the limit even if you explicitly
release COM objects using Marshal.ReleaseCOMObject.
Avoid using multiple dot notation (e.g. rdoFolder.Items.Count) to make sure
the compiler does not create implicit variables that you cannot reference
and release.
GC.Collect() might help.
Next version of Redemption (send me an e-mail if you want a beta version)
will keep track of the open MAPI objeects and transparently release oldest
unmodified MAPI objects that can be easily reopened on demand (Redemption
objects will stay alive of course) when the number of open objects of a
particular kind approaches the limit.

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

AK said:
Hello Dmitry,

Thank you very much for your assistance.

I'm getting following error randomly.

"Error in IMAPIFolder.GetContentsTable: MAPI_E_CALL_FAILED"

Please find below my code

======================================
private static void ProcessUserMailbox(string userName)
{
RDOSession rdoSession = new RDOSessionClass();
try
{
// get the admin user name from the configuration file
string systemUserName =
Properties.Settings.Default.userName;
string exchangeSrvrName =
Properties.Settings.Default.UserMailboxServer;

if (string.IsNullOrEmpty(systemUserName))
systemUserName =
WindowsIdentity.GetCurrent().Name.ToString();

rdoSession.LogonExchangeMailbox(systemUserName,
exchangeSrvrName);
IRDOFolder rootFolder =
rdoSession.GetSharedMailbox(userName).RootFolder;

// iterate through all folders and subfolders
RDOFolders rdoFolders = rootFolder.Folders;
if (rdoFolders != null)
{
RDOFolder rdoFolder = rdoFolders.GetFirst();
if (rdoFolder != null)
{
// iterate folder list
for (int i = 0; i < rdoFolders.Count; i++)
{
// process the individual folder to search for
emails that can be deleted
ProcessFolder(userName, rootFolder.Name + " >
",
rdoFolder);
rdoFolder = rdoFolders.GetNext();
}
}
}
// release the COM object for the root folder.

System.Runtime.InteropServices.Marshal.ReleaseComObject(rootFolder);
rootFolder = null;
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{0}, Failed to process mailbox for user :
{0}",
userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
strInfo.AppendFormat(ex.Message + Environment.NewLine +
Util.GetInnerException(ex));
throw new ApplicationException(strInfo.ToString());
}
finally
{
rdoSession.Logoff();

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoSession);
rdoSession = null;
GC.Collect();
}
}

private static void ProcessFolder(string userName, string
folderPath, RDOFolder rdoFolder)
{
RDOFolders subFolders;
RDOFolder subFolder;

try
{
subFolders = rdoFolder.Folders;
if (subFolders != null)
{
subFolder = subFolders.GetFirst();
if (subFolder != null)
{
// iterate sub folder list
for (int i = 0; i < subFolders.Count; i++)
{
// process the subfolder
ProcessFolder(userName, rdoFolder.Name + "", subFolder);
subFolder = subFolders.GetNext();
}
}
}
// if found an individual folder (rdoFolder) without
the
subfolder then release the subfolder and subfolders COM objects

//System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolder);
subFolder = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolders);
subFolders = null;

// if there are no sub folders then process the
rdoFolder and iterate through all items
// process the individual items/emails in the folder
for (int i = 0; i < rdoFolder.Items.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead,
rdoMail.ReceivedTime, rdoMail.Mileage))
{
Util.LogMessage(string.Format("{0}, Email
with subject '{1}' Read : '{2}' Received Time : '{3}' Mileage : '{4}' is
deleted.", userName, rdoMail.Subject, !(rdoMail.UnRead),
rdoMail.ReceivedTime, rdoMail.Mileage));
if (DeleteMail)
rdoMail.Delete(0); //dfSoftDelete (0) -
default. Deletes the item. Can still be recoverable if retention policy is
set on Exchange Server.

//dfMoveToDeletedItems
(1) - the item is moved to the Deleted Items folder
//dfHardDelete (2) -
Exchange only. Permanently deletes the item; will not be recoverable
}
}
catch (Exception ex)
{
// in case of exception for processing
individual mail log the exception and process next email
Util.LogException(ex);
}
finally
{

//System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
}
}
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{1}, Failed to process folder : {0} for user
:
{1}", rdoFolder.Name, userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoFolder);
rdoFolder = null;
}
}
==================================================

The above error is coming for some of the emails for user. Can you please
let me know what could be the reason of this error.

Thanks and Regards,
Amit Kathane

Dmitry Streblechenko said:
1. Exchange transparently manages the locatiton of the mailboxes. It will
list all mailboxes in the organization. As an extra check, you might want
to
read the PR_EMS_AB_HOME_MDB property using RDOAddressEntry.Fields[] (the
property returns the name of the server where teh user has a mailbox) -
there can be entries that have no mailboxes.
2. Yes, use either (but not both) Logon or LogonExchangeMailbox. Or set
the
MAPIOBJECT property if you already have a running instance of Outlook
(use
Namespace.MAPIOBJECT) or CDO 1.21 session (Session.MAPIOBJECT).

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

Hello Dmitry,

Thank you very much for your reply.

According to your solution mentioned in one of the above responses i.e.

1. Run your code under the Windows user identity who can access all of
the
required mailboxes
2. Call RDOSession.Logon if you have an existing profilee pointing to
that
user's mailbox or call RDOSession.LogonExchangeMailbox
3. Loop through the entries in the RDOSession.AddressBook.GAL;
4. For each RDOAddressEntry with DisplayType property equal to
DT_MAILUSER
(= 0)
5. Call RDOSession.GetSharedMailbox passing that RDOAddressEntry object
(get
back RDOExchangeMailboxStore)
6. Loop through all subfolder and messages in tha tmailbox store
stating
with the RDOStore.IPMRootFolder collection

Will there be problem in step 3 and 4 if there is a multiple exchange
server
box in the domain and various users are configured with different
exchange
box. Will I still be able to get all user's RDOAddressEntry object in
RDOSession.AddressBook.GAL or will it give only those users which are
on
the
same exchange box as of the Windows user under which the application is
running?

Also I assume that RDOSession.Logon using the existing profile of using
RDOSession.LogonExchangeMailbox are exclusive. I guess I don't have to
create
the profile if it's not there and can use the
RDOSession.LogonExchangeMailbox
method instead. Is that right?

Many Thanks for your help.
Amit Kathane

:

Once again, see steps 1 and 2. You need to call LogonExchangeMailbox
*once*
for the current Windows user who must have the right to access othe
rmailboxes.
To access other mailboxess call RDOSession.GetSharedMailbox. Do *not*
call
LogonExchangeMailbox for the other mailboxes.

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

Hi,

Thanks you very much for your reply.

I'm understanding bit of Redemeption library now.

One issue is when I'm trying to do login using
RDOSession.LogonExchangeMailbox

it's popping up the UI.

According to my requirement, I've to traverse through all mailbox
which
I
guess will require login into each mailbox and a pop up UI could be
an
issue?

This UI does not come when using RDOSession.Logon method, is there
anything
 
D

Dmitry Streblechenko

The line
rdoFolder.Items;
evaluates to
rdoFolder.Items.Item(i)
So you are still using multiplee dot notation.

Do you want a beta version to see if it fixes this problem for you?

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

AK said:
Hello Dmitry,

I'm using GC.Collect and modified my code to remove multiple dot notations
(e.g. rdoFolder.Items.count) but still I'm getting an error for
"MAPI_E_CALL_FAILED".

This is happening especially for RDOMail object.

My code for it reads as

====================================================
Queue DeleteEMailQueue = new Queue();
RDOItems rdoItems = rdoFolder.Items;
for (int i = 0; i < rdoItems.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead, rdoMail.ReceivedTime,
rdoMail.Mileage))
{
rdoMail.Delete(0);
}
}
catch (Exception ex)
{
Util.LogException(ex);
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
GC.Collect();
}
====================================================

The error occurs after reading say around 700/800 emails in one folder.

Can you please suggest any workaround for this?

Thanks and Regards,
Amit Kathane

Dmitry Streblechenko said:
You are most likely running out of the RPC channel limit
(http://support.microsoft.com/kb/830829) - since .Net does not release
COM
objects immediately, you can easily go over the limit even if you
explicitly
release COM objects using Marshal.ReleaseCOMObject.
Avoid using multiple dot notation (e.g. rdoFolder.Items.Count) to make
sure
the compiler does not create implicit variables that you cannot reference
and release.
GC.Collect() might help.
Next version of Redemption (send me an e-mail if you want a beta version)
will keep track of the open MAPI objeects and transparently release
oldest
unmodified MAPI objects that can be easily reopened on demand (Redemption
objects will stay alive of course) when the number of open objects of a
particular kind approaches the limit.

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

AK said:
Hello Dmitry,

Thank you very much for your assistance.

I'm getting following error randomly.

"Error in IMAPIFolder.GetContentsTable: MAPI_E_CALL_FAILED"

Please find below my code

======================================
private static void ProcessUserMailbox(string userName)
{
RDOSession rdoSession = new RDOSessionClass();
try
{
// get the admin user name from the configuration file
string systemUserName =
Properties.Settings.Default.userName;
string exchangeSrvrName =
Properties.Settings.Default.UserMailboxServer;

if (string.IsNullOrEmpty(systemUserName))
systemUserName =
WindowsIdentity.GetCurrent().Name.ToString();

rdoSession.LogonExchangeMailbox(systemUserName,
exchangeSrvrName);
IRDOFolder rootFolder =
rdoSession.GetSharedMailbox(userName).RootFolder;

// iterate through all folders and subfolders
RDOFolders rdoFolders = rootFolder.Folders;
if (rdoFolders != null)
{
RDOFolder rdoFolder = rdoFolders.GetFirst();
if (rdoFolder != null)
{
// iterate folder list
for (int i = 0; i < rdoFolders.Count; i++)
{
// process the individual folder to search
for
emails that can be deleted
ProcessFolder(userName, rootFolder.Name + "

",
rdoFolder);
rdoFolder = rdoFolders.GetNext();
}
}
}
// release the COM object for the root folder.

System.Runtime.InteropServices.Marshal.ReleaseComObject(rootFolder);
rootFolder = null;
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{0}, Failed to process mailbox for user :
{0}",
userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
strInfo.AppendFormat(ex.Message + Environment.NewLine +
Util.GetInnerException(ex));
throw new ApplicationException(strInfo.ToString());
}
finally
{
rdoSession.Logoff();

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoSession);
rdoSession = null;
GC.Collect();
}
}

private static void ProcessFolder(string userName, string
folderPath, RDOFolder rdoFolder)
{
RDOFolders subFolders;
RDOFolder subFolder;

try
{
subFolders = rdoFolder.Folders;
if (subFolders != null)
{
subFolder = subFolders.GetFirst();
if (subFolder != null)
{
// iterate sub folder list
for (int i = 0; i < subFolders.Count; i++)
{
// process the subfolder
ProcessFolder(userName, rdoFolder.Name +
"

", subFolder);
subFolder = subFolders.GetNext();
}
}
}
// if found an individual folder (rdoFolder) without
the
subfolder then release the subfolder and subfolders COM objects

//System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolder);
subFolder = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolders);
subFolders = null;

// if there are no sub folders then process the
rdoFolder and iterate through all items
// process the individual items/emails in the folder
for (int i = 0; i < rdoFolder.Items.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead,
rdoMail.ReceivedTime, rdoMail.Mileage))
{
Util.LogMessage(string.Format("{0},
Email
with subject '{1}' Read : '{2}' Received Time : '{3}' Mileage : '{4}'
is
deleted.", userName, rdoMail.Subject, !(rdoMail.UnRead),
rdoMail.ReceivedTime, rdoMail.Mileage));
if (DeleteMail)
rdoMail.Delete(0); //dfSoftDelete
(0) -
default. Deletes the item. Can still be recoverable if retention policy
is
set on Exchange Server.

//dfMoveToDeletedItems
(1) - the item is moved to the Deleted Items folder
//dfHardDelete
(2) -
Exchange only. Permanently deletes the item; will not be recoverable
}
}
catch (Exception ex)
{
// in case of exception for processing
individual mail log the exception and process next email
Util.LogException(ex);
}
finally
{

//System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
}
}
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{1}, Failed to process folder : {0} for
user
:
{1}", rdoFolder.Name, userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoFolder);
rdoFolder = null;
}
}
==================================================

The above error is coming for some of the emails for user. Can you
please
let me know what could be the reason of this error.

Thanks and Regards,
Amit Kathane

:

1. Exchange transparently manages the locatiton of the mailboxes. It
will
list all mailboxes in the organization. As an extra check, you might
want
to
read the PR_EMS_AB_HOME_MDB property using RDOAddressEntry.Fields[]
(the
property returns the name of the server where teh user has a
mailbox) -
there can be entries that have no mailboxes.
2. Yes, use either (but not both) Logon or LogonExchangeMailbox. Or
set
the
MAPIOBJECT property if you already have a running instance of Outlook
(use
Namespace.MAPIOBJECT) or CDO 1.21 session (Session.MAPIOBJECT).

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

Hello Dmitry,

Thank you very much for your reply.

According to your solution mentioned in one of the above responses
i.e.

1. Run your code under the Windows user identity who can access all
of
the
required mailboxes
2. Call RDOSession.Logon if you have an existing profilee pointing
to
that
user's mailbox or call RDOSession.LogonExchangeMailbox
3. Loop through the entries in the RDOSession.AddressBook.GAL;
4. For each RDOAddressEntry with DisplayType property equal to
DT_MAILUSER
(= 0)
5. Call RDOSession.GetSharedMailbox passing that RDOAddressEntry
object
(get
back RDOExchangeMailboxStore)
6. Loop through all subfolder and messages in tha tmailbox store
stating
with the RDOStore.IPMRootFolder collection

Will there be problem in step 3 and 4 if there is a multiple
exchange
server
box in the domain and various users are configured with different
exchange
box. Will I still be able to get all user's RDOAddressEntry object
in
RDOSession.AddressBook.GAL or will it give only those users which
are
on
the
same exchange box as of the Windows user under which the application
is
running?

Also I assume that RDOSession.Logon using the existing profile of
using
RDOSession.LogonExchangeMailbox are exclusive. I guess I don't have
to
create
the profile if it's not there and can use the
RDOSession.LogonExchangeMailbox
method instead. Is that right?

Many Thanks for your help.
Amit Kathane

:

Once again, see steps 1 and 2. You need to call
LogonExchangeMailbox
*once*
for the current Windows user who must have the right to access othe
rmailboxes.
To access other mailboxess call RDOSession.GetSharedMailbox. Do
*not*
call
LogonExchangeMailbox for the other mailboxes.

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

Hi,

Thanks you very much for your reply.

I'm understanding bit of Redemeption library now.

One issue is when I'm trying to do login using
RDOSession.LogonExchangeMailbox

it's popping up the UI.

According to my requirement, I've to traverse through all mailbox
which
I
guess will require login into each mailbox and a pop up UI could
be
an
issue?

This UI does not come when using RDOSession.Logon method, is
there
anything
 
A

AK

Hi Dimitry,

Is it a limitation from .NET side that it's not able to release the COM
objects? It seems bit wired that it’s not able to release the COM object even
after calling Marshal.ReleaseCOMObject as well as using GC.Collect.

Anyhow, it surely will help if you can send me the beta version to try it
out. Do let me know what exactly I need to do for the same? Which account I
should send an email to?

We have recently purchased the 4.4 version of Redemption library. Do you
want me to give any reference number for the same?

Many thanks for your help.
Regards,
Amit Kathane


Dmitry Streblechenko said:
The line
rdoFolder.Items;
evaluates to
rdoFolder.Items.Item(i)
So you are still using multiplee dot notation.

Do you want a beta version to see if it fixes this problem for you?

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

AK said:
Hello Dmitry,

I'm using GC.Collect and modified my code to remove multiple dot notations
(e.g. rdoFolder.Items.count) but still I'm getting an error for
"MAPI_E_CALL_FAILED".

This is happening especially for RDOMail object.

My code for it reads as

====================================================
Queue DeleteEMailQueue = new Queue();
RDOItems rdoItems = rdoFolder.Items;
for (int i = 0; i < rdoItems.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead, rdoMail.ReceivedTime,
rdoMail.Mileage))
{
rdoMail.Delete(0);
}
}
catch (Exception ex)
{
Util.LogException(ex);
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
GC.Collect();
}
====================================================

The error occurs after reading say around 700/800 emails in one folder.

Can you please suggest any workaround for this?

Thanks and Regards,
Amit Kathane

Dmitry Streblechenko said:
You are most likely running out of the RPC channel limit
(http://support.microsoft.com/kb/830829) - since .Net does not release
COM
objects immediately, you can easily go over the limit even if you
explicitly
release COM objects using Marshal.ReleaseCOMObject.
Avoid using multiple dot notation (e.g. rdoFolder.Items.Count) to make
sure
the compiler does not create implicit variables that you cannot reference
and release.
GC.Collect() might help.
Next version of Redemption (send me an e-mail if you want a beta version)
will keep track of the open MAPI objeects and transparently release
oldest
unmodified MAPI objects that can be easily reopened on demand (Redemption
objects will stay alive of course) when the number of open objects of a
particular kind approaches the limit.

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

Hello Dmitry,

Thank you very much for your assistance.

I'm getting following error randomly.

"Error in IMAPIFolder.GetContentsTable: MAPI_E_CALL_FAILED"

Please find below my code

======================================
private static void ProcessUserMailbox(string userName)
{
RDOSession rdoSession = new RDOSessionClass();
try
{
// get the admin user name from the configuration file
string systemUserName =
Properties.Settings.Default.userName;
string exchangeSrvrName =
Properties.Settings.Default.UserMailboxServer;

if (string.IsNullOrEmpty(systemUserName))
systemUserName =
WindowsIdentity.GetCurrent().Name.ToString();

rdoSession.LogonExchangeMailbox(systemUserName,
exchangeSrvrName);
IRDOFolder rootFolder =
rdoSession.GetSharedMailbox(userName).RootFolder;

// iterate through all folders and subfolders
RDOFolders rdoFolders = rootFolder.Folders;
if (rdoFolders != null)
{
RDOFolder rdoFolder = rdoFolders.GetFirst();
if (rdoFolder != null)
{
// iterate folder list
for (int i = 0; i < rdoFolders.Count; i++)
{
// process the individual folder to search
for
emails that can be deleted
ProcessFolder(userName, rootFolder.Name + "

",
rdoFolder);
rdoFolder = rdoFolders.GetNext();
}
}
}
// release the COM object for the root folder.

System.Runtime.InteropServices.Marshal.ReleaseComObject(rootFolder);
rootFolder = null;
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{0}, Failed to process mailbox for user :
{0}",
userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
strInfo.AppendFormat(ex.Message + Environment.NewLine +
Util.GetInnerException(ex));
throw new ApplicationException(strInfo.ToString());
}
finally
{
rdoSession.Logoff();

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoSession);
rdoSession = null;
GC.Collect();
}
}

private static void ProcessFolder(string userName, string
folderPath, RDOFolder rdoFolder)
{
RDOFolders subFolders;
RDOFolder subFolder;

try
{
subFolders = rdoFolder.Folders;
if (subFolders != null)
{
subFolder = subFolders.GetFirst();
if (subFolder != null)
{
// iterate sub folder list
for (int i = 0; i < subFolders.Count; i++)
{
// process the subfolder
ProcessFolder(userName, rdoFolder.Name +
"

", subFolder);
subFolder = subFolders.GetNext();
}
}
}
// if found an individual folder (rdoFolder) without
the
subfolder then release the subfolder and subfolders COM objects

//System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolder);
subFolder = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolders);
subFolders = null;

// if there are no sub folders then process the
rdoFolder and iterate through all items
// process the individual items/emails in the folder
for (int i = 0; i < rdoFolder.Items.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead,
rdoMail.ReceivedTime, rdoMail.Mileage))
{
Util.LogMessage(string.Format("{0},
Email
with subject '{1}' Read : '{2}' Received Time : '{3}' Mileage : '{4}'
is
deleted.", userName, rdoMail.Subject, !(rdoMail.UnRead),
rdoMail.ReceivedTime, rdoMail.Mileage));
if (DeleteMail)
rdoMail.Delete(0); //dfSoftDelete
(0) -
default. Deletes the item. Can still be recoverable if retention policy
is
set on Exchange Server.

//dfMoveToDeletedItems
(1) - the item is moved to the Deleted Items folder
//dfHardDelete
(2) -
Exchange only. Permanently deletes the item; will not be recoverable
}
}
catch (Exception ex)
{
// in case of exception for processing
individual mail log the exception and process next email
Util.LogException(ex);
}
finally
{

//System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
}
}
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{1}, Failed to process folder : {0} for
user
:
{1}", rdoFolder.Name, userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoFolder);
rdoFolder = null;
}
}
==================================================

The above error is coming for some of the emails for user. Can you
please
let me know what could be the reason of this error.

Thanks and Regards,
Amit Kathane

:

1. Exchange transparently manages the locatiton of the mailboxes. It
will
list all mailboxes in the organization. As an extra check, you might
want
to
read the PR_EMS_AB_HOME_MDB property using RDOAddressEntry.Fields[]
(the
property returns the name of the server where teh user has a
mailbox) -
there can be entries that have no mailboxes.
2. Yes, use either (but not both) Logon or LogonExchangeMailbox. Or
set
the
MAPIOBJECT property if you already have a running instance of Outlook
(use
Namespace.MAPIOBJECT) or CDO 1.21 session (Session.MAPIOBJECT).

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

Hello Dmitry,
 
A

AK

Hello Dimitry,

There is one more error I'm getting randomly.

Error in IMAPISession::OpenMsgStore(pbExchangeProviderPrimaryUserGuid):
0x80010106
Error: Unknown Error.

Can you please help me on this one as well?

Thanks and Regards,
Amit Kathane

Hi Dimitry,

Is it a limitation from .NET side that it's not able to release the COM
objects? It seems bit wired that it’s not able to release the COM object even
after calling Marshal.ReleaseCOMObject as well as using GC.Collect.

Anyhow, it surely will help if you can send me the beta version to try it
out. Do let me know what exactly I need to do for the same? Which account I
should send an email to?

We have recently purchased the 4.4 version of Redemption library. Do you
want me to give any reference number for the same?

Many thanks for your help.
Regards,
Amit Kathane


Dmitry Streblechenko said:
The line
rdoFolder.Items;
evaluates to
rdoFolder.Items.Item(i)
So you are still using multiplee dot notation.

Do you want a beta version to see if it fixes this problem for you?

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

AK said:
Hello Dmitry,

I'm using GC.Collect and modified my code to remove multiple dot notations
(e.g. rdoFolder.Items.count) but still I'm getting an error for
"MAPI_E_CALL_FAILED".

This is happening especially for RDOMail object.

My code for it reads as

====================================================
Queue DeleteEMailQueue = new Queue();
RDOItems rdoItems = rdoFolder.Items;
for (int i = 0; i < rdoItems.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead, rdoMail.ReceivedTime,
rdoMail.Mileage))
{
rdoMail.Delete(0);
}
}
catch (Exception ex)
{
Util.LogException(ex);
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
GC.Collect();
}
====================================================

The error occurs after reading say around 700/800 emails in one folder.

Can you please suggest any workaround for this?

Thanks and Regards,
Amit Kathane

:

You are most likely running out of the RPC channel limit
(http://support.microsoft.com/kb/830829) - since .Net does not release
COM
objects immediately, you can easily go over the limit even if you
explicitly
release COM objects using Marshal.ReleaseCOMObject.
Avoid using multiple dot notation (e.g. rdoFolder.Items.Count) to make
sure
the compiler does not create implicit variables that you cannot reference
and release.
GC.Collect() might help.
Next version of Redemption (send me an e-mail if you want a beta version)
will keep track of the open MAPI objeects and transparently release
oldest
unmodified MAPI objects that can be easily reopened on demand (Redemption
objects will stay alive of course) when the number of open objects of a
particular kind approaches the limit.

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

Hello Dmitry,

Thank you very much for your assistance.

I'm getting following error randomly.

"Error in IMAPIFolder.GetContentsTable: MAPI_E_CALL_FAILED"

Please find below my code

======================================
private static void ProcessUserMailbox(string userName)
{
RDOSession rdoSession = new RDOSessionClass();
try
{
// get the admin user name from the configuration file
string systemUserName =
Properties.Settings.Default.userName;
string exchangeSrvrName =
Properties.Settings.Default.UserMailboxServer;

if (string.IsNullOrEmpty(systemUserName))
systemUserName =
WindowsIdentity.GetCurrent().Name.ToString();

rdoSession.LogonExchangeMailbox(systemUserName,
exchangeSrvrName);
IRDOFolder rootFolder =
rdoSession.GetSharedMailbox(userName).RootFolder;

// iterate through all folders and subfolders
RDOFolders rdoFolders = rootFolder.Folders;
if (rdoFolders != null)
{
RDOFolder rdoFolder = rdoFolders.GetFirst();
if (rdoFolder != null)
{
// iterate folder list
for (int i = 0; i < rdoFolders.Count; i++)
{
// process the individual folder to search
for
emails that can be deleted
ProcessFolder(userName, rootFolder.Name + "

",
rdoFolder);
rdoFolder = rdoFolders.GetNext();
}
}
}
// release the COM object for the root folder.

System.Runtime.InteropServices.Marshal.ReleaseComObject(rootFolder);
rootFolder = null;
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{0}, Failed to process mailbox for user :
{0}",
userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
strInfo.AppendFormat(ex.Message + Environment.NewLine +
Util.GetInnerException(ex));
throw new ApplicationException(strInfo.ToString());
}
finally
{
rdoSession.Logoff();

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoSession);
rdoSession = null;
GC.Collect();
}
}

private static void ProcessFolder(string userName, string
folderPath, RDOFolder rdoFolder)
{
RDOFolders subFolders;
RDOFolder subFolder;

try
{
subFolders = rdoFolder.Folders;
if (subFolders != null)
{
subFolder = subFolders.GetFirst();
if (subFolder != null)
{
// iterate sub folder list
for (int i = 0; i < subFolders.Count; i++)
{
// process the subfolder
ProcessFolder(userName, rdoFolder.Name +
"

", subFolder);
subFolder = subFolders.GetNext();
}
}
}
// if found an individual folder (rdoFolder) without
the
subfolder then release the subfolder and subfolders COM objects

//System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolder);
subFolder = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolders);
subFolders = null;

// if there are no sub folders then process the
rdoFolder and iterate through all items
// process the individual items/emails in the folder
for (int i = 0; i < rdoFolder.Items.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead,
rdoMail.ReceivedTime, rdoMail.Mileage))
{
Util.LogMessage(string.Format("{0},
Email
with subject '{1}' Read : '{2}' Received Time : '{3}' Mileage : '{4}'
is
deleted.", userName, rdoMail.Subject, !(rdoMail.UnRead),
rdoMail.ReceivedTime, rdoMail.Mileage));
if (DeleteMail)
rdoMail.Delete(0); //dfSoftDelete
(0) -
default. Deletes the item. Can still be recoverable if retention policy
is
set on Exchange Server.

//dfMoveToDeletedItems
(1) - the item is moved to the Deleted Items folder
//dfHardDelete
(2) -
Exchange only. Permanently deletes the item; will not be recoverable
}
}
catch (Exception ex)
{
// in case of exception for processing
individual mail log the exception and process next email
Util.LogException(ex);
}
finally
{

//System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
}
}
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{1}, Failed to process folder : {0} for
user
:
{1}", rdoFolder.Name, userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoFolder);
rdoFolder = null;
}
}
==================================================

The above error is coming for some of the emails for user. Can you
please
let me know what could be the reason of this error.

Thanks and Regards,
Amit Kathane

:

1. Exchange transparently manages the locatiton of the mailboxes. It
will
list all mailboxes in the organization. As an extra check, you might
want
to
 
D

Dmitry Streblechenko

Marshal.ReleaseCOMObject releases a *particular* COM object. If you use
multiple dot notation, teh compiler creates an implicit variable to hodl the
intermediate resul(s). You cannot explicitly access and release these
implicit variables.
Senbd an e-mail to (e-mail address removed) , including the ShareIt reference
number of at least the name of the company woudl be helpful.

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

AK said:
Hi Dimitry,

Is it a limitation from .NET side that it's not able to release the COM
objects? It seems bit wired that it's not able to release the COM object
even
after calling Marshal.ReleaseCOMObject as well as using GC.Collect.

Anyhow, it surely will help if you can send me the beta version to try it
out. Do let me know what exactly I need to do for the same? Which account
I
should send an email to?

We have recently purchased the 4.4 version of Redemption library. Do you
want me to give any reference number for the same?

Many thanks for your help.
Regards,
Amit Kathane


Dmitry Streblechenko said:
The line
rdoFolder.Items;
evaluates to
rdoFolder.Items.Item(i)
So you are still using multiplee dot notation.

Do you want a beta version to see if it fixes this problem for you?

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

AK said:
Hello Dmitry,

I'm using GC.Collect and modified my code to remove multiple dot
notations
(e.g. rdoFolder.Items.count) but still I'm getting an error for
"MAPI_E_CALL_FAILED".

This is happening especially for RDOMail object.

My code for it reads as

====================================================
Queue DeleteEMailQueue = new Queue();
RDOItems rdoItems = rdoFolder.Items;
for (int i = 0; i < rdoItems.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead, rdoMail.ReceivedTime,
rdoMail.Mileage))
{
rdoMail.Delete(0);
}
}
catch (Exception ex)
{
Util.LogException(ex);
}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
GC.Collect();
}
====================================================

The error occurs after reading say around 700/800 emails in one folder.

Can you please suggest any workaround for this?

Thanks and Regards,
Amit Kathane

:

You are most likely running out of the RPC channel limit
(http://support.microsoft.com/kb/830829) - since .Net does not release
COM
objects immediately, you can easily go over the limit even if you
explicitly
release COM objects using Marshal.ReleaseCOMObject.
Avoid using multiple dot notation (e.g. rdoFolder.Items.Count) to make
sure
the compiler does not create implicit variables that you cannot
reference
and release.
GC.Collect() might help.
Next version of Redemption (send me an e-mail if you want a beta
version)
will keep track of the open MAPI objeects and transparently release
oldest
unmodified MAPI objects that can be easily reopened on demand
(Redemption
objects will stay alive of course) when the number of open objects of
a
particular kind approaches the limit.

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

Hello Dmitry,

Thank you very much for your assistance.

I'm getting following error randomly.

"Error in IMAPIFolder.GetContentsTable: MAPI_E_CALL_FAILED"

Please find below my code

======================================
private static void ProcessUserMailbox(string userName)
{
RDOSession rdoSession = new RDOSessionClass();
try
{
// get the admin user name from the configuration
file
string systemUserName =
Properties.Settings.Default.userName;
string exchangeSrvrName =
Properties.Settings.Default.UserMailboxServer;

if (string.IsNullOrEmpty(systemUserName))
systemUserName =
WindowsIdentity.GetCurrent().Name.ToString();

rdoSession.LogonExchangeMailbox(systemUserName,
exchangeSrvrName);
IRDOFolder rootFolder =
rdoSession.GetSharedMailbox(userName).RootFolder;

// iterate through all folders and subfolders
RDOFolders rdoFolders = rootFolder.Folders;
if (rdoFolders != null)
{
RDOFolder rdoFolder = rdoFolders.GetFirst();
if (rdoFolder != null)
{
// iterate folder list
for (int i = 0; i < rdoFolders.Count; i++)
{
// process the individual folder to
search
for
emails that can be deleted
ProcessFolder(userName, rootFolder.Name +
"

",
rdoFolder);
rdoFolder = rdoFolders.GetNext();
}
}
}
// release the COM object for the root folder.

System.Runtime.InteropServices.Marshal.ReleaseComObject(rootFolder);
rootFolder = null;
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{0}, Failed to process mailbox for user
:
{0}",
userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
strInfo.AppendFormat(ex.Message + Environment.NewLine
+
Util.GetInnerException(ex));
throw new ApplicationException(strInfo.ToString());
}
finally
{
rdoSession.Logoff();

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoSession);
rdoSession = null;
GC.Collect();
}
}

private static void ProcessFolder(string userName, string
folderPath, RDOFolder rdoFolder)
{
RDOFolders subFolders;
RDOFolder subFolder;

try
{
subFolders = rdoFolder.Folders;
if (subFolders != null)
{
subFolder = subFolders.GetFirst();
if (subFolder != null)
{
// iterate sub folder list
for (int i = 0; i < subFolders.Count;
i++)
{
// process the subfolder
ProcessFolder(userName,
rdoFolder.Name +
"

", subFolder);
subFolder = subFolders.GetNext();
}
}
}
// if found an individual folder (rdoFolder)
without
the
subfolder then release the subfolder and subfolders COM objects

//System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolder);
subFolder = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolders);
subFolders = null;

// if there are no sub folders then process the
rdoFolder and iterate through all items
// process the individual items/emails in the
folder
for (int i = 0; i < rdoFolder.Items.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead,
rdoMail.ReceivedTime, rdoMail.Mileage))
{
Util.LogMessage(string.Format("{0},
Email
with subject '{1}' Read : '{2}' Received Time : '{3}' Mileage :
'{4}'
is
deleted.", userName, rdoMail.Subject, !(rdoMail.UnRead),
rdoMail.ReceivedTime, rdoMail.Mileage));
if (DeleteMail)
rdoMail.Delete(0); //dfSoftDelete
(0) -
default. Deletes the item. Can still be recoverable if retention
policy
is
set on Exchange Server.

//dfMoveToDeletedItems
(1) - the item is moved to the Deleted Items folder
//dfHardDelete
(2) -
Exchange only. Permanently deletes the item; will not be recoverable
}
}
catch (Exception ex)
{
// in case of exception for processing
individual mail log the exception and process next email
Util.LogException(ex);
}
finally
{

//System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
}
}
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{1}, Failed to process folder : {0} for
user
:
{1}", rdoFolder.Name, userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoFolder);
rdoFolder = null;
}
}
==================================================

The above error is coming for some of the emails for user. Can you
please
let me know what could be the reason of this error.

Thanks and Regards,
Amit Kathane

:

1. Exchange transparently manages the locatiton of the mailboxes.
It
will
list all mailboxes in the organization. As an extra check, you
might
want
to
read the PR_EMS_AB_HOME_MDB property using RDOAddressEntry.Fields[]
(the
property returns the name of the server where teh user has a
mailbox) -
there can be entries that have no mailboxes.
2. Yes, use either (but not both) Logon or LogonExchangeMailbox. Or
set
the
MAPIOBJECT property if you already have a running instance of
Outlook
(use
Namespace.MAPIOBJECT) or CDO 1.21 session (Session.MAPIOBJECT).

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

Hello Dmitry,
 
D

Dmitry Streblechenko

The error is RPC_E_CHANGED_MODE.

How are you calling CoInitializeEx? Is it CoInitializeEx(NULL,
COINIT_MULTITHREADED)?

Or, in case of .Net, what happens if you mark the Main() method in your app
as STA?



[STAThread]

static void Main(string[] args)

{

....


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

AK said:
Hello Dimitry,

There is one more error I'm getting randomly.

Error in IMAPISession::OpenMsgStore(pbExchangeProviderPrimaryUserGuid):
0x80010106
Error: Unknown Error.

Can you please help me on this one as well?

Thanks and Regards,
Amit Kathane

Hi Dimitry,

Is it a limitation from .NET side that it's not able to release the COM
objects? It seems bit wired that it's not able to release the COM object
even
after calling Marshal.ReleaseCOMObject as well as using GC.Collect.

Anyhow, it surely will help if you can send me the beta version to try it
out. Do let me know what exactly I need to do for the same? Which account
I
should send an email to?

We have recently purchased the 4.4 version of Redemption library. Do you
want me to give any reference number for the same?

Many thanks for your help.
Regards,
Amit Kathane


Dmitry Streblechenko said:
The line
rdoFolder.Items;
evaluates to
rdoFolder.Items.Item(i)
So you are still using multiplee dot notation.

Do you want a beta version to see if it fixes this problem for you?

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

Hello Dmitry,

I'm using GC.Collect and modified my code to remove multiple dot
notations
(e.g. rdoFolder.Items.count) but still I'm getting an error for
"MAPI_E_CALL_FAILED".

This is happening especially for RDOMail object.

My code for it reads as

====================================================
Queue DeleteEMailQueue = new Queue();
RDOItems rdoItems = rdoFolder.Items;
for (int i = 0; i < rdoItems.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead, rdoMail.ReceivedTime,
rdoMail.Mileage))
{
rdoMail.Delete(0);
}
}
catch (Exception ex)
{
Util.LogException(ex);
}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
GC.Collect();
}
====================================================

The error occurs after reading say around 700/800 emails in one
folder.

Can you please suggest any workaround for this?

Thanks and Regards,
Amit Kathane

:

You are most likely running out of the RPC channel limit
(http://support.microsoft.com/kb/830829) - since .Net does not
release
COM
objects immediately, you can easily go over the limit even if you
explicitly
release COM objects using Marshal.ReleaseCOMObject.
Avoid using multiple dot notation (e.g. rdoFolder.Items.Count) to
make
sure
the compiler does not create implicit variables that you cannot
reference
and release.
GC.Collect() might help.
Next version of Redemption (send me an e-mail if you want a beta
version)
will keep track of the open MAPI objeects and transparently release
oldest
unmodified MAPI objects that can be easily reopened on demand
(Redemption
objects will stay alive of course) when the number of open objects
of a
particular kind approaches the limit.

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

Hello Dmitry,

Thank you very much for your assistance.

I'm getting following error randomly.

"Error in IMAPIFolder.GetContentsTable: MAPI_E_CALL_FAILED"

Please find below my code

======================================
private static void ProcessUserMailbox(string userName)
{
RDOSession rdoSession = new RDOSessionClass();
try
{
// get the admin user name from the configuration
file
string systemUserName =
Properties.Settings.Default.userName;
string exchangeSrvrName =
Properties.Settings.Default.UserMailboxServer;

if (string.IsNullOrEmpty(systemUserName))
systemUserName =
WindowsIdentity.GetCurrent().Name.ToString();

rdoSession.LogonExchangeMailbox(systemUserName,
exchangeSrvrName);
IRDOFolder rootFolder =
rdoSession.GetSharedMailbox(userName).RootFolder;

// iterate through all folders and subfolders
RDOFolders rdoFolders = rootFolder.Folders;
if (rdoFolders != null)
{
RDOFolder rdoFolder = rdoFolders.GetFirst();
if (rdoFolder != null)
{
// iterate folder list
for (int i = 0; i < rdoFolders.Count; i++)
{
// process the individual folder to
search
for
emails that can be deleted
ProcessFolder(userName, rootFolder.Name
+ "

",
rdoFolder);
rdoFolder = rdoFolders.GetNext();
}
}
}
// release the COM object for the root folder.

System.Runtime.InteropServices.Marshal.ReleaseComObject(rootFolder);
rootFolder = null;
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{0}, Failed to process mailbox for
user :
{0}",
userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
strInfo.AppendFormat(ex.Message +
Environment.NewLine +
Util.GetInnerException(ex));
throw new ApplicationException(strInfo.ToString());
}
finally
{
rdoSession.Logoff();

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoSession);
rdoSession = null;
GC.Collect();
}
}

private static void ProcessFolder(string userName, string
folderPath, RDOFolder rdoFolder)
{
RDOFolders subFolders;
RDOFolder subFolder;

try
{
subFolders = rdoFolder.Folders;
if (subFolders != null)
{
subFolder = subFolders.GetFirst();
if (subFolder != null)
{
// iterate sub folder list
for (int i = 0; i < subFolders.Count;
i++)
{
// process the subfolder
ProcessFolder(userName,
rdoFolder.Name +
"

", subFolder);
subFolder = subFolders.GetNext();
}
}
}
// if found an individual folder (rdoFolder)
without
the
subfolder then release the subfolder and subfolders COM objects

//System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolder);
subFolder = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolders);
subFolders = null;

// if there are no sub folders then process the
rdoFolder and iterate through all items
// process the individual items/emails in the
folder
for (int i = 0; i < rdoFolder.Items.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead,
rdoMail.ReceivedTime, rdoMail.Mileage))
{
Util.LogMessage(string.Format("{0},
Email
with subject '{1}' Read : '{2}' Received Time : '{3}' Mileage :
'{4}'
is
deleted.", userName, rdoMail.Subject, !(rdoMail.UnRead),
rdoMail.ReceivedTime, rdoMail.Mileage));
if (DeleteMail)
rdoMail.Delete(0);
//dfSoftDelete
(0) -
default. Deletes the item. Can still be recoverable if retention
policy
is
set on Exchange Server.

//dfMoveToDeletedItems
(1) - the item is moved to the Deleted Items folder

//dfHardDelete
(2) -
Exchange only. Permanently deletes the item; will not be
recoverable
}
}
catch (Exception ex)
{
// in case of exception for processing
individual mail log the exception and process next email
Util.LogException(ex);
}
finally
{

//System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
}
}
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{1}, Failed to process folder : {0}
for
user
:
{1}", rdoFolder.Name, userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoFolder);
rdoFolder = null;
}
}
==================================================

The above error is coming for some of the emails for user. Can you
please
let me know what could be the reason of this error.

Thanks and Regards,
Amit Kathane

:

1. Exchange transparently manages the locatiton of the mailboxes.
It
will
list all mailboxes in the organization. As an extra check, you
might
want
to
 
A

AK

Hi Dmitry,

I've emailed you the details, please send me the beta version at the earliest.

One more thing,

When I'm trying to traverse through a folder and deleting the emails it is
skipping some emails. I'll try to explain it using a example.

Say in folder Test there are 10 emails, out of which 3 emails (3rd, 5th and
7th) are qualified (older than say 2 months) to be deleted.

When i traverse for that folder using below code to delete it

=======================================
RDOMail rdoMail = rdoItems.GetFirst();
while (rdoMail != null)
{
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead, rdoMail.ReceivedTime,
rdoMail.Mileage))
{
if (DeleteMail)
rdoMail.Delete(0);
}
}
catch (Exception ex)
{
// in case of exception log the exception and process next email
Util.LogException(ex);
}
finally
{
Util.ReleaseComObject(rdoMail);
GC.Collect();
}
rdoMail = rdoItems.GetNext();
}
=============================================

it does not delete the 5th and 7th mail. I'm not sure but I guess the reason
for this is as soon as it deletes the 3rd mail the index of emails in folder
changes and hence it's not able to find the emails properly.

Is there any other way to traverse through folder and delete emails, so as
to find all emails. I tried storing index in array and then traversing folder
only for that array, but it didn't help. It would be helpful if there is any
direct method from RDO object library to do so.

Once again many thanks for your help.

Regards,
Amit Kathane

Dmitry Streblechenko said:
Marshal.ReleaseCOMObject releases a *particular* COM object. If you use
multiple dot notation, teh compiler creates an implicit variable to hodl the
intermediate resul(s). You cannot explicitly access and release these
implicit variables.
Senbd an e-mail to (e-mail address removed) , including the ShareIt reference
number of at least the name of the company woudl be helpful.

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

AK said:
Hi Dimitry,

Is it a limitation from .NET side that it's not able to release the COM
objects? It seems bit wired that it's not able to release the COM object
even
after calling Marshal.ReleaseCOMObject as well as using GC.Collect.

Anyhow, it surely will help if you can send me the beta version to try it
out. Do let me know what exactly I need to do for the same? Which account
I
should send an email to?

We have recently purchased the 4.4 version of Redemption library. Do you
want me to give any reference number for the same?

Many thanks for your help.
Regards,
Amit Kathane


Dmitry Streblechenko said:
The line
rdoFolder.Items;
evaluates to
rdoFolder.Items.Item(i)
So you are still using multiplee dot notation.

Do you want a beta version to see if it fixes this problem for you?

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

Hello Dmitry,

I'm using GC.Collect and modified my code to remove multiple dot
notations
(e.g. rdoFolder.Items.count) but still I'm getting an error for
"MAPI_E_CALL_FAILED".

This is happening especially for RDOMail object.

My code for it reads as

====================================================
Queue DeleteEMailQueue = new Queue();
RDOItems rdoItems = rdoFolder.Items;
for (int i = 0; i < rdoItems.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead, rdoMail.ReceivedTime,
rdoMail.Mileage))
{
rdoMail.Delete(0);
}
}
catch (Exception ex)
{
Util.LogException(ex);
}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
GC.Collect();
}
====================================================

The error occurs after reading say around 700/800 emails in one folder.

Can you please suggest any workaround for this?

Thanks and Regards,
Amit Kathane

:

You are most likely running out of the RPC channel limit
(http://support.microsoft.com/kb/830829) - since .Net does not release
COM
objects immediately, you can easily go over the limit even if you
explicitly
release COM objects using Marshal.ReleaseCOMObject.
Avoid using multiple dot notation (e.g. rdoFolder.Items.Count) to make
sure
the compiler does not create implicit variables that you cannot
reference
and release.
GC.Collect() might help.
Next version of Redemption (send me an e-mail if you want a beta
version)
will keep track of the open MAPI objeects and transparently release
oldest
unmodified MAPI objects that can be easily reopened on demand
(Redemption
objects will stay alive of course) when the number of open objects of
a
particular kind approaches the limit.

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

Hello Dmitry,

Thank you very much for your assistance.

I'm getting following error randomly.

"Error in IMAPIFolder.GetContentsTable: MAPI_E_CALL_FAILED"

Please find below my code

======================================
private static void ProcessUserMailbox(string userName)
{
RDOSession rdoSession = new RDOSessionClass();
try
{
// get the admin user name from the configuration
file
string systemUserName =
Properties.Settings.Default.userName;
string exchangeSrvrName =
Properties.Settings.Default.UserMailboxServer;

if (string.IsNullOrEmpty(systemUserName))
systemUserName =
WindowsIdentity.GetCurrent().Name.ToString();

rdoSession.LogonExchangeMailbox(systemUserName,
exchangeSrvrName);
IRDOFolder rootFolder =
rdoSession.GetSharedMailbox(userName).RootFolder;

// iterate through all folders and subfolders
RDOFolders rdoFolders = rootFolder.Folders;
if (rdoFolders != null)
{
RDOFolder rdoFolder = rdoFolders.GetFirst();
if (rdoFolder != null)
{
// iterate folder list
for (int i = 0; i < rdoFolders.Count; i++)
{
// process the individual folder to
search
for
emails that can be deleted
ProcessFolder(userName, rootFolder.Name +
"

",
rdoFolder);
rdoFolder = rdoFolders.GetNext();
}
}
}
// release the COM object for the root folder.

System.Runtime.InteropServices.Marshal.ReleaseComObject(rootFolder);
rootFolder = null;
}
catch (Exception ex)
{
StringBuilder strInfo = new
StringBuilder(string.Format("{0}, Failed to process mailbox for user
:
{0}",
userName));
Util.LogMessage(strInfo.ToString());
Util.LogException(ex);
strInfo.AppendFormat(ex.Message + Environment.NewLine
+
Util.GetInnerException(ex));
throw new ApplicationException(strInfo.ToString());
}
finally
{
rdoSession.Logoff();

System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoSession);
rdoSession = null;
GC.Collect();
}
}

private static void ProcessFolder(string userName, string
folderPath, RDOFolder rdoFolder)
{
RDOFolders subFolders;
RDOFolder subFolder;

try
{
subFolders = rdoFolder.Folders;
if (subFolders != null)
{
subFolder = subFolders.GetFirst();
if (subFolder != null)
{
// iterate sub folder list
for (int i = 0; i < subFolders.Count;
i++)
{
// process the subfolder
ProcessFolder(userName,
rdoFolder.Name +
"

", subFolder);
subFolder = subFolders.GetNext();
}
}
}
// if found an individual folder (rdoFolder)
without
the
subfolder then release the subfolder and subfolders COM objects

//System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolder);
subFolder = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(subFolders);
subFolders = null;

// if there are no sub folders then process the
rdoFolder and iterate through all items
// process the individual items/emails in the
folder
for (int i = 0; i < rdoFolder.Items.Count; i++)
{
RDOMail rdoMail = rdoFolder.Items;
try
{
// check if email can be deleted
if (DeleteEmail(rdoMail.UnRead,
rdoMail.ReceivedTime, rdoMail.Mileage))
{
Util.LogMessage(string.Format("{0},
Email
with subject '{1}' Read : '{2}' Received Time : '{3}' Mileage :
'{4}'
is
deleted.", userName, rdoMail.Subject, !(rdoMail.UnRead),
rdoMail.ReceivedTime, rdoMail.Mileage));
if (DeleteMail)
rdoMail.Delete(0); //dfSoftDelete
(0) -
default. Deletes the item. Can still be recoverable if retention
policy
is
set on Exchange Server.

//dfMoveToDeletedItems
(1) - the item is moved to the Deleted Items folder
//dfHardDelete
(2) -
Exchange only. Permanently deletes the item; will not be recoverable
}
}
catch (Exception ex)
{
// in case of exception for processing
individual mail log the exception and process next email
Util.LogException(ex);
}
finally
{

//System.Runtime.InteropServices.Marshal.ReleaseComObject(rdoMail);
rdoMail = null;
}
}
}
 
Top