rpremraj / mailR

A utility to send emails from the R programming environment
http://rpremraj.github.io/mailR/
190 stars 57 forks source link

send.mail inside mclapply unable to connect? #10

Closed jmrichardson closed 9 years ago

jmrichardson commented 9 years ago

Hi,

For some reason send.mail does not work within mclappy. For example, this will fail:

mclapply(1:2,function(i){ send.mail(from = "me@me.com", to = "me@me.com", subject = "Test", body = "Body", html = TRUE, inline = TRUE, smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "me", passwd = "xxxx", ssl = TRUE), authenticate = TRUE, send = TRUE) })

It fails with this error:

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(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at RJavaTools.invokeMethod(RJavaTools.java:386) Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;

However, this will work inside mcapply (notice only 1 worker): mclapply(1,function(i){ send.mail(from = "me@me.com", to = "me@me.com", subject = "Test", body = "Body", html = TRUE, inline = TRUE, smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "me", passwd = "xxxx", ssl = TRUE), authenticate = TRUE, send = TRUE) })

The above works fine.

I need to use mclapply to run sending the email in parallel workers. I am not sure if this is a mailR issue or something to do with mclapply. But was hoping you could take a look...

Thanks, John

jmrichardson commented 9 years ago

Just in case, here is the full error message:

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(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) 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.SSLException: Server key at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1986) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:656) at javax.mail.Service.connect(Service.java:367) at javax.mail.Service.connect(Service.java:226) at javax.mail.Service.connect(Service.java:175) 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.SSLException: Server key at sun.security.ssl.Handshaker.throwSSLException(Handshaker.java:1212) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:279) at sun.security.ssl.Handshaker.processLoop(Handshaker.java:878) at sun.security.ssl.Handshaker.process_record(Handshaker.java:814) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323) at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:532) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:337) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:208) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950) ... 13 more Caused by: java.security.spec.InvalidKeySpecException: Could not create EC public key at sun.security.pkcs11.P11ECKeyFactory.engineGeneratePublic(P11ECKeyFactory.java:169) at java.security.KeyFactory.generatePublic(KeyFactory.java:334) at sun.security.ssl.HandshakeMessage$ECDH_ServerKeyExchange.(HandshakeMessage.java:1057) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:274) ... 23 more Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_DEVICE_ERROR at sun.security.pkcs11.wrapper.PKCS11.C_CreateObject(Native Method) at sun.security.pkcs11.P11ECKeyFactory.generatePublic(P11ECKeyFactory.java:233) at sun.security.pkcs11.P11ECKeyFactory.engineGeneratePublic(P11ECKeyFactory.java:164) ... 26 more 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(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) 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.SSLException: Server key at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1986) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:656) at javax.mail.Service.connect(Service.java:367) at javax.mail.Service.connect(Service.java:226) at javax.mail.Service.connect(Service.java:175) 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.SSLException: Server key at sun.security.ssl.Handshaker.throwSSLException(Handshaker.java:1212) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:279) at sun.security.ssl.Handshaker.processLoop(Handshaker.java:878) at sun.security.ssl.Handshaker.process_record(Handshaker.java:814) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323) at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:532) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:337) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:208) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950) ... 13 more Caused by: java.security.spec.InvalidKeySpecException: Could not create EC public key at sun.security.pkcs11.P11ECKeyFactory.engineGeneratePublic(P11ECKeyFactory.java:169) at java.security.KeyFactory.generatePublic(KeyFactory.java:334) at sun.security.ssl.HandshakeMessage$ECDH_ServerKeyExchange.(HandshakeMessage.java:1057) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:274) ... 23 more Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_DEVICE_ERROR at sun.security.pkcs11.wrapper.PKCS11.C_CreateObject(Native Method) at sun.security.pkcs11.P11ECKeyFactory.generatePublic(P11ECKeyFactory.java:233) at sun.security.pkcs11.P11ECKeyFactory.engineGeneratePublic(P11ECKeyFactory.java:164) ... 26 more Warning message: In mclapply(1:2, function(i, fun) { : all scheduled cores encountered errors in user code

jmrichardson commented 9 years ago

Finally figured it out. Simple fix is to not load mailR in the master but individually in the mclapply function:

mclapply(1:2,function(i){

Load mailr here

library(mailR)

send.mail(from = "me@me.com", to = "me@me.com", subject = "Test", body = "Body", html = TRUE, inline = TRUE, smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "me", passwd = "xxxx", ssl = TRUE), authenticate = TRUE, send = TRUE) })

rpremraj commented 9 years ago

Strange huh! Glad it worked... I will investigate this further for one of the future releases of mailR.