如何使用R RDCOMClient使用最新版本从outlook发送邮件?

时间:2017-03-23 09:39:16

标签: r email outlook shiny

当我使用最新版本的R RDCOMClient包发送Outlook电子邮件时,它显示错误:  " [[< - 为类型为#34的对象定义; S4"仅适用于环境的子类"

相同的代码:

    library(RDCOMClient)
    ## init com api
    OutApp <- COMCreate("Outlook.Application")
    ## create an email 
    outMail = OutApp$CreateItem(0)

    outMail$GetInspector()      

    signature = outMail[["HTMLBody"]]
    ## configure  email parameter 
    outMail[["To"]] = "some@outlook.com"
    outMail[["CC"]] <- "Some@outlook.com"
    outMail[["subject"]] = "some subject"
    outMail[["body"]] = "some body"
    outMail[["Attachments"]]$Add("C:\\Users\\Some\\Desktop\\file.csv")

    outMail[["HTMLBody"]] = paste0('<p>some body', signature, '</p>')
    ## send it                     
    outMail$Send()

**Error:**
 signature = outMail[["HTMLBody"]]
Error in mget(plabels[hasSubclass], env) : invalid first argument
## configure  email parameter 
outMail[["To"]] = "some@outlook.com"
Error in `[[<-`(`*tmp*`, "To", value = "some@outlook.com") : 
  [[<- defined for objects of type "S4" only for subclasses of environment

1 个答案:

答案 0 :(得分:0)

我认为以下代码应该适合您。您可能必须像我一样单独定义电子邮件的body,然后将其粘贴到outMail[["HTMLbody"]] = paste0("<p>", body, "</p>", Signature),如下所示。如果您安装了RDCOMClient软件包,此代码可以正常运行。我使用最新版本的R(V3.4.2),RDCOMClient和RStudio测试了这段代码。如果这有助于你,请告诉我。

library(RDCOMClient)

OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)

# Get signature from outlook
# GetInspector renders the message in html format.
# Note that if you have not created any signatures, this will return blank
outMail$GetInspector()
Signature <- outMail[["HTMLbody"]]

# Define the body of you email separately
body <- "Define your body here."

outMail[["To"]] = "test@test.com"
outMail[["subject"]] = "TEST EMAIL"

# Paste the body and signatures into the email body
outMail[["HTMLbody"]] = paste0("<p>", body, "</p>", Signature)

# Add your attachment
outMail[["Attachments"]]$Add("C:\\Users\\Some\\Desktop\\file.csv")

outMail$Send()