remailoutlooksenderrdcomclient

Is there a way to find/read the Sender Email Address using R, RDCOMClient


library("tm")
library("NLP")
library("dplyr")
library("readtext")
library("readxl")
library("foreach")
library("devtools")
library("RDCOMClient")
library("rlist")

WDF = vector()
OutApp <- COMCreate("Outlook.Application")
outlookNameSpace = OutApp$GetNameSpace("MAPI")

folderName = "Folder Name"
fld <- outlookNameSpace$GetDefaultFolder(6)
fld = fld$folders(folderName)
Cnt = fld$Items()$Count()
emails <- fld$items
df = data.frame(sno = 1:Cnt,Text = "",stringsAsFactors=FALSE)

for(i in 1:10){
  d = as.data.frame(emails(i)$Body(), stringsAsFactors=FALSE)
  df$Text[i] = d[1]
  df$Sender[i] = emails(i)[['SenderName']]
  df$To[i] = emails(i)[['To']]
  df$sub[i] = emails(i)[['subject']]
}
emails(2)[['SenderName']] 

I'm trying to get the senders Email Address by using following code :

emails(2)[['SenderEmailAddress']]

But it ends up giving like this :

[1] "/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=E4CD239AB9F44AC4AC0A4015B6F4805A-RATINGSDIRE"

Solution

  • The problem is that exchange stores the sender address as either the normal smtp version of the address for external users, but for Exchange users it uses the MS Exchange address. To get the normal smtp address, you have to look up the exchange user on and get their normal smtp email address.

    You may want to look at the extrospectr package on github. I haven't used it but it looks like it would give you a clean inbox like you're looking for.

    If you look at the .lookup_exchange_sender function in the file read_inbox.R it shows how they handled looking up the address. First you have to look at what type of user the Sender is (which you can do by retrieving the Sender property of the MailItem, and then the AddressEntryUserType property, which has this enumeration). This ends up like emails(2)$Sender()$AddressEntryUserType().

    Then then if it's an Exchange user, you would need to get the Sender property of the MailItem (which is an AddressEntry) and then use the GetExchangeUser method on the AddressEntry to return an ExchangeUser object. Once you have that you just need to access the PrimarrySMTPAddress property of the ExchangeUser. When you put it all together, it looks like this: emails(2)$Sender()$GetExchangeUser()$PrimarySMTPAddress().

    Link to extrospectr on github: https://github.com/aecoleman/extrospectr

    This explains the Outlook methodology for what's stored in the sender email property: SenderEmailAddress property does not contain a standard email address for internal contacts