d2marcelo
asked on
PERL - MAPI.Session
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.Sess ion")
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();
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.Sess
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 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();
Change this:
$session = Win32::OLE->new("MAPI.Sess ion")
or die "Could not create a new MAPI Session: $!";
to
$session = Win32::OLE->new("MAPI.Sess ion")
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.
$session = Win32::OLE->new("MAPI.Sess
or die "Could not create a new MAPI Session: $!";
to
$session = Win32::OLE->new("MAPI.Sess
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.
ASKER
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,
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,
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.
ASKER
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.
Is there a MAPI profile set up on this machine for the user that this is running as?
ASKER
Yes , there is.
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\SOFTWAR E\Classes\ MAPI.Sessi on\CLSID
And that the CLSID referenced (most likely {3FA7DEB3-6438-101B-ACC1-0 0AA0042332 6})
HKEY_LOCAL_MACHINE\SOFTWAR E\Classes\ CLSID\{3FA 7DEB3-6438 -101B-ACC1 -00AA00423 326}\Inpro cServer32
references an existing DLL (probably C:\Program Files\Common Files\SYSTEM\MSMAPI\1033\C DO.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.
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\SOFTWAR
And that the CLSID referenced (most likely {3FA7DEB3-6438-101B-ACC1-0
HKEY_LOCAL_MACHINE\SOFTWAR
references an existing DLL (probably C:\Program Files\Common Files\SYSTEM\MSMAPI\1033\C
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.
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.
ASKER
Thanks for the help clockwatcher, i've tried all the above but still keep getting the error message.
ASKER
Would you know an alternative way of sending mapi mail from perl?
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.
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.
ASKER
yes, The reason why I need MAPI is because I'll be dealing with (task request, calendar appointment , public folders etc).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks a lot.
What version of Windows?
What user are you running the script as?
What email setup do you have?