如何使用最新版本的R RDCOMClient从Outlook发送邮件? [英] How to send mails from outlook using R RDCOMClient using latest Version?

查看:165
本文介绍了如何使用最新版本的R RDCOMClient从Outlook发送邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When i am using latest version R RDCOMClient package for sending outlook Emails, It is showing up an error : "[[<- defined for objects of type "S4" only for subclasses of environment"

代码相同:

    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

推荐答案

我认为以下代码应为您工作.您可能需要像我一样分别定义电子邮件的body,然后将其粘贴到outMail[["HTMLbody"]] = paste0("<p>", body, "</p>", Signature),如下所示.如果您已安装RDCOMClient软件包,那么此代码可以正常工作.我已经使用R(V3.4.2),RDCOMClient和RStudio的最新版本测试了此代码.让我知道这是否对您有帮助.

I think the below code should work for you. You may have to define body of the email separately as I have done and then paste it at outMail[["HTMLbody"]] = paste0("<p>", body, "</p>", Signature) as seen below. This code works without any errors provided you have installed the RDCOMClient package. I have tested this code using the latest version of R (V3.4.2), RDCOMClient and RStudio. Let me know if this helps you.

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

这篇关于如何使用最新版本的R RDCOMClient从Outlook发送邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆