Solved

Outlook InBox using vb.net and Redemption SafeMail

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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