6

Background:

Using an Outlook Rule to trigger a script, I want all of my email from certain high-visibility clients to open immediately upon receipt.

I have successfully set up a rule that triggers when those clients send me email. It looks something like this:

emails from Jobs and Wozniak

And I have successfully created a script that opens a reply to the email, depending on who sent it. It looks something like this:

Sub OpenEmailImmediately(oEmail As Outlook.MailItem)

    Select Case oEmail.Sender
        Case "Jobs, Steve"
            oEmail.Reply.Display
    End Select

End Sub

Although this works, I have implemented it using .Sender property.

Problem:

I would prefer to implement this using the sender's email address (a unique value associated with every email I receive). Unfortunately, oEmail.SenderEmailAddress, which I expected to contain the email address, only worked for external clients.

While troubleshooting, I discovered that where I expected oEmail.SenderEmailAddress to have a value similar to this:

steve.jobs@apple.com

for internal emails it had a value similar to this:

/O=APPLE/CN=RECIPIENTS/CN=JOBSS6738

Question:

Is there a way I can make this script work for internals and externals using their standard email address?

Code with implemented solution:

Using Dmitry's answer below, the code was modified to use the email address:

Sub OpenEmailImmediately(oEmail As Outlook.MailItem)

    If oEmail.SenderEmailType = "EX" Then
        Select Case oEmail.Sender.GetExchangeUser().PrimarySmtpAddress
            Case "steve.jobs@apple.com"
                oEmail.Reply.Display
        End Select
    Else
        Select Case oEmail.SenderEmailAddress
            Case "tom.brady@patriots.com"
                oEmail.Reply.Display
        End Select
    End If

End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Instant Breakfast
  • 1,383
  • 2
  • 14
  • 28
  • 3
    Does this answer your question? [Get sender's email address with Excel VBA](https://stackoverflow.com/questions/34922075/get-senders-email-address-with-excel-vba) – FreeSoftwareServers Jun 25 '21 at 20:15
  • This is not the same question, and although they both work with .SenderEmailAddress the nuance is different. In my opinion, hiding and closing this question will be a disservice to the community. – Instant Breakfast Jun 27 '21 at 10:42

1 Answers1

12

This is a perfectly valid address of type "EX" (as opposed to SMTP) - check the value of the MailItem.SenderEmailType property. If it is "SMTP", use MailItem.SenderEmailAddress. If it is "EX", use MailItem.Sender.GetExchangeUser().PrimarySmtpAddress.

You can also try to read PidTagSenderSmtpAddress MAPI property (DASL name http://schemas.microsoft.com/mapi/proptag/0x5D01001F) using MailItem.PropertyAccessor.GetProperty - take a look at the message with OutlookSpy (I am its author - click IMessage button) or MFCMAPI.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78