使用sendemailR的问题

时间:2014-05-21 19:28:41

标签: r email sendmailr

我正在尝试在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)

谢谢!

1 个答案:

答案 0 :(得分:4)

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

@David Arenburg所述,to应包含有效的电子邮件地址。

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

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

另一种选择是使用允许身份验证的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)

希望它有所帮助,

亚历