如何使用R RDCOMClient检索Outlook收件箱电子邮件?

时间:2017-03-03 08:09:40

标签: r email outlook rdcomclient

library(RDCOMClient)
## create outlook object
OutApp <- COMCreate("Outlook.Application")

我想从名为&#39; AUX&#39;的Outlook文件夹中检索今天的电子邮件。 通过电子邮件的标题解析,如果符合某些条件,我想解析电子邮件的内容以查找某些字符串。

我设法写了一封来自R的电子邮件并将其发送出去,但到目前为止还无法检索到这些电子邮件。

2 个答案:

答案 0 :(得分:7)

以下是我通过反复试验工作的一些示例代码:

library(RDCOMClient)

folderName = "AUX"

## create outlook object
OutApp <- COMCreate("Outlook.Application")
outlookNameSpace = OutApp$GetNameSpace("MAPI")

folder <- outlookNameSpace$Folders(1)$Folders(folderName)
# Check that we got the right folder
folder$Name(1)

emails <- folder$Items

# Can't figure out how to get number of items, so just doing first 10
for (i in 1:10)
{
  subject <- emails(i)$Subject(1)
  # Replace "#78" with the text you are looking for in Email Subject line
  if (grepl("#78", subject)[1]){
    print(emails(i)$Body())
    break
  } 
}

很抱歉,但我不知道为什么其中一些COM对象需要参数(如Subject(1)),但其他人不喜欢(如Body())。这在Outlook 2013上对我有用,但它也适用于2007年以来的所有Outlook版本。

要获得有关Outlook对象模型的更多信息,我建议您获取Ken Slovak's Outlook 2007 book(对于更高版本的Outlook仍然相关),或者查看我的个人网站http://www.gregthatcher.com(查看&#34;脚本&#34;部分 - 我多年来一直在编译它们。)

答案 1 :(得分:0)

folderName = "foldername"

## create outlook object
OutApp <- COMCreate("Outlook.Application")
outlookNameSpace = OutApp$GetNameSpace("MAPI")

fld <- outlookNameSpace$GetDefaultFolder(6)

# Check that we got the right folder
fld = fld$folders(folderName)

Cnt = fld$Items()$Count()

emails <- fld$items

df = data.frame(sno = 1:Cnt,Text = "",stringsAsFactors=FALSE)

for(i in seq(Cnt)){
  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']]
}