Outlook automation security problem

O

Ofer Lavi

Hello,

I am trying to reach the MailItem SenderName property.
Now, I know it is blocked since the email security update, and I
tested my code on a system without the property, and it went well.

However, the behavior I expected is seeing a pop-up window asking me
for permission to access the item (similar to the one I get
synchronizing my Palm handheld, but inetead I get an exception "object
reference not set to an instance of object". Not only that, but
afterwards trying to go back to the Outlook application, it crashes
with an access violation.

I checked it on VB6 and VB.net but the result is the same.

Here is my vb.net smaple code to reproduce that:

Dim m_application As Outlook.Application = New
Outlook.Application()
Dim mail_item As Outlook.MailItem =
m_application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Items.Item(1)
' This is to check everything is OK. (I get the body msg here in
runtime)
MsgBox(mail_item.Body)
Dim ex As Exception
Try
MsgBox(mail_item.SenderName)
Catch ex
MsgBox(ex.ToString)
MsgBox(ex.StackTrace.ToString)
MsgBox(ex.InnerException.ToString)
End Try

I am using Outlook 2000 (and object reference ver 9.0) with Windows
2000.

Thanks for your help,
Ofer.
 
D

Dmitry Streblechenko

Make sure you logon to MAPI:
Dim m_application As Outlook.Application = New Outlook.Application()
set NS = m_application.GetNamespace("MAPI")
NS.Logon

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
O

Ofer Lavi

Dmitry Streblechenko said:
Make sure you logon to MAPI:
Dim m_application As Outlook.Application = New Outlook.Application()
set NS = m_application.GetNamespace("MAPI")
NS.Logon

Thank you. I did this, but it still doesn't work. Note that I do get
the other fields (which are not restricted) likt the body of the
message. I guess it is some kind of a dll problem, and that for some
reason the trigger for the security window, asking for my permission
to read the addressbook for 1-10 minutes fails.

The behavior is the same in three different computers. Is this exact
code works for you?
 
W

Wei-Dong Xu [MSFT]

Hi Ofer,

Thank you for replying!

For the outlook mail security, the kb article 262701 will provide more information for you. Please go to:
262701 OL2000: Developer Information About the Outlook E-mail Security Update
http://support.microsoft.com/?id=262701

Furthermore, I have write one sample code with detailed commentary for you in C# for retrieving the first mail from Outlook inbox which may
provide some assistance for you on this issue.

//--Code begin ------------------------------

using System;
using System.Reflection; //for Missing.Value
using Microsoft.Office.Interop.Outlook;

...

try
{
// Create Outlook Application
// in-line initialization
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();

// Get MAPI Namespace
Microsoft.Office.Interop.Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

// Logon using default profile or existing session, no dialog
oNS.Logon(Missing.Value,Missing.Value,false,true);

// Alternate logon using a specific profile name
// TODO: if using this logon, specify correct profile name
// and comment the Logon line above
//oNS.Logon("profilename",Missing.Value,false,true);

//Get Inbox folder
Microsoft.Office.Interop.Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(
Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

//Get Items collection in the Inbox folder
Microsoft.Office.Interop.Outlook.Items oItems = oInbox.Items;

// Get first message
// Since Items could contain various item types,
// use explicit type casting with the assignment
Microsoft.Office.Interop.Outlook.MailItem oMsg = ( Microsoft.Office.Interop.Outlook.MailItem)oItems.GetFirst();

//Output some common properties
Console.WriteLine(oMsg.Subject);
Console.WriteLine(oMsg.SenderName); //pop up alert window
Console.WriteLine(oMsg.ReceivedTime);
Console.WriteLine(oMsg.Body); //pop up alert window

//check for attachments
int AttachCnt = oMsg.Attachments.Count;

//Display the message
oMsg.Display(true); //modal

//Logoff
oNS.Logoff();

//Explicitly release objects
oMsg = null;
oItems = null;
oInbox = null;
oNS = null;
oApp = null;


}
//Error handler
catch (System.Exception exp)
{
Console.WriteLine("{0} Exception caught: ", exp);
}

//--Code end -------------------------------

When I am going to retrieve the Sendername and body property, Outlook will popup alert window.

Please feel free to let me know if you have any error when runs the codes above.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
O

Ofer Lavi

Thank you for your reply.

I didn't think i did something wrong in the first place, but
nevertheless, I copied your code except for a minor change:

The compiler didn't recognize the Microsoft.Office.Interop.* (it
marked the Office as problematic in the first place, and Interop as
problematic after I added a reference to the .net office dll -
Microsoft.net\Framework\v1.0.3705\office.dll)

So I added the Microsoft Outlook 9.0 Object Library msoutl9.olb (which
produced the Interop.Outlook.dll), and changed all
Microsoft.Office.Interop.Outlook.* in the code to Outlook.*)

Anyway, It crashed in the exact same manner as my vb.net project:

I could see the subject with no problem:

Console.WriteLine(oMsg.Subject);

But I got an exception at the line retrieving the SenderName property:

Console.WriteLine(oMsg.SenderName);

This was the program output:

Letter to Nahum
System.Runtime.InteropServices.COMException (0x80010105): The server
threw an exception.
at Outlook._MailItem.get_SenderName()
at WindowsApplication2.Form1.Form1_Load(Object sender, EventArgs e)
in c:\documents and settings\ofer\my documents\visual studio
projects\windowsapplication2\form1.cs:line 113 Exception caught:
The program '[2260] WindowsApplication2.exe' has exited with code 0
(0x0).

"Letter to Nahum" is the first message's subject.
The exception is what I go afterwards.

Then, when I went back to the Outlook application window, the
application crashed on an access violation.

It is not the normal behavior. I know I should expect the security
warning, and that the crash shouldn't happen.

a. Is it because I used the wrong references (msoutl9.olb), and if so
- what should I use? What exactly should I add in the "Add reference"
menu?
b. If not - what could be the origin of the bug? I would like to
mention that I use Windows 2000 with Outlook 2000 in Hebrew.

Thanks,
Ofer.
 
W

Wei-Dong Xu [MSFT]

Hi Ofer,

Thank you for replying and more information about the troubleshooting!

I'd suggest you can add reference from the visual studio for the Microsoft outlook 9.0 library in the com tab of reference. This way, Visual studio will
use utility to import the unmanaged com component into .net project with .net interop technology so that we can access the unmanaged object
from .net. Then you can start object viewer to check whether the namespace "Microsoft.office.interop" has been imported into the .net project.
Then have a test for the project. Furthermore, my sample code is built in C# so you'd better change the syntax from C# to vb.net.

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top