# Ruby iterating over Exchange Mailboxes

**URL:** <https://www.ruby-forum.com/t/ruby-iterating-over-exchange-mailboxes/133225>\
**Category:** Ruby\
**Created:** [March 29, 2008, 2:17am UTC](https://www.ruby-forum.com/t/ruby-iterating-over-exchange-mailboxes/133225 "2008-03-29T02:17:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [March 29, 2008, 2:17am UTC](https://www.ruby-forum.com/t/ruby-iterating-over-exchange-mailboxes/133225/1 "2008-03-29T02:17:01Z")

</div>

Hello,  
I have been hitting my head against the wall for hours now. For the  
life of me, I can’t figure out whats wrong with my script… I have a  
test script that interacts with CDO to access Microsoft Exchange, as  
follows:

require ‘win32ole’

#~ START MAPI/CDO SESSION  
session = WIN32OLE.new(‘Mapi.session’)  
session.logon(“username”, “password”)

#~ GET A LIST OF MAILBOXES TO ITERATE OVER  
address\_list = session.GetAddressList(0)  
address\_entry = address\_list.AddressEntries

#~ INITIATE NEW MAPI SESSION (FOR DYNAMIC LOGONS)  
newsession = WIN32OLE.new(‘Mapi.session’)

#~ ITERATE OVER ALL MAILBOXES  
address\_entry.each do |row|  
profileInfo = “EX” + “\n” + row.Name  
newsession.logon("", “”, “FALSE”, “TRUE”, “0”, “TRUE”, profileInfo)

#~ EXCEPTION HANDLING  
begin  
contacts\_folder = newsession.GetDefaultFolder(5)  
puts newsession.CurrentUser.Name  
rescue Exception =\> blah  
puts “DISABLED”  
puts blah.inspect  
ensure  
newsession.logoff

end  
end

session.Logoff

Basically, the error comes out of the exception handling portion. When I  
comment it out and put “puts newsession.CurrentUser.Name” it gives me  
what I want - which is the script accessing every mailbox on Exchange.  
The error handling says that if GetDefaultFolder(5) (which is the  
Contacts Folder in Outlook) doesn’t exist, then that user is disabled,  
so skip to the next user. For some reason, with the error handling  
section in there, it never outputs consistent data. It could list 5 or 6  
users, then, without changing anything and just running it again, it’ll  
give me 100 users. The message it gives me is as follows:

test\_mapisession.rb:42: [BUG] Segmentation fault  
ruby 1.8.6 (2007-09-24) [i386-mswin32]

This application has requested the Runtime to terminate it in an unusual  
way.  
Please contact the application’s support team for more information.

line 42 of my code is the “newsession.logoff” that is in the ensure  
clause of the error handling statement. Does anybody know what is going  
on? I have never encountered a Segmentation fault… I am lost on this,  
so any and all help is appreciated!

Thanks!!!

- Jeff M.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [March 31, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/ruby-iterating-over-exchange-mailboxes/133225/2 "2008-03-31T23:32:58Z")

</div>

Hello everybody,  
I just got this recommendation from a friend:

“Exchange has a limit of 250 open RPC channels, so you need to release  
objects as you use them or you’ll run out of channels. You’ll probably  
have to dig a little to find out whether Ruby releases objects  
immediately or, like .NET, in its own sweet time and whether it can be  
forced to release immediately.”

Thus, does anybody know how I can force release of an object?

Thanks!!

- Jeff M.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [April 1, 2008, 12:29am UTC](https://www.ruby-forum.com/t/ruby-iterating-over-exchange-mailboxes/133225/3 "2008-04-01T00:29:28Z")

</div>

On Mon, Mar 31, 2008 at 11:33 PM, Jeff M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello everybody,  
> I just got this recommendation from a friend:
> 
> “Exchange has a limit of 250 open RPC channels, so you need to release  
> objects as you use them or you’ll run out of channels. You’ll probably  
> have to dig a little to find out whether Ruby releases objects  
> immediately or, like .NET, in its own sweet time and whether it can be  
> forced to release immediately.”
> 
> Thus, does anybody know how I can force release of an object?

try WIN32OLE#ole\_free:  
[http://ruby-doc.org/stdlib/libdoc/win32ole/rdoc/classes/WIN32OLE.html#M009643](http://ruby-doc.org/stdlib/libdoc/win32ole/rdoc/classes/WIN32OLE.html#M009643)

most probably:  
some\_object.ole\_free  
some\_object = nil

warning: I have not tried this.

Jano

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [April 1, 2008, 7:51am UTC](https://www.ruby-forum.com/t/ruby-iterating-over-exchange-mailboxes/133225/4 "2008-04-01T07:51:54Z")

</div>

On Tue, Apr 1, 2008 at 12:49 AM, Jeff M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks for the reply, but I’ve tried those multiple times… still no  
> luck

You may try running your script under windbg to see where the crash  
happens  
and/or what’s the problem (although you’ll need debug symbols to see  
anything more useful).

J.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [April 1, 2008, 12:49am UTC](https://www.ruby-forum.com/t/ruby-iterating-over-exchange-mailboxes/133225/5 "2008-04-01T00:49:35Z")

</div>

Thanks for the reply, but I’ve tried those multiple times… still no  
luck

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [February 25, 2011, 6:21pm UTC](https://www.ruby-forum.com/t/ruby-iterating-over-exchange-mailboxes/133225/6 "2011-02-25T18:21:37Z")

</div>

I have been working for some months with some kinds of programs which  
might resolve any problem just like this. One of the best to my mind is  
the next utility, which is able to show how it recovers mailboxes bigger  
than 2gb in Exchange 2003 and etc. What is more this tool is quite  
reliable and easy to use -  
[http://www.mailboxexchangerecovery.com](http://www.mailboxexchangerecovery.com)
