2017-08-30 64 views
0

我一直在用mailR软件包来从我的Gmail帐户发送邮件,但到目前为止我一直无法使它工作。其代码如下(与隐私/安全“AAAA”取代电子邮件和密码):R mailR包发送消息给STMP服务器(gmail)

library(mailR) 

sender <- '[email protected]' 
recipients <- c('[email protected]') 
send.mail(from = sender, 
       to = recipients, 
       subject = 'My bot sent this', 
       body = 'Test Successful', 
       smtp = list(host.name = 'stmp.gmail.com', port= 587, 
           user.name = '[email protected]', 
           passwd = 'aaaa', tls= TRUE), 
       authenticate = TRUE, 
       send = TRUE) 

从我的理解,这似乎是一个适当的设置为这个包。此外,我已确保双重登录已关闭,并且已启用“允许访问不太安全的应用程序”。我也用tls和ssl(完成适当的端口)尝试了这一点,这没有什么区别。

当我运行代码,我得到了以下错误:

org.apache.commons.mail.EmailException: Sending the email to the following server failed : stmp.gmail.com:587 
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410) 
    at org.apache.commons.mail.Email.send(Email.java:1437) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at RJavaTools.invokeMethod(RJavaTools.java:386) 
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: stmp.gmail.com, 587; timeout 60000; 
    nested exception is: 
    java.net.UnknownHostException: stmp.gmail.com 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2053) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697) 
    at javax.mail.Service.connect(Service.java:386) 
    at javax.mail.Service.connect(Service.java:245) 
    at javax.mail.Service.connect(Service.java:194) 
    at javax.mail.Transport.send0(Transport.java:253) 
    at javax.mail.Transport.send(Transport.java:124) 
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400) 
    ... 6 more 
Caused by: java.net.UnknownHostException: stmp.gmail.com 
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source) 
    at java.net.PlainSocketImpl.connect(Unknown Source) 
    at java.net.SocksSocketImpl.connect(Unknown Source) 
    at java.net.Socket.connect(Unknown Source) 
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:310) 
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236) 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2019) 
    ... 13 more 
NULL 
Error: EmailException (Java): Sending the email to the following server failed : stmp.gmail.com:587 

为了记录在案,这里是回溯输出,以及:

> traceback() 
10: stop(list(message = "org.apache.commons.mail.EmailException: Sending the email to the following server failed : stmp.gmail.com:587", 
     call = .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", 
      cl, .jcast(if (inherits(o, "jobjRef") || inherits(o, 
       "jarrayRef")) o else cl, "java/lang/Object"), .jnew("java/lang/String", 
       method), j_p, j_pc, use.true.class = TRUE, evalString = simplify, 
      evalArray = FALSE), jobj = <S4 object of class "jobjRef">)) 
9: .Call(RJavaCheckExceptions, silent) 
8: .jcheck(silent = FALSE) 
7: .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, 
     .jcast(if (inherits(o, "jobjRef") || inherits(o, "jarrayRef")) o else cl, 
      "java/lang/Object"), .jnew("java/lang/String", method), 
     j_p, j_pc, use.true.class = TRUE, evalString = simplify, 
     evalArray = FALSE) 
6: .jrcall(x, name, ...) 
5: email$send() at MessageBot.R#15 
4: eval(expr, envir, enclos) 
3: eval(ei, envir) 
2: withVisible(eval(ei, envir)) 
1: source("MessageBot.R") 

任何想法是怎么回事?

回答

0

由于您要求使用mailr软件包提供故障排除解决方案,因此如果您打算尝试使用其他软件包,请使用gmailr

install.packages('gmailr') 
library(gmailr) 
setwd() #set to desired working directory 
gmail_auth() 

mail <- mime(
    To = "[email protected]", 
    From = "[email protected]", 
    Subject = "My bot sent this", 
    body = "Test successful") 
send_message(mail) 

当您使用gmail_auth功能,如果你想连接到您的Gmail帐户,将交互询问。请注意,使用通过公共应用程序连接的gmailR软件包。

通过在终端中输入'yes'并单击浏览器中的'接受',您将在当前工作目录(为什么我们将开始设置工作目录)中存储一个.httr-auth标记(加密用户名和密码)。您可以通过在终端中运行list.files(all = T)来查看文件。这允许您重新运行代码,而无需执行。)将物理代码中的密码存储在您的用户名中,并b)以交互方式“重新接受”连接。

+0

我会给一个镜头,但我对如何使这项工作在mailR答案仍然打开! –

0

您插入错误的主机名:host.name = 'stmp.gmail.com' 该协议是SMTP,所以host.name = 'smtp.gmail.com'