0

I'm using mailR for sending emails from within R. This used to work just fine on other machines. That was before my Mac Mini. I can run install.packages(c("rJava", "mailR")) without any problems. However when sending, I run into errors that (based on what has been asked before) seem to be related to Java.

Here is my code:

library(rJava)
library(mailR)

smtp_list = list(host.name = "smtp.sendgrid.net",
                port = 587,
                user.name = "XXX",
                passwd = "XXX",
                ssl = T)

body_html_test <- "<html><meta charset='UTF-8'><p>Hello<p></html>"

send.mail(from = "some@adress.com",
                 to = "my@adress.com",
                 subject = "This is a test",
                 body = body_html_test,
                 encoding="utf-8",
                 html = TRUE,
                 smtp = smtp_list,
                 authenticate = TRUE,
                 send = TRUE)

NULL
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.sendgrid.net: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.sendgrid.net, 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:101)
at sun.security.ssl.TransportContext.kickstart(TransportContext.java:238)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:394)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:373)
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.sendgrid.net:465

At first I was inclined to believe that this has to do with my mailclient but based on what others have asked, this seems to be related to java. The fact that this script runs smoothly on other machines points into that direction as well.

OS version:

$ sw_vers
ProductName:    macOS
ProductVersion: 11.2
BuildVersion:   20D64

Java version:

$ java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)

R version:

$ R
R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin17.0 (64-bit)

R Java configuration:

$ R CMD javareconf
Java interpreter : /usr/bin/java
Java version     : 1.8.0_291
Java home path   : /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Java compiler    : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar

trying to compile and link a JNI program 
detected JNI cpp flags    : 
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -    DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c conftest.c -o conftest.o
conftest.c:1:10: fatal error: 'jni.h' file not found
#include <jni.h>
         ^~~~~~~
1 error generated.
make: *** [conftest.o] Error 1
Unable to compile a JNI program


JAVA_HOME        : /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Java library path: 
JNI cpp flags    : 
JNI linker flags : 
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.

Thanks in advance for help.

Thomas
  • 1,392
  • 3
  • 22
  • 38
  • You seem to be connecting to port 465 which [uses SSL](https://sendgrid.com/docs/for-developers/sending-email/getting-started-smtp/#send-an-smtp-email-using-telnet). Support for SSL 3.0 [was dropped](https://stackoverflow.com/questions/28236091/how-to-enable-ssl-3-in-java), so I'd recommend connecting to one of the ports that supports TLS instead (rather than enabling the insecure cipher suite). – Kayaman May 06 '21 at 14:13
  • Yes but the same code runs perfectly on another machine...? Additionally, the port is specified above to be ```port = 587```... – Thomas May 07 '21 at 14:59
  • Code that runs on one machine can fail on another, or even fail on the same machine on a different run. The stacktrace indicates it's connecting to port `465` in any case, which is why I assumed it's failing because it's trying to negotiate an unsupported cipher suite. Try using `tls = T` instead of `ssl = T` as seen [here](https://www.rdocumentation.org/packages/mailR/versions/0.4.1) (under the MS Exchange server). – Kayaman May 08 '21 at 14:33
  • Switching from ```ssl=TRUE``` to ```tls=TRUE``` results in the same error message but instead of failing to connect to port ```465``` (as in the original question) it now fails to connect to ```587```... – Thomas May 10 '21 at 07:45
  • Well, at least now you see that you weren't connecting to the port you thought you were. That's why reading error messages is worth a lot more than reading configuration values. Why don't you run the code [here](https://stackoverflow.com/a/36981532/2541560) to see which cipher suites are enabled. – Kayaman May 10 '21 at 16:54
  • Finally, @Kayaman Thank you for pointing me into the right direction. Enabling SSL 3.0 did work out for me. – Thomas May 11 '21 at 14:06
  • I'm more surprised about the TLS not working. SSL 3.0 being disabled is normal, but why didn't you have a suitable TLS cipher suite? There should be v1.2 available. Enabling an insecure cipher suite might not be the best course of action. – Kayaman May 11 '21 at 19:07

0 Answers0