?
Solved

Outlook InBox using vb.net and Redemption SafeMail

Posted on 2006-07-19
5
Medium Priority
?
900 Views
Last Modified: 2012-05-05
I found a bit of vb.net code on MSDN which sequences through an Outlook / Exchange mailbox.   I've been unable to modify it to use Redemption (and avoid the security permissions dialog box).  Could someone give me a hand?  

The console application code is below.  I've indicated sections of interest with '***
The error System.InvalidCast Exception occurs while looping through the inbox.  

I know that I should change the line "Dim oItems As Outlook.Items = oInbox.Items" to something but I'm not sure what.

Any assistance is appreciated!
**********************************************

Imports System.Reflection
Imports Microsoft.Office.Interop
Imports Redemption

Module Module1

    Sub Main()
        ' Create Outlook application.
        Dim oApp As Outlook.Application = New Outlook.Application
        ' String used for comparison with mail item.
        Dim sClassComp = "IPM.Note"

        ' Get Mapi NameSpace.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")

        ' Get Messages collection of Inbox.
        Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        ' ***
        ' *** Change line below to ??? to make Remedy Enabled.
        Dim oItems As Outlook.Items = oInbox.Items
        ' ***
        Console.WriteLine("Total : " & oItems.Count)

        ' Get unread e-mail messages.
        oItems = oItems.Restrict("[Unread] = true")
        Console.WriteLine("Total Unread : " & oItems.Count)

        ' Loop each unread message.
        ' ***
        ' *** Original Line is Below
        ' Dim oMsg As Outlook.MailItem
        ' Remedy Enabled
        Dim sMsg As Redemption.SafeMailItem
        ' ***

        Dim i As Integer
        For i = 1 To oItems.Count
            'Test to make sure item is a mail item
            'and not a meeting request.
            If oItems.Item(i).MessageClass = sClassComp Then
                ' *** System.InvalidCast Exception Occurs Here
                sMsg = oItems.Item(i)
                ' ***
                Console.WriteLine(i)
                Console.WriteLine(sMsg.SenderName.ToString)
                Console.WriteLine(sMsg.Subject.ToString)
                Console.WriteLine(sMsg.ReceivedTime.ToString)
                Console.WriteLine(sMsg.BodyFormat.ToString)
                Console.WriteLine(sMsg.UnRead.ToString)

                Console.WriteLine("---------------------------")
            End If
        Next

        ' Clean up.
        oApp = Nothing
        oNS = Nothing
        oItems = Nothing
        sMsg = Nothing

    End Sub
End Module

0
Comment
Question by:RichardKline
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:ptakja
ID: 17138996
I couldn't find my sample code but I did find this link:

http://www.ureader.com/message/902144.aspx

Basically, you need to create the redemption "Safe" items to use instead of the Outlook objects.
0
 
LVL 14

Expert Comment

by:jjardine
ID: 17140461
I believe that there is a sub property for the safemailitem  In your line where the invalid cast exception occurs change the SMsg = olItems.Item(i) to   SMsg.Item = olItems.Item(i)  and see if that fixes it.
0
 
LVL 1

Author Comment

by:RichardKline
ID: 17141107
jjardine,  That makes sense  but...
   sMsg.Item = oItems.Item(i) produces 'System.NullReferenceException'
The orginal code produces the same error if I use
   OMsg.Item = oItems.Item(i)

OMsg = oItems.Item(i) works correctly (but does give the security warning).

Thanks!

   
0
 
LVL 14

Accepted Solution

by:
jjardine earned 2000 total points
ID: 17141211
Is it possible to set   sMsg = new SafeMailItem?  That is where your null reference is happening.. You declare the object but you don't instantiate it before calling .item
0
 
LVL 1

Author Comment

by:RichardKline
ID: 17141244
That did it!
Many thanks to all!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 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