2

我正在尝试在 R 中使用 sendemailR 包,但我收到一个错误,我不知道如何解决。

尝试默认参数时:

library(sendmailR)
from <- "your_email"
to <- "your_email"
subject <- "Test send email in R"
body <- "It works!"                     
mailControl=list(smtpServer="smtp.gmail.com")
sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)

我得到错误

Error in socketConnection(host = server, port = port, blocking = TRUE) : 
cannot open the connection
In addition: Warning message:
In socketConnection(host = server, port = port, blocking = TRUE) :
Gmail SMTP Server:25 cannot be opened

所以我将端口更改为 465,它似乎可以工作

library(sendmailR)
from <- "your_email"
to <- "your_email"
subject <- "Test send email in R"
body <- "It works!"                     
mailControl=list(smtpServer="smtp.gmail.com", smtpPort="465")
sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)

但后来我收到以下错误

Error in if (code == lcode) { : argument is of length zero

知道发生了什么吗?

这是 R 和 Windows 的版本

R version 3.0.3 (2014-03-06) -- "Warm Puppy"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

谢谢!

4

1 回答 1

4

您的示例中有两点需要注意:

正如@David Arenburg评论的那样,to应该包含一个有效的电子邮件地址。

第二件事是您正在使用的 smtp 服务器:smtp.gmail.com. 此服务器需要身份验证,而 sendmailR 不支持。

您可以使用不需要身份验证的 smtp 服务器(例如受限的 gmail smtp 服务器:aspmx.l.google.com,端口 25,请参阅此处了解详细信息)

另一种选择是使用mailR允许身份验证的包。

尝试类似的事情(当然你必须输入有效的电子邮件地址和 user.name 和 passwd 才能工作):

library(mailR)
sender <- "SENDER@gmail.com"
recipients <- c("RECIPIENT@gmail.com")
send.mail(from = sender,
to = recipients,
subject="Subject of the email",
body = "Body of the email",
smtp = list(host.name = "smtp.gmail.com", port = 465, 
        user.name="YOURUSERNAME@gmail.com", passwd="YOURPASSWORD", ssl=TRUE),
authenticate = TRUE,
send = TRUE)

希望能帮助到你,

亚历克斯

于 2014-05-21T21:37:08.493 回答