rpremraj / mailR

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

MailR suddenly no longer works on linux system #100

Closed ccampb17 closed 3 years ago

ccampb17 commented 3 years ago

Hello,

I've been using mailR for a long time with no issues - however, last week it suddenly stopped working and I cannot work out why. It uses a gmail address, so I have enabled the 'less secure apps' access (again it was working great until last week).

I've tried combinations of switching ports, TLS/SSL on and off, nothing seems to work.

I can still use the code to send mails on a windows 10 machine.

Any help is much appreciated as mailR is much better than the alternatives... when it was working at least.

For reference, I provide the error message, code used to generate it, and my sessionInfo in that order below. Please let me know if any other info would be useful.

The (verbose) error message is as follows:

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:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    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: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    at sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:171)
    at sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:98)
    at sun.security.ssl.TransportContext.kickstart(TransportContext.java:220)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:428)
    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
Error: EmailException (Java): Sending the email to the following server failed : smtp.gmail.com:465

Here is the code used to send the mail

send.mail(from = "username@gmail.com",
          to = c("person1@place.com", "person2@place.kp"),
          subject = subject.text,
          body = body.text,
          html=F,
          smtp = list(host.name = "smtp.gmail.com",
                      port=465,
                      user.name="username@gmail.com",
                      passwd="password123",
                      ssl=T),
          authenticate = T)

and the sessioninfo:

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
 [1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C                  LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8        LC_MONETARY=en_US.UTF-8      
 [6] LC_MESSAGES=en_US.UTF-8       LC_PAPER=en_US.UTF-8          LC_NAME=en_US.UTF-8           LC_ADDRESS=en_US.UTF-8        LC_TELEPHONE=en_US.UTF-8     
[11] LC_MEASUREMENT=en_US.UTF-8    LC_IDENTIFICATION=en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] rJava_1.0-4           tabulizer_0.2.2       xlsx_0.6.5            mailR_0.4.1           IMFData_0.2.0         flipTime_2.9.0        htmltab_0.8.1        
#truncated
ghost commented 3 years ago

We're currently experiencing the same issue which seems to have popped up out of nowhere. I think the only real difference is that we're attempting to send mail from an office365 smtp site rather than a gmail one.

Very interested in what could have happened here?

jrausch12 commented 3 years ago

+1

jrausch12 commented 3 years ago

@rpremraj Please advise

dseynaev commented 3 years ago

I had the same issue. For me this was related to the deprecation of TLS v1 and TLSv1.1 in java 1.8 8u291

see https://www.oracle.com/java/technologies/javase/8u291-relnotes.html

You can re-enable them by removing them from jdk.tls.disabledAlgorithms in the java security file (for me this is /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security/java.security )

but the (better, from a security POV at least) approach I ended up using was forcing apache commons mail (which mailR is a wrapper around) to use TLSv1.2 by setting a java system property:

rJava::.jinit()
rJava::.jcall("java/lang/System","S","setProperty", "mail.smtp.ssl.protocols", "TLSv1.2")
mailR::send.mail(...)

I'm using STARTTLS and you might need something slightly different if you are using TLS wrapper

GerZ-ad-Din commented 3 years ago

Thanks, @dseynaev this realy helps

jrausch12 commented 3 years ago

@dseynaev telling java to use 1.2 via an rJava call worked like a charm. Thanks buddy!

ccampb17 commented 3 years ago

@dseynaev thank you - this worked like a charm! I had a feeling some java had gone weird somewhere.

Now I can switch back to mailR instead of the intricate and unreliable workaround I made last week :)

Thanks again!

ghost commented 3 years ago

I know that this issue has been fixed, but through this I realized that mailR doesn't seem to be actively maintained anymore. My team switched over to Blastula, https://github.com/rstudio/blastula, and we're very happy with the change. It's maintained by RStudio themselves, and doesn't rely on Java to send SMTP messages, so less system configuration is needed to get it to work correctly.

ccampb17 commented 3 years ago

@mmccarthy-bnt thank you for this recommendation! Blastula looks really great - I have added switching over to it to my to do list.

Weirdly though, I have one particular case where I need something that can send non-HTML emails. Since mailR plus the solution from @dseynaev does this very well, I guess I will be still using it for the time being.