2017-03-23 168 views
2

我正在使用以下代碼向我的Gmail帳戶發送電子郵件。 庫(mailR)發送電子郵件至R

sender <- "*********@gmail.com"  
recipients <- c("**********@gmail.com")  
send.mail(from = sender,  
      to = recipients,  
      subject = "Test",  
       body = "Test",  
      smtp = list(host.name = "smtp.gmail.com", port = 465,  
         user.name = "**********@gmail.com",    
         passwd = "*********", ssl = TRUE),  
      authenticate = TRUE,  
      send = TRUE) 

我曾嘗試使用不同的端口也很喜歡25,522但沒有任何工程。另外,我還可以訪問「不太安全的應用程序」。請幫助。

但我得到一個錯誤:

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465 
    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: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; 
    nested exception is: 
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2055) 
    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: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
    at sun.security.ssl.Alerts.getSSLException(Unknown Source) 
    at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source) 
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source) 
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source) 
    at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source) 
    at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source) 
    at sun.security.ssl.Handshaker.processLoop(Unknown Source) 
    at sun.security.ssl.Handshaker.process_record(Unknown Source) 
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) 
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) 
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) 
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) 
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:543) 
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:348) 
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:215) 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2019) 
    ... 13 more 
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
    at sun.security.validator.PKIXValidator.doBuild(Unknown Source) 
    at sun.security.validator.PKIXValidator.engineValidate(Unknown Source) 
    at sun.security.validator.ValiNULL 
dator.validate(Unknown Source) 
    at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source) 
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source) 
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source) 
    ... 25 more 
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
    at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source) 
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source) 
    at java.security.cert.CertPathBuilder.build(Unknown Source) 
    ... 31 more 
Error: EmailException (Java): Sending the email to the following server failed : smtp.gmail.com:465 

回答

1

因爲,沒有人真正幫助我在這個問題上,但我一直在嘗試和探索的東西,有一個鏈接/解決方案來解決我的問題。

  1. 發送電子郵件可以與sendmailR或mailR包做,但你需要有正確的SMTP設置(從IT),否則將無法正常工作,無論你多麼努力。無論如何,我發現關於mailR沒有爲我工作。

  2. 另一部分是建立一個API併爲您的gmail帳戶獲取客戶端ID,然後使用此文件建立連接併發送電子郵件。下面顯示了執行此操作的鏈接。

https://github.com/jennybc/send-email-with-r

希望這有助於大家誰遇到這個問題和我一樣。