R mailR包发送消息到STMP服务器(gmail)的问题

时间:2017-08-30 23:09:20

标签: r email sendmailr

我一直在使用mailR软件包从我的Gmail帐户发送电子邮件,但到目前为止我一直无法使其正常运行。代码如下(电子邮件和密码被'aaaa'替换为隐私/安全性):

library(mailR)

sender <- 'aaaa@gmail.com'
recipients <- c('aaaa@gmail.com')
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 = 'aaaa@gmail.com',
                               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")

知道出了什么问题吗?

2 个答案:

答案 0 :(得分:0)

鉴于您已使用mailr软件包要求解决问题解决方案,但不是完整答案,但如果您打算尝试使用其他软件包,请使用gmailr

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

mail <- mime(
  To = "to_address@domain.com",
  From = "from_address@domain.com",
  Subject = "My bot sent this",
  body = "Test successful")
send_message(mail)

当您使用gmail_auth功能时,它会以交互方式询问您是否要连接到您的Gmail帐户。请注意使用通过公共应用程序连接的gmailR包。

输入“是”&#39;在终端中点击“接受”#39;在浏览器中,您在当前工作目录中存储.httr-auth令牌(加密的用户名和密码)(为什么我们在开始时设置工作目录)。您可以通过在终端中运行list.files(all = T)来查看该文件。这使您可以重新运行代码,而无需使用。)将您的用户名存储在物理代码中,并且b。)重新接受&#39;交互式连接。

答案 1 :(得分:0)

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