0

I am trying to detect if an email is exchange or not.

Here is the code I have:

Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend

    Dim sendAddress As String

    If Item.SenderEmailType = "EX" Then
        sendAddress = Item.Sender.GetExchangeUser().PrimarySmtpAddress
    Else
        sendAddress = Item.SenderEmailAddress
    End If

    System.Windows.Forms.MessageBox.Show(sendAddress)

End Sub

When I run this, I get the following error:

System.NullReferenceException: 'Object variable or With block variable not set.'

Any ideas why this is?

Bugs
  • 4,356
  • 9
  • 30
  • 39
danyo
  • 5,084
  • 14
  • 55
  • 105
  • What is the type of item? Please add more relevant code. – Esko Apr 25 '17 at 10:30
  • Sorry, i have edited. Does that help? – danyo Apr 25 '17 at 10:31
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bugs Apr 25 '17 at 10:32
  • Object can be anything, guessing it does not have the properties you think it has, ie. ***Sender*** or ***SenderEmailAddress***. Debug the code and see which line throws the error and what does the item consist of. Also I recommend familiarising yourself with strongly typed classes and habits. – Esko Apr 25 '17 at 10:33

1 Answers1

1

Inspired by this answer I think what you want is something like this:

Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles application.ItemSend

    Dim mailItem As Outlook.MailItem = DirectCast(Item, Outlook.MailItem)

    Dim sender As Outlook.AddressEntry = mailItem.Sender
    Dim senderAddress As String = ""

    If sender IsNot Nothing AndAlso
       (sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeAgentAddressEntry OrElse _
        sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry) Then
        Dim exchangeUser As Outlook.ExchangeUser = sender.GetExchangeUser()

        If exchangeUser IsNot Nothing Then
            senderAddress = exchangeUser.PrimarySmtpAddress()
        End If
    Else
        Dim recipient As Outlook.Recipient = application.Session.CreateRecipient(mailItem.SenderEmailAddress)
        If recipient IsNot Nothing Then
            Dim exchangeUser As Outlook.ExchangeUser = recipient.AddressEntry.GetExchangeUser()
            If exchangeUser IsNot Nothing Then
                senderAddress = exchangeUser.PrimarySmtpAddress()
            End If
        End If

        'check if senderAddress has been set with above code. If not try SenderEmailAddress
        If senderAddress = "" Then
            senderAddress = mailItem.SenderEmailAddress()
        End If
    End If

    MessageBox.Show(senderAddress)

End Sub

First thing to note is that I'm casting the object Item to a MailItem. This is so I can correctly access the properties. I would suggest to you to turn Option Strict On:

Restricts implicit data type conversions to only widening conversions, disallows late binding, and disallows implicit typing that results in an Object type.

You may also want to check other OlAddressEntryUserTypes but I'll leave that to you. This code should resolve your error at least.

However after reviewing the above code, I wonder if it is necessary, at least in the Application.ItemSend method. I believe this could be condensed a little to something like this:

Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend

    Dim mailItem As Outlook.MailItem = DirectCast(Item, Outlook.MailItem)

    Dim senderAddress As String = mailItem.SenderEmailAddress

    Dim recipient As Outlook.Recipient = Application.Session.CreateRecipient(senderAddress)
    If recipient IsNot Nothing Then
        Dim exchangeUser As Outlook.ExchangeUser = recipient.AddressEntry.GetExchangeUser()
        If exchangeUser IsNot Nothing Then
            senderAddress = exchangeUser.PrimarySmtpAddress()
        End If
    End If

    MessageBox.Show(senderAddress)

End Sub
Bugs
  • 4,356
  • 9
  • 30
  • 39