Grails Mail plugin not working - grails

I am trying to send mails from a Grails application, but without any success.
I've used gmail and other smtp server (without ssl!) but the same error occurs:
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?. Failed messages: javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
I am using in Config.groovy (example for gmail):
grails.mail.host = "smtp.gmail.com"
grails.mail.from = "xxx#gmail.com"
grails.mail.port = "465"
grails.mail.ssl = "on"
grails.mail.username = "xxx#gmail.com"
grails.mail.password = "xxx"
grails.mail.props = ["mail.smtp.auth": "true",
"mail.smtp.socketFactory.port": "465",
"mail.smtp.socketFactory.class": "javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback": "false",
"mail.smtp.starttls.enable": "true",
"mail.debug": "true"]
EDIT: I made a simple app with just the mail plugin and a controller and the config posted by Javid Jamae works (3rd answer, also I think the other should work).
BUT even if I just copy-paste the same config and the same sending mail code, on my primary project it still gives me the same exception! I think this can be caused by Nimble plugin (Mail plugin was installed by it).
My configuration is:
Grails version: 1.3.4
Groovy version: 1.7.4
JVM version: 1.6.0_21
jquery - 1.4.2.5
mail - 0.9
shiro - 1.0.1
nimble - 0.4-SNAPSHOT
FINAL EDIT :
I resolved the issue: it seems that I have to use the same settings in the Nimble plugin also, in NimbleConfig.groovy -> mail { ... (must have "from = ...") } .
Stupid issue, but waisted a lot of time on it.

I'm not using SSL and I have the following defined at the bottom of my Config.groovy (not under the environments section):
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "xxx#gmail.com"
password = "xxx"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
}
I'm using:
app.grails.version=1.2.1
plugins.mail=0.9
This works for me.

You have enabled SSL:
grails.mail.ssl = "on"
And got exception
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?.
So disable SSL (my config):
host = "smtp.gmail.com"
port = 465
username = "username#gmail.com"
password = "password"
javaMailProperties = ['mail.smtp.auth': 'true',
'mail.smtp.socketFactory.port': '465',
'mail.smtp.socketFactory.class': 'javax.net.ssl.SSLSocketFactory',
'mail.smtp.socketFactory.fallback': 'false']
Anyway, if you want to enable SSL - try 587 port.
Also try to set
mail.smtp.starttls.required : 'true'
Because if server not supports secure connection or client doesn't accept server's certificate secure connection will not started and you will got your exception.
But after setting starttls.required = true and secure connection is impossible whole connection will fails so you got proper exception message.
P.S. Take a note that SSL and TLS - is different protocols.

I dont have this line in my configuration
"mail.smtp.starttls.enable": "true"
and my connection is working
also the port should not be in quotes
grails.mail.port = 465

I ran into a similar strain of this issue as well, so I'll share my experience. When using the Nimble plugin (which uses the Mail plugin as a dependency), Nimble defines its own mail settings in grails-app/conf/NimbleConfig.groovy.
The settings in NimbleConfig.groovy appear to overwrite those set in Config.groovy (presumably because NimbleConfig is executed after Config).
The solution here is conditional:
If you're using Nimble, set the mail properties in grails-app/conf/NimbleConfig.groovy; you do not need to set them in grails-app/conf/Config.groovy
If you're not using Nimble, then just follow the Mail plugin instructions for configuration (or use Javid Jamae's answer)
This seems to be what the OP alluded to in his/her edits, but I just thought I'd confirm the edits with my understanding of what's happening.
Update:
For a reference, here are the NimbleConfig.groovy settings that worked for me:
nimble {
...
messaging {
...
mail {
host = 'smtp.gmail.com'
port = 465
username = '...#gmail.com'
password = '...'
props = [
'mail.smtp.auth': 'true',
'mail.smtp.socketFactory.port': '465',
'mail.smtp.socketFactory.class': 'javax.net.ssl.SSLSocketFactory',
'mail.smtp.socketFactory.fallback': 'false'
]
}
}
}

You can verify your config settings at runtime by checking the
mailService.mailSender properties.Something like this:
mailService.mailSender.properties.each{println}
It will yield the host, port, username, password and a few other values.
If you're sure they are all correct, I would suspect a firewall issue.

Related

How to visualize remote neo4j auradb with neovis.js?

I have a remote AuraDB, but I'm unable to visualize with neovis.js
var config = {
container_id: "viz",
server_url: "bolt://<server_url>:7687",
server_user: <user>,
server_password: <pwd>,
initial_cypher: "match n=(:Person)-[:Knows]->(:Person) return n"
}
The above code segment is from the neovis GitHub documentation.
(https://github.com/neo4j-contrib/neovis.js/)
If I use the "bolt" protocol, the error is :
Neo4jError: WebSocket connection failure. Due to security constraints in your web browser, the reason for the failure is not available to this Neo4j Driver. Please use your browsers development console to determine the root cause of the failure. Common reasons include the database being unavailable, using the wrong connection URL or temporary network problems. If you have enabled encryption, ensure your browser is configured to trust the certificate Neo4j is configured to use. WebSocket readyState is: 3
If I use the "neo4j" protocol, then :
Neo4jError: Could not perform discovery. No routing servers available.
If I use the "neo4j+s" / "neo4j+ssc" protocols, then :
Encryption/trust can only be configured either through URL or config, not both
I have observed neovis plugin for my webapp works well with a local neo4j db and the bolt protocol.
Please help me out with some understanding in the case of visualizing a remote neo4j aura db.
Aura has strict encryption policies. And the driver config used for neovis.js seems half-baked. It doesn't seem to be enough for Aura and doesn't seem to allow turning encryption off completely either.
So the best option here is to enforce encryption in the config and use an unencrypted connection Scheme.
Working config for Aura by using an unencrypted connection URI and enforcing encryption in the config
var config = {
encrypted:"ENCRYPTION_ON",
trust: "TRUST_SYSTEM_CA_SIGNED_CERTIFICATES",
container_id: "viz",
server_url: "neo4j://<dbid>.databases.neo4j.io",
...
Updated Solution for Neovis 2.0.2, from this GitHub issue
var config = {
containerId: "viz",
neo4j: {
serverUrl: "neo4j://<dbid>.databases.neo4j.io",
serverUser: "neo4j",
serverPassword: "secret",
driverConfig: {
encrypted: "ENCRYPTION_ON",
trust: "TRUST_SYSTEM_CA_SIGNED_CERTIFICATES"
}
},

Graphstory and Neo4jphp

I have succesfully used neo4jphp library with graphenedb with this simple steps as per documentation (considering that graphenedb does not require https)
require('vendor/autoload.php'); // or your custom autoloader
// Connecting to a different port or host
$client = new Everyman\Neo4j\Client(url, port);
// Connecting using HTTP and Basic Auth
$client->getTransport()
->setAuth('username', 'password');
// Test connection to server
print_r($client->getServerInfo());
However, when trying to connect to a graphstory instance (of course they both work fine if I call the rest api from browser, the neo4j console works fine etc.) which requires https as follows
require('vendor/autoload.php'); // or your custom autoloader
// Connecting to a different port or host
$client = new Everyman\Neo4j\Client(url, port);
// Connecting using HTTPS and Basic Auth
$client->getTransport()
->useHttps()
->setAuth('username', 'password');
// Test connection to server
print_r($client->getServerInfo());
I get the following error. They should be identical, I can't get why.
Fatal error: Uncaught exception 'Everyman\Neo4j\Exception' with message 'Can't open connection to https://neo-54f500bf2cc7e-364459c455.do-stories.graphstory.com:7473/db/data/' in /Applications/XAMPP/xamppfiles/htdocs/graphene/vendor/everyman/neo4jphp/lib/Everyman/Neo4j/Transport/Curl.php:91
Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/graphene/vendor/everyman/neo4jphp/lib/Everyman/Neo4j/Transport.php(95): Everyman\Neo4j\Transport\Curl->makeRequest('GET', '/', NULL)
#1 /Applications/XAMPP/xamppfiles/htdocs/graphene/vendor/everyman/neo4jphp/lib/Everyman/Neo4j/Command.php(64): Everyman\Neo4j\Transport->get('/', NULL)
#2 /Applications/XAMPP/xamppfiles/htdocs/graphene/vendor/everyman/neo4jphp/lib/Everyman/Neo4j/Client.php(828): Everyman\Neo4j\Command->execute()
#3 /Applications/XAMPP/xamppfiles/htdocs/graphene/vendor/everyman/neo4jphp/lib/Everyman/Neo4j/Client.php(464): Everyman\Neo4j\Client->runCommand(Object(Everyman\Neo4j\Command\GetServerInfo))
#4 /Applications/XAMPP/xamppfiles/htdocs/graphene/story.php(20): Every in /Applications/XAMPP/xamppfiles/htdocs/graphene/vendor/everyman/neo4jphp/lib/Everyman/Neo4j/Transport/Curl.php on line 91
It seems to me that neo4jphp is not configuring the TLS part in the cURL request.
I fixed it by downloading the certificate bundle from http://curl.haxx.se/docs/caextract.html (ca_bundle.crt) and adding the following line to Everyman\Neo4j\Transport\Curl.php, function makeRequest:
$options[CURLOPT_CAINFO] = "your/path/to/ca-bundle.crt";
I've created an issue on GitHub for this: https://github.com/jadell/neo4jphp/issues/171
I'm the CTO/Lead Dev at Graph Story. Sorry to hear you're having troubles. I've actually just taken a look at your instance and things seem OK from the server side.
Without additional info it's hard to say if there's an issue with your sample connection code. Considering that you've used that same library to connect to GrapheneDB in the past, I think the chances an error in the sample code is low.
Based on the current state of your instance and on the exception thrown by Neo4jPHP, my guess is that port 7473 may be blocked on your network. You can confirm that with local tech support or by switching to a network where you know port 7473 is open and trying to connect again.

Grails:Groovy:SSLPeerUnverifiedException: peer not authenticated

I want to hit an xml request to a url while running the code in my local system it is working well i have created a war file and deployed the same in server,but while running in server getting an exception 'javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated'
i have used groovy http builder
def http = new HTTPBuilder(url)
http.auth.basic('username', 'password')
try {
http.request(Method.POST, ContentType.TEXT) {
req->
headers.accept = "application/xml"
body = request //xml request
response.success = {
resp,reader ->
Response = reader.text
}
}
}
catch(HttpResponseException ex) {
println ex;
}
how can i solve this problem in this case..?
Workaround: The following answer provides a workaround for this issue: https://stackoverflow.com/a/25076888/782034
Just found out that new version (0.7.1) of HttpBuilder introduces
method:
ignoreSSLIssues()
This solves all problems regarding invalid SSL certificates (of course
you have to be aware that it also decrease security).
More information about this method:
http://groovy.codehaus.org/modules/http-builder/doc/ssl.html (section
at the bottom)
i.e.
def http = new HTTPBuilder(url)
http.ignoreSSLIssues()
Solution: If you want to do things the 'proper' way, checkout the other solutions regarding importing the server's certificates. e.g. SSLPeerUnverifiedException: peer not authenticated
If you use ignoreSSLIssues() but still get same exception.
Just add latest httpclient into Gradle/Maven
compile 'org.apache.httpcomponents:httpclient:4.5.3'
Can't use ignoreSSLIssues in HttpBuilder version 0.7.1

Using grails rest plugin with proxy authentication?

I would like to use the following grails plugin to use Google shorten url API.
http://www.grails.org/plugin/rest
The problem is I need to use it with proxy authentication setting (with username and password)
The API gives me setProxy, but without username and password setting.
How can I set my proxy authentication detail here?
Let me know if there is another alternative. I can't use google-url-shortener plugin because I'm still using grails 1.2.0 which is not compatible with the plugin.
Thanks,
Robert
You could try setting the Proxy-Authorization header to authenticate to the proxy. Something similar to the following:
withRest(id: "urlshortener",
requestContentType: JSON,
body: [longUrl: "http://www.google.com/"]
proxy: [host: "myproxy.acme.com", port: 8080, scheme: "http"]) {
def response = post(path: '/urlshortener/v1/url') {
headers.'Proxy-Authorization' = 'Basic ' +
"${username}:${password}".toString().bytes.encodeBase64()
}
}

FTPS (TLS/SSL) from Ruby on Rails App

I have an FTP server which only accepts connections through running FTPS (explicit FTP over TLS). I need to be able to connect to this using a Ruby on Rails app.
Does anybody know of a method to do this? I have tried the Net::FTP library but this does not appear to support FTPS connections.
How about using Net::FTPTLS ?
Since Ruby 2.4, TLS over FTP has been available with Net::FTP... this has caused gems like double-bag-ftps to become archived and all your google searches to yield outdated answers.
If you can do explicit FTP over TLS (Connects to FTP normally, then issues a command AUTH TLS to switch to TLS Mode), then great... that should be able to use Ruby's Net::FTP out of the box by just passing {ssl: true} in the options.
Implicit FTP over TLS (runs over TLS from the get-go) does not work out of the box, however, and you must override Net::FTP's connection method to establish an SSL socket and then optionally send commands to the FTP server.
Inidka K posted a Github Gist, but since those are bad form (can go stale), I've posted my version that works against a ShareFile Implicit FTP setup (which seems to only support Implicit FTP):
require 'net/ftp'
class ImplicitFtp < Net::FTP
FTP_PORT = 990
def connect(host, port = FTP_PORT)
synchronize do
#host = host
#bare_sock = open_socket(host, port)
begin
ssl_sock = start_tls_session(Socket.tcp(host, port))
#sock = BufferedSSLSocket.new(ssl_sock, read_timeout: #read_timeout)
voidresp
if #private_data_connection
voidcmd("PBSZ 0")
voidcmd("PROT P")
end
rescue OpenSSL::SSL::SSLError, Net::OpenTimeout
#sock.close
raise
end
end
end
end
Then, in your code:
ftp_options = {
port: 990,
ssl: true,
debug_mode: true, # If you want to see what's going on
username: FTP_USER,
password: FTP_PASS
}
ftp = ImplicitFtp.open(FTP_HOST, ftp_options)
puts ftp.list
ftp.close
I done something like this with Implicit/Explicit FTPS, I used double-bag-ftps gem that I patched to support reuse of ssl session. It's a requirement for a lot of ftps servers.
I put the code on github here : https://github.com/alain75007/double-bag-ftps
EDIT: I figured out how to get it running locally, but am having issues getting it to work on Heroku. That's a bit of a departure from this question, so I've created a new one:
Heroku with FTPTLS - Error on SSL Connection
require 'net/ftptls'
ftp = Net::FTPTLS.new()
ftp.passive = true
#make sure you define port_number
ftp.connect('host.com', port_number)
ftp.login('Username', 'Password')
ftp.gettextfile('filename.ext', 'where/to/save/file.ext')
ftp.close
If you want to use Implicit FTPS, please try this gist.
For Explicit FTPs, you can use the ruby gem ftpfxp.
Support for implicit FTPS was merged into ruby/net-ftp in January 2022 (in this PR). If you want to make use of this straight away, you can include the latest version directly in your Gemfile:
gem "net-ftp", github: "ruby/net-ftp", branch: "master"
Then you just need:
options = {
ssl: true,
port: 990,
implicit_ftps: true,
username: "your-user",
password: "*********",
debug_mode: true
}
Net::FTP.open("yourhost.com", options) do |ftp|
ftp.list.map{ |f| puts f }
end
I implemented an ftps solution using double-bag-ftps
double-bag-ftps

Resources