# How to get 'NewMail' event from Outlook

**URL:** <https://www.ruby-forum.com/t/how-to-get-newmail-event-from-outlook/140231>\
**Category:** Ruby\
**Created:** [June 12, 2008, 2:44am UTC](https://www.ruby-forum.com/t/how-to-get-newmail-event-from-outlook/140231 "2008-06-12T02:44:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cklin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@cklin](https://www.ruby-forum.com/u/cklin)\
**Post date:** [June 12, 2008, 2:44am UTC](https://www.ruby-forum.com/t/how-to-get-newmail-event-from-outlook/140231/1 "2008-06-12T02:44:20Z")

</div>

Hi,

I wrote the this script to get the ‘NewMail’ event from Outlook

================newmail.rb=======================  
require ‘win32ole’

ol = WIN32OLE.new(‘Outlook.Application’)  
ev = WIN32OLE\_EVENT.new(ol, ‘ApplicationEvents’)  
ev.on\_event(“NewMail”) { puts ‘NewMail!’ }

k = gets

=================================================

But when I ran the script and tried to send a mail to myself, the  
outlook hung. If I terminate my ruby program, then the outlook comes  
back.

The script seems to affect the execution of outlook.

Does anyone have any idea? Is there anything wroing in my code.

Thanks for your time.

BR,  
cklin

---

<div class="post-metadata">

**Author:** ![cklin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@cklin](https://www.ruby-forum.com/u/cklin)\
**Post date:** [June 12, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/how-to-get-newmail-event-from-outlook/140231/2 "2008-06-12T19:58:32Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Jun 12, 2008, at 2:43 AM, Chuankai Lin wrote:

> k = gets
> 
> =================================================

I don’t know you system, but what should k = gets do? This usually  
lets you script wait for input on $stdin (whatever that may be). This  
is no way to set you script into “waiting mode”!

Regards,  
Florian G.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkhRY60ACgkQJA/zY0IIRZYPYgCgkSRA2FD4KcMhV+a7qj4k04gM  
02kAnRyVptkRJgTffgDO5MQJYdczK657  
=cckK  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![cklin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@cklin](https://www.ruby-forum.com/u/cklin)\
**Post date:** [June 15, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/how-to-get-newmail-event-from-outlook/140231/3 "2008-06-15T17:21:18Z")

</div>

The answer is found by my friend. The script below works on my windows  
box:

=========================================  
require ‘win32ole’

ol = WIN32OLE.new(‘Outlook.Application’)  
#puts ol.Explorers.Item(1)  
#puts ol.ActiveExplorer.CurrentFolder.UnReadItemCount  
ev = WIN32OLE\_EVENT.new(ol, ‘ApplicationEvents’)  
puts ol.GetNamespace(“MAPI”).GetDefaultFolder(6).UnReadItemCount  
ev.on\_event(“NewMail”) { puts ‘NewMail!’ }

# loop { WIN32OLE\_EVENT.message\_loop }

Florian G. wrote:

> -----BEGIN PGP SIGNED MESSAGE-----  
> Hash: SHA1
> 
> On Jun 12, 2008, at 2:43 AM, Chuankai Lin wrote:
> 
> > k = gets
> > 
> > =================================================
> 
> I don’t know you system, but what should k = gets do? This usually  
> lets you script wait for input on $stdin (whatever that may be). This  
> is no way to set you script into “waiting mode”!
> 
> Regards,  
> Florian G.  
> -----BEGIN PGP SIGNATURE-----  
> Version: GnuPG v1.4.8 (Darwin)
> 
> iEYEARECAAYFAkhRY60ACgkQJA/zY0IIRZYPYgCgkSRA2FD4KcMhV+a7qj4k04gM  
> 02kAnRyVptkRJgTffgDO5MQJYdczK657  
> =cckK  
> -----END PGP SIGNATURE-----
