如何使用 R RDCOMClient 检索 Outlook 收件箱电子邮件?
How to retrieve Outlook inbox emails using R RDCOMClient?
library(RDCOMClient)
## create outlook object
OutApp <- COMCreate("Outlook.Application")
我想从名为 'AUX' 的 Outlook 文件夹中检索今天的电子邮件。
解析邮件标题,如果满足一定条件,我想解析邮件内容中的某些字符串。
我设法从 R 写了一封电子邮件并发送出去,但到目前为止无法检索电子邮件。
这是我通过反复试验得到的一些示例代码:
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
# Just doing first 10, get total number with emails()$Count()
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 (still relevent for later versions of Outlook), or else check out my personal website, http://www.gregthatcher.com(查看 "Scripts" 部分——我已经编译这些内容很多年了。)
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']]
}
library(RDCOMClient)
## create outlook object
OutApp <- COMCreate("Outlook.Application")
我想从名为 'AUX' 的 Outlook 文件夹中检索今天的电子邮件。 解析邮件标题,如果满足一定条件,我想解析邮件内容中的某些字符串。
我设法从 R 写了一封电子邮件并发送出去,但到目前为止无法检索电子邮件。
这是我通过反复试验得到的一些示例代码:
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
# Just doing first 10, get total number with emails()$Count()
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 (still relevent for later versions of Outlook), or else check out my personal website, http://www.gregthatcher.com(查看 "Scripts" 部分——我已经编译这些内容很多年了。)
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']]
}