通过outlook在R中发送电子邮件

 不曾孤独_815 发布于 2022-12-12 15:53

有谁知道是否可以通过Outlook在R中发送电子邮件.sendmailR的所有示例都使用gmail服务器.我不能这样做.有任何想法吗?

谢谢!

3 个回答
  • 可以通过Outlook在R中发送电子邮件.sendmailR适用于Windows 7和Outlook 2010.我引用了http://cran.es.r-project.org/web/packages/sendmailR/sendmailR.pdf

    smtpServer = Outlook 2010的信息位于文件 - >帐户设置 - >帐户设置 - >双击您的帐户 - >"服务器"框中的文本

    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)
    

    2022-12-12 15:56 回答
  • 您还可以使用vbscript来公开Outlook的所有功能

    参考:MailItem成员(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服务器发送电子邮件时,此解决方案将非常有用.

    2022-12-12 15:56 回答
  • 您可以使用RDCOMClient包从R中访问COM对象.您可以轻松访问应用程序对象(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"
    

    2022-12-12 15:56 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有