RDCOM Client Error: Unable to send Emails

RDCOM Client Error: Unable to send Emails

我目前正在尝试使用 RDCOMClient 库通过 R 发送 5 封单独的电子邮件和电子表格。这是我尝试发送的电子表格 vector

 [1] "C:/Users/sxc3gfl/Documents/data-science-team-master/INTERLINE BRANDS.xlsx"       
 [2] "C:/Users/sxc3gfl/Documents/data-science-team-master/POLY-AMERICA, LP.xlsx"       
 [3] "C:/Users/sxc3gfl/Documents/data-science-team-master/SO TTI POWER EQUIPMENT.xlsx" 
 [4] "C:/Users/sxc3gfl/Documents/data-science-team-master/GENERAC SO.xlsx"             
 [5] "C:/Users/sxc3gfl/Documents/data-science-team-master/SO TOTER LLC.xlsx"  

电子表格路径似乎是正确的。这是我发送电子邮件的脚本:

path_names <- "C:/Users/sxc3gfl/Documents/data-science-team-master/"

attachments <-  c(paste0(path_names, sheet_names28))

for (sheet in sheet_names28) {
  attachments = c(paste0(path_names, sheet))
  OutApp <- COMCreate("Outlook.Application")
  outMail = OutApp$CreateItem(0)
  outMail[["To"]] = paste("user@outlook.com"
                          , sep=";", collapse=NULL)
  outMail[["subject"]] = "Subject"
  outMail[["body"]] = "Hi -
Attached is the spreadsheet 

"
  purrr::map(attachments, ~ outMail[["attachments"]]$Add(.))
  outMail$Send()
}

前两个电子表格发送正常,但是当脚本到达 [3] "C:/Users/sxc3gfl/Documents/data-science-team-master/SO TTI POWER EQUIPMENT.xlsx" 时,我收到下面列出的 错误

 [1] "C:/Users/sxc3gfl/Documents/data-science-team-master/INTERLINE BRANDS.xlsx"       
 [2] "C:/Users/sxc3gfl/Documents/data-science-team-master/POLY-AMERICA, LP.xlsx"

错误

**<checkErrorInfo> 80020009 
No support for InterfaceSupportsErrorInfo
checkErrorInfo -2147352567
Error: Exception occurred.**

首先,不需要在循环中每次都创建一个新的OutlookApplication实例:

for (sheet in sheet_names28) {
  attachments = c(paste0(path_names, sheet))
  OutApp <- COMCreate("Outlook.Application")
  outMail = OutApp$CreateItem(0)
  outMail[["To"]] = paste("user@outlook.com"
                          , sep=";", collapse=NULL)
  outMail[["subject"]] = "Subject"
  outMail[["body"]] = "Hi -
Attached is the spreadsheet 

"
  purrr::map(attachments, ~ outMail[["attachments"]]$Add(.))
  outMail$Send()
}

相反,您可以创建一个新的 Application 一次,然后只创建新的邮件项目并发送它们。

attachments = c(paste0(path_names, sheet))
OutApp <- COMCreate("Outlook.Application")

for (sheet in sheet_names28) {
  outMail = OutApp$CreateItem(0)
  outMail[["To"]] = paste("user@outlook.com"
                          , sep=";", collapse=NULL)
  outMail[["subject"]] = "Subject"
  outMail[["body"]] = "Hi -
Attached is the spreadsheet 

"
  purrr::map(attachments, ~ outMail[["attachments"]]$Add(.))
  outMail$Send()
}

最后,确保将有效的文件路径传递给 Add 方法。并且分别为每个文件(条目)调用该方法。无法批量处理文件。