danyo
danyo

Reputation: 5846

Check if Outlook SenderEmailType is exchange and set variable

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?

Upvotes: 0

Views: 814

Answers (1)

Bugs
Bugs

Reputation: 4489

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

Upvotes: 1

Related Questions