有没有人知道是否可以通过Outlook在R中发送电子邮件。 sendmailR的所有示例都使用gmail服务器。我不能这样做。有什么想法吗?
谢谢!
答案 0 :(得分:71)
您可以使用RDCOMClient
包从R中访问COM对象。您可以轻松访问Application Object (Outlook)并将其配置为发送电子邮件。这是一个显示如何发送和发送电子邮件的简单示例:
library(RDCOMClient)
## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email
outMail = OutApp$CreateItem(0)
## configure email parameter
outMail[["To"]] = "dest@dest.com"
outMail[["subject"]] = "some subject"
outMail[["body"]] = "some body"
## send it
outMail$Send()
当然,这假设您已经安装了Outlook并将其配置为发送/接收您的电子邮件。此外,您可以扩展它以添加附件。
outMail[["Attachments"]]$Add(path_to_attch_file)
outMail[["SentOnBehalfOfName"]] = "yoursecondary@mail.com"
答案 1 :(得分:13)
可以通过Outlook在R中发送电子邮件。 sendmailR适用于Windows 7和Outlook 2010.我引用了http://cran.es.r-project.org/web/packages/sendmailR/sendmailR.pdf
smtpServer = Outlook 2010的信息位于文件中 - &gt;帐户设置 - &gt;帐户设置 - &gt;双击您的帐户 - &gt;文本&#34;服务器&#34;框
library(sendmailR)
#set working directory
setwd("C:/workingdirectorypath")
#####send plain email
from <- "you@account.com"
to <- "recipient@account.com"
subject <- "Email Subject"
body <- "Email body."
mailControl=list(smtpServer="serverinfo")
sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)
#####send same email with attachment
#needs full path if not in working directory
attachmentPath <- "subfolder/log.txt"
#same as attachmentPath if using working directory
attachmentName <- "log.txt"
#key part for attachments, put the body and the mime_part in a list for msg
attachmentObject <- mime_part(x=attachmentPath,name=attachmentName)
bodyWithAttachment <- list(body,attachmentObject)
sendmail(from=from,to=to,subject=subject,msg=bodyWithAttachment,control=mailControl)
此外,可以通过将另一个mime_part添加到msg列表来发送多个文件,如下所示(我也将其压缩):
attachmentObject <- mime_part(x="subfolder/log.txt",name="log.txt")
attachmentObject2 <- mime_part(x="subfolder/log2.txt",name="log2.txt")
bodyWithAttachment <- list(body,attachmentObject,attachmentObject2)
答案 2 :(得分:6)
您还可以使用vbscript公开Outlook的所有功能
参考:MailItem Members (Outlook) - MSDN - Microsoft
################################################
#Outlook Enumerations
################################################
#OlMailRecipientType Enumeration (Outlook)
OlMailRecipientType <- c(olBCC=3, olCC=2, olOriginator=0, olTo=1)
#OlBodyFormat Enumeration (Outlook)
OlBodyFormat <- c(olFormatHTML=2, olFormatPlain=1, olFormatRichText=3, olFormatUnspecified=0)
#OlImportance Enumeration (Outlook)
OlImportance <- c(olImportanceHigh=2, olImportanceLow=0, olImportanceNormal=1)
#OlSensitivity Enumeration (Outlook)
OlSensitivity <- c(olConfidential=3, olNormal=0, olPersonal=1, olPrivate=2)
#OlDefaultFolders Enumeration (Outlook)
OlDefaultFolders <- c(olFolderCalendar=9, olFolderConflicts=19, olFolderContacts=10, olFolderDeletedItems=3,
olFolderDrafts=16, olFolderInbox=6, olFolderJournal=11, olFolderJunk=23,
olFolderLocalFailures=21, olFolderManagedEmail=29, olFolderNotes=12, olFolderOutbox=4,
olFolderSentMail=5, olFolderServerFailures=22, olFolderSyncIssues=20,
olFolderTasks=13, olFolderToDo=28, olPublicFoldersAllPublicFolders=18, olFolderRssFeeds=25)
################################################
#function to send email using Outlook
################################################
outlookSend <- function(To, Subject='', CC=NULL, BCC=NULL,
HTMLBodyFile=NULL, HTMLBody=NULL, Body='', Attachments=NULL,
DeferredDeliveryTime=NULL, OriginatorDeliveryReportRequested=NULL, ReadReceiptRequested=NULL,
FlagRequest=NULL, Importance=NULL, Sensitivity=NULL,
SentOnBehalfOfName=NULL)
{
vbscript <- c('Dim objOutlook',
'Dim objMailItem',
'Dim objFileSystem',
'Dim objNamespace',
'Dim objSentFolder',
'Set objOutlook = CreateObject("Outlook.Application")',
'Set objMailItem = objOutlook.CreateItem(0)',
'With objMailItem',
paste0('\t.Subject = "', Subject, '"'),
#Add recipients
unlist(lapply(To, function(recip) c(
paste0('\tSet recip = .Recipients.Add ("',recip,'")'),
paste('\trecip.Type =',OlMailRecipientType['olTo'])))),
if (!is.null(CC)) {
unlist(lapply(To, function(recip) c(
paste0('\tSet recip = .Recipients.Add ("',recip,'")'),
paste('\trecip.Type =',OlMailRecipientType['olCC']))))
},
if (!is.null(BCC)) {
unlist(lapply(To, function(recip) c(
paste0('\tSet recip = .Recipients.Add ("',recip,'")'),
paste('\trecip.Type =',OlMailRecipientType['olBCC']))))
},
'\t.Recipients.ResolveAll',
#Insert email body
if (!is.null(HTMLBodyFile)) {
c(paste('\t.BodyFormat =', OlBodyFormat['olFormatHTML']),
'\tSet objFileSystem = CreateObject("Scripting.FileSystemObject")',
paste0('\tSet file = objFileSystem.OpenTextFile("',HTMLBodyFile,'", 1)'),
'\t.HTMLBody = file.ReadAll')
} else if (!is.null(HTMLBody)) {
c(paste('\t.BodyFormat =', OlBodyFormat['olFormatHTML']),
paste0('\t.HTMLBody = "',HTMLBody,'"'))
} else {
c(paste('\t.BodyFormat =', OlBodyFormat['olFormatPlain']),
paste0('\t.Body = "', Body, '"'))
},
#Add attachments
if (!is.null(Attachments)) {
vapply(Attachments, function(x) paste0('\t.Attachments.Add "',x,'"'), character(1))
},
#copy of the mail message is saved down
'\t.DeleteAfterSubmit = False',
#Delay delivery in minutes
if (!is.null(DeferredDeliveryTime)) {
paste0('\t.DeferredDeliveryTime = dateadd("n", ',DeferredDeliveryTime,', Now)')
},
#Indicates the requested action for a mail item
if (!is.null(FlagRequest)) {
'\t.FlagRequest = "Follow up"'
},
#Indicates the relative importance level for the mail item
if (!is.null(Importance)) {
paste('\t.Importance =', OlImportance[Importance])
},
#OriginatorDeliveryReportRequested of type logical: whether to receive delivery report
if (!is.null(OriginatorDeliveryReportRequested) && OriginatorDeliveryReportRequested) {
'\t.OriginatorDeliveryReportRequested = True'
},
#ReadReceiptRequested of type logical: request read receipt
if (!is.null(ReadReceiptRequested) && ReadReceiptRequested) {
'\t.ReadReceiptRequested = True'
},
#Indicates the the display name for the intended sender of the mail message
if (!is.null(SentOnBehalfOfName)) {
paste('\t.SentOnBehalfOfName =', SentOnBehalfOfName)
},
#Indicates the sensitivity for the Outlook item
if (!is.null(Sensitivity)) {
paste('\t.Sensitivity =', OlSensitivity[Sensitivity])
},
'End With',
'objMailItem.Send',
'Set objFileSystem = Nothing',
'Set objMailItem = Nothing',
'Set objOutlook = Nothing')
vbsfile <- tempfile('vbs',getwd(),'.vbs')
write(vbscript, vbsfile)
shell(paste('cscript /NOLOGO', vbsfile))
unlink(vbsfile)
} #outlookSend
#Examples Usage:
outlookSend(To=c("XXX@mail.com","YYY@mail.com"),
Subject="XXX", HTMLBodyFile='C:/Users/XXX/Documents/XXX.html',
Attachments=c('C:/Users/XXX/Documents/XXX.html', 'C:/Users/XXX/Documents/XXX.txt'))
当您被阻止直接通过SMTP服务器发送电子邮件时,此解决方案将非常有用。
答案 3 :(得分:0)
旧问题,但如果某人想使用mailR进行此任务,请查看“设置”(在Outlook中),然后单击“电子邮件”(在“您的应用程序设置”下方),然后导航至“ POP和IMAP”。您将在此处找到SMTP的主机名和端口。