Solved

Outlook InBox using vb.net and Redemption SafeMail

Posted on 2006-07-19
5
891 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with sorting data in Listbox using VB.NET 3 62
VB.Net - KeyPress Event 4 36
Obtain cell value using column ID in DataGrid using VB.NET 2 19
vb.net 1 month apart 11 30
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

778 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