routlookrdcomclient

RDCOM Client Error: Unable to send Emails


I am currently trying to send 5 individual emails and spreadsheets through R using the RDCOMClient library. Here are the spreadsheets I am attempting to send in a 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"  

The spreadsheets paths seem to be correct. Here is my script to send out the emails:

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()
}

The first two spreadsheets get sent out fine, however when the script reaches [3] "C:/Users/sxc3gfl/Documents/data-science-team-master/SO TTI POWER EQUIPMENT.xlsx" I get the error listed below

 [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"

ERROR

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

Solution

  • First of all, there is no need to create a new Outlook Application instance each time in the loop:

    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()
    }
    

    Instead, you can create a new Application once and then just create new mail items and then send them.

    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()
    }
    

    Finally, make sure you pass a valid file path to the Add method. And the method is called for each file (entry) separately. It can't process files in batch.