Solved

Outlook InBox using vb.net and Redemption SafeMail

Posted on 2006-07-19
5
882 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
  • 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 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

18 Experts available now in Live!

Get 1:1 Help Now