Monitoring an MS Exchange mailbox

Tim Golden tim.golden at viacom-outdoor.co.uk
Tue Dec 30 11:17:06 CET 2003


>From: Lindstrom Greg - glinds [mailto:Greg.Lindstrom at acxiom.com]
>
>I have written a script to monitor my MS Exchange mailbox for certain
>messages and post information to an MS SQL Server Database.  Everything
>works great except that each time I run the script I am 
>prompted by Windows
>to choose the profile of the mailbox via a dialog box.
>
>Is there any way to either specify the profile or accept the default?  

You have three options that I can think of. I assume you're using CDO
(ie the MAPI.Session COM object). Each of the techniques should leave
you with a working session. The Logon method has a number of other
parameters controlling, for example, whether a dialog box should be
displayed if the logon fails and so on. Consult MSDN for details: it's
quite lucid on the subject.

1) Hardcode a profile name with the options you need and pass it in:

<code>
import win32com.client
session = win32com.client.Dispatch ("MAPI.Session")
session.Logon (ProfileName="Tim Golden") 
# obviously replace my name by the 
# display name of your profile, the
# one that appears in the dialog box
</code>

2) If you don't have that much control, find the user's default
   profile and use that:

<code>
import win32com.client
import _winreg

reg1 = "\\".join ([
  "Software",
  "Microsoft",
  "Windows NT",
  "CurrentVersion",
  "Windows Messaging Subsystem",
  "Profiles"
])
reg2 = "\\".join ([
  "Software",
  "Microsoft",
  "Windows Messaging Subsystem",
  "Profiles"
])

try:
  key = _winreg.OpenKey (_winreg.HKEY_CURRENT_USER, reg1)
except:
  try:
    key = OpenKey (_winreg.HKEY_CURRENT_USER, reg2)
  except:
    key = None

if key:
  try:
    default_profile, should_be_string = \
      _winreg.QueryValueEx (key, "DefaultProfile")
  except:
    default_profile = ""

session = win32com.client.Dispatch ("MAPI.Session")
session.Logon (ProfileName=default_profile)
</code>

3) One other option, with limitations, is to construct a
   profile info string on the fly, and pass that to the
   logon method:

<code>
import win32com.client

EXCHANGE_SERVER = "xyz"
MAILBOX_NAME = "tim.golden"
#
# Obviously replace the server and
# mailbox names to suit.
#

session = win32com.client.Dispatch ("MAPI.Session")
session.Logon ("%s\n%s" % (EXCHANGE_SERVER, MAILBOX_NAME))
</code>

TJG


________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________





More information about the Python-list mailing list