Solved

PERL - MAPI.Session

Posted on 2006-11-19
15
2,421 Views
Last Modified: 2011-10-03
Hi,

Hello, I got this script from Active State, but when i run it, I keep getting  "Could not create a new MAPI Session".  

thanks for help.

Here's the script:

# Sender's Name and Password
#
my $sender = "username";
my $passwd = "password";

# Create a new MAPI Session
#
use Win32::OLE;
$session = Win32::OLE->new("MAPI.Session")
    or die "Could not create a new MAPI Session: $!";

# Attempt to log on.
#
my $err = $session->Logon($sender, $passwd);
if ($err) {
    die "Logon failed: $!";
}

# Add a new message to the Outbox.
#
$msg = $session->Outbox->Messages->Add();

# Add the recipient.
#
$rcpt = $msg->Recipients->Add();
$rcpt->{Name} = "username";
$rcpt->Resolve();

# Create a subject and a body.
#
$msg->{Subject} = "Test Message";
$msg->{Text} = <<EOF;
This is a sample test message.

Cheers,

Mr. Email

EOF

# Send the message and log off.
#
$msg->Update();
$msg->Send(0, 0, 0);
$session->Logoff();
0
Comment
Question by:d2marcelo
  • 7
  • 7
15 Comments
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
Was that the *complete* error message?

What version of Windows?

What user are you running the script as?

What email setup do you have?
0
 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
Change this:

$session = Win32::OLE->new("MAPI.Session")
    or die "Could not create a new MAPI Session: $!";

to

$session = Win32::OLE->new("MAPI.Session")
    or die "Could not create a new MAPI Session: $! -- ".Win32::OLE->GetLastError();

And check if you're getting an OLE exception.

Do you have Outlook installed?  You need window messaging installed to use MAPI.  It's installed with Microsoft's messaging clients-- Outlook, or the old Microsoft Mail, or Exchange Server Admin.
0
 

Author Comment

by:d2marcelo
Comment Utility
Hi,  

This is the complete error message with the OLE exception

Could not create a new MAPI Session: Bad file descriptor -- Win32::OLE(0.1702) e
rror 0x800401f3: "Invalid class string" at C:\mapi.pl line 15.

I'm using windows 2003
I'm using an outook 2003 account as the script user and pwd.

Thanks,


0
 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
It doesn't sound like you have Outlook installed on the computer you're trying to run this on.  You need the windows messaging components installed for the MAPI controls to be installed.
0
 

Author Comment

by:d2marcelo
Comment Utility
Yes, I have outlook 2003 installed. I check the registry under Windows Messaging subsystem and the key for OLEMessaging is set to 1 as well as MAPIX, MAPI and MAPIXVER.
0
 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
Is there a MAPI profile set up on this machine for the user that this is running as?
0
 

Author Comment

by:d2marcelo
Comment Utility
Yes , there is.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
So logged into this computer as this user Microsoft Outlook works without a problem?  And your script bombs with that error under the same conditions-- logged in as this user, running as this user?  Extremely weird that you're getting the error that you're getting if that's the case.  

Sorry I'm at a loss.  As a sanity check, I'd verify that I had a CLSID registered for a MAPI.Session (although if Outlook is working, it would seem that it would have to be there).  In other words, that the following key exists:

  HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MAPI.Session\CLSID

And that the CLSID referenced (most likely {3FA7DEB3-6438-101B-ACC1-00AA00423326})

  HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{3FA7DEB3-6438-101B-ACC1-00AA00423326}\InprocServer32

references an existing DLL (probably C:\Program Files\Common Files\SYSTEM\MSMAPI\1033\CDO.DLL) and that the user you're running this as has permissions to that DLL.

All of the above should be the case if Outlook runs correctly as this user... don't know where else to go.  Maybe someone else will have come across this.
0
 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
BTW, I would check the above registry keys as the user that you're running your script as... to verify the user has rights to the registry keys involved.
0
 

Author Comment

by:d2marcelo
Comment Utility
Thanks for the help clockwatcher, i've tried all the above but still keep getting the error message.
0
 

Author Comment

by:d2marcelo
Comment Utility
Would you know an alternative way of sending mapi mail from perl?
0
 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
What's the end goal?  I guess my question really is why are you using MAPI?  Are you trying to send a specific type of message (task request, calendar appointment)?  Or do you need the message to end up in Sent Items?  Are you trying to read items in a particular exchange folder?  Why did you decide to go with MAPI?  If your just interested in getting out a normal email (which is basically what you're getting with your sample code) then straight SMTP would make the most sense.

The other options (besides SMTP and MAPI) with Exchange would be IMAP, POP3 and WebDAV.  MAPI is going to be the most well-documented and the most flexible but without knowing what you're trying to do with it-- don't know if you need it.
0
 

Author Comment

by:d2marcelo
Comment Utility
yes, The reason why I need MAPI is because I'll be dealing with (task request, calendar appointment , public folders etc).
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 250 total points
Comment Utility
In that case, you could look into using WebDAV.  Don't know how many people are actually using it (but it would be cross-platform compatible).

Sending a message:
 
  http://support.microsoft.com/kb/296713

Sending a meeting request:

  http://support.microsoft.com/kb/308373

A task request:

  http://groups.google.com/group/microsoft.public.exchange2000.development/browse_thread/thread/64a5a05da1a5a49d/a0350e751a5fef86?lnk=st&q=&rnum=1&hl=en#a0350e751a5fef86

You can also get to public and private folder stores using it.  

But, back to your original question, I don't think the MAPI problem is a perl issue.   I would guess you'd get the same problem with this vbscript.

example.vbs
--------------
set mapi = wscript.createobject("MAPI.Session")

I would see if the script above bombs and if it does post it in the VB category.  Better chance that someone there would have run into the problem you're having there than in the perl category.  Perhaps, it's a permissions issue with 2003 or something.
0
 

Author Comment

by:d2marcelo
Comment Utility
thanks a lot.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video discusses moving either the default database or any database to a new volume.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now