如何解决向 SMTP 服务器 (mailR+Outlook) 发送邮件的问题

How to resolve an issue with sending message to SMTP Server (mailR+Outlook)

我一直在设置mailR包来从我的Outlook帐户发送邮件,但到目前为止我一直无法顺利地工作,我得到的结果有时发送成功,有时失败。

代码如下(privacy/security用'aaa'和'bbb'代替邮箱和密码):

  send.mail(from = "aaa@domain.com",
            to = "bbb@domain.com",
            subject = subject,
            body = 'Test Successful',
            encoding = 'utf-8',
            html = TRUE,
            inline = TRUE,
            attach.files = filenamex,
            smtp = list(host.name = 'smtp.office365.com',
                        port = 587,
                        user.name="aaa@domain.com",
                        passwd= "abc",
                        tls = TRUE                       
            ),
            authenticate = TRUE,
            send = TRUE)

当我 运行 每 5 分钟安排一次代码时,我得到以下结果

2. send email at 2021-10-04 20:55:01 +07 failed !
1. send email at 2021-10-04 21:00:01 +07 successful !
2. send email at 2021-10-04 21:05:01 +07 failed !
2. send email at 2021-10-04 21:10:02 +07 failed !
2. send email at 2021-10-04 21:15:01 +07 failed !
1. send email at 2021-10-04 23:03:09 +07 successful !
2. send email at 2021-10-04 23:05:01 +07 failed !
1. send email at 2021-10-04 23:10:01 +07 successful !
2. send email at 2021-10-04 23:15:01 +07 failed !
1. send email at 2021-10-04 23:20:01 +07 successful !
1. send email at 2021-10-04 23:25:01 +07 successful !
1. send email at 2021-10-04 23:30:02 +07 successful !

知道出了什么问题吗?

RDCOMClient 示例:

install.packages("RDCOMClient", repos = "http://www.omegahat.net/R")
#or devtools::install_github("omegahat/RDCOMClient")

# Load the DCOM library
library (RDCOMClient)

# Open Outlook
Outlook <- COMCreate("Outlook.Application")

# Create a new message
Email = Outlook$CreateItem(0)

# Set the recipient, subject, and body
Email[["to"]] = "recipient1@test.com; recipient2@test.com; 
recipient3@test.com"
Email[["cc"]] = ""
Email[["bcc"]] = ""
Email[["subject"]] = "Quarterly Sales Analysis Updated"
Email[["body"]] = 
"The quarterly sales analysis has been updated.  
You can find it at: D:\Reports\Sales Analysis.xlsx"

# Send the message
Email$Send()

# Close Outlook, clear the message
rm(Outlook, Email)

查看更多there

以emayili为例

library(emayili)

#making an email
email <- envelope() %>%
from("user@sender.com") %>%
to(c("Recipient 1 <user1@recipient.com>", "Recipient 2 
<user@recipient.com>")) %>%
cc("cc@recipient.com") %>%
bcc("bcc@recipient.com") %>%
reply("reply-to@recipient.com") %>%
subject("Test email subject") %>%
body("Test email body")

#configuring the SMTP
smtp <- server(host = "smtp.mailtrap.io",
        port = 25,
        username = "********",
        password = "*********")

#sending    
smtp(email, verbose = TRUE)

查看更多there