[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Outlook InBox using vb.net and Redemption SafeMail

Posted on 2006-07-19
5
Medium Priority
?
902 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 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