5
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"
r2evans
  • 141,215
  • 6
  • 77
  • 149
Parth Kalra
  • 127
  • 1
  • 1
  • 10

1 Answers1

4

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

AColeman
  • 485
  • 3
  • 8
Roger-123
  • 2,232
  • 1
  • 13
  • 33
  • Thanks to @AColeman for edits to improve my answer and for creating the `extrospectr` package – Roger-123 Feb 19 '20 at 19:33
  • 2
    I'm glad it came in handy! @Parth Kalra, please accept this answer if it solves your issue, or provide details as to the errors or issues that you receive when trying to implement it on your machine. – AColeman Feb 21 '20 at 01:52
  • @Roger-123 Thank you this was really helpful. – Parth Kalra Feb 24 '20 at 09:15
  • @AColeman Using the same solution mentioned it works, but it's taking a long long time, when we have a big list to find. Currently I'm having >50k email id's to scan for. Is there any way to make it more robust ? – Parth Kalra Mar 06 '20 at 06:52
  • @Roger-123 can you help make it more robust ? it's taking long time, let's say 1 to 2 sec for 1 ID. – Parth Kalra Mar 06 '20 at 06:53
  • Could you create a new question and post the updated code with a request on how to speed it up? That's probably a clearer path to understanding the issue and getting it resolved. Just post the new link in the comments here. – Roger-123 Mar 19 '20 at 18:38