Rspec Testing SMTP Connection - ruby

Using RSpec, I would like to test the ability of my App to connect to my SMTP server without (necessarily) delivering any messages.
How can I open a connection (with login) to an SMTP email server and test/review the response received?

smtp = Net::SMTP.new 'mail.example.com', example_port
# smtp.enable_starttls_auto # uncomment this if starttls is needed
# smtp.enable_tls # uncomment this if ssl/tls is needed but starttls is not supported
smtp.start('mydomain.example.com') do
expect{ smtp.authenticate 'me#mydomain.example.com', 'mypassword', 'plain' }.to_not raise_error
end
Calling authenticate will raise a Net::SMTPAuthenticationError if the authentication fails.
Otherwise, it will return a Net::SMTP::Response, and calling status on the response will return "235".

Related

OpenSSL::SSL::SSLError while testing the net/smtp library

I am trying to send emails using the net/smtp library but I receive the following error message:
SSL_connect returned=1 errno=0 state=unknown state: unknown protocol (OpenSSL::SSL::SSLError)
The code I am using is an extract from Peter Cooper's Beginning Ruby book, modified to use SSL, as required by yahoo servers:
require 'net/smtp'
message = %q{
From: Private Person <me#privacy.net>
To: myself <username#yahoo.com>
Subject: SMTP e-mail test
This is a test e-mail message.
}
smtp = Net::SMTP.new('smtp.mail.yahoo.com', 587)
smtp.enable_ssl
smtp.start('example.org', 'username#yahoo.com', 'password', :login) do |s|
s.send_message message, 'me#privacy.net', 'username#yahoo.com'
end
I tested the above code with Ruby 2.3.1 and Ruby 2.5.1 but I keep receiving the same error message.
By using Net::SMTP#enable_ssl (which is an alias to Net::SMTP#enable_tls), you are instructing the Ruby SMTP client to open a TLS tunnel as the very first step after connecting to the server. This requires the server to also support this on the used server port.
With Yahoos SMTP servers, they support this mode on port 465. With many other providers, you will see that they only support opportunistic TLS on port 587.
What happens there is that the client first starts with plain text SMTP and then negotiates with the server whether they support to update the connection to a TLS tunnel by using the STARTTLS command.
With net/smtp in Ruby, you can use Net::SMTP#enable_starttls instead.
If you are not sure of the server supports STARTTLS and you want to gracefully fallbvack to plaintext transfer of your password and the email to the server, you can also use Net::SMTP#enable_starttls_auto.
Thus, with Yahoo, you have two options:
you can use Net::SMTP#enable_tls on port 465
or you can use Net::SMTP#enable_starttls on port 587

Connect to localhost failed using Mail::Sendmail module on Windows 10

I have the following code:
#!C:\Perl\bin\perl.exe -w
use strict;
use warnings;
use Mail::Sendmail;
sendmail(
From => 'xxxxxx#hotmail.com',
To => 'xxxxxx#hotmail.com',
Subject => 'test email',
Message => "body of the message",
);
I get the following error:
Connect to localhost failed (An attempt was made to access a socket in a way forbidden by its access permissions.) no (more) retries!
I'm not sure how to correct it, any idea? I'm using Windows 10.
Your local system isn't offering an SMTP service
You need to specify an SMTP server by specifying the host name in the smtp parameter of your call to sendmail

connect server over net::ssh

hy
i write this code in my script
def connect_sql
Net::SSH.start( #host, #user, :port=>22, :verbose => :debug ) do |ssh|
puts ssh.exec!("./root/scripts/MysqlCleanInstance.sh #{#uid}")
end
end
The probleme is for etablish a conenction between two server we use dns .The DNS information are in /etc/resolv.conf.
i dont know why when i try to connect to another server i get this error:
, [2014-01-08T17:55:34.905977 #28115] DEBUG -- tcpsocket[3f87c7e6b534]: received packet nr 5 type 51 len 44
D, [2014-01-08T17:55:34.906129 #28115] DEBUG -- net.ssh.authentication.session[3f87c7e5b2b0]: allowed methods: publickey,password
D, [2014-01-08T17:55:34.906309 #28115] DEBUG -- net.ssh.authentication.methods.keyboard_interactive[3f87c7e58948]: keyboard-interactive failed
E, [2014-01-08T17:55:34.906487 #28115] ERROR -- net.ssh.authentication.session[3f87c7e5b2b0]: all authorization methods failed (tried none, publickey, hostbased, password, keyboard-interactive)
/var/lib/gems/1.8/gems/net-ssh-2.7.0/lib/net/ssh.rb:215:in `start': Net::SSH::AuthenticationFailed (Net::SSH::AuthenticationFailed)
from clean_instance.rb:87:in `connect_sql'
Make sure your SSH keys are in place. It appears you are not able to authenticate over SSH, even after all authentication methods have been tried.
The reason you are unable to connect to another server is likely because you are not able to authenticate with that other server using a SSH key, which I assume you were able to do with the first server.

suddenly PossibleAuthenticationFailureError in amqp

I'm using the ruby amqp gem. I ran a AMQP.start event loop, but 'suddenlyit raised aPossibleAuthenticationFailureError` during the loop.
AMQP.start(amqp_config) do |connection|
channel = AMQP::Channel.new connection
channel.on_error do |channel, channel_close|
puts "Oops... a channel-level exception: code = #{channel_close.reply_code}, message = #{channel_close.reply_text}"
end
my_worker = MyWorker.new
my_worker.start
end
[amqp] Detected TCP connection failure
/home/raincole/.rvm/gems/ruby-1.9.3-p125/gems/amq-client-0.9.3/lib/amq/client/async/adapters/event_machine.rb:164:in `block in initialize': AMQP broker closed TCP connection before authentication succeeded: this usually means authentication failure due to misconfiguration. Settings are {:host=>"localhost", :port=>5672, :user=>"guest", :pass=>"guest", :vhost=>"/", :timeout=>nil, :logging=>false, :ssl=>false, :broker=>nil, :frame_max=>131072} (AMQP::PossibleAuthenticationFailureError)
The weird part is, my worker have received some messages before I got PossibleAuthenticationFailureError. It seems like that the configuration should be correct(and I checked it over and over again).
Are there other potential reasons for PossibleAuthenticationFailureError?
I recommend a 4 step approach to investigating this issue:
a) Eliminate the obvious - Are your credentials correct and is the user account alive and well (default = 'guest')? Are you connecting to the appropriate vhost (default = '/')?
$ rabbitmqctl list_users
Listing users ...
guest [administrator]
...done.
$ rabbitmqctl list_user_permissions guest
Listing permissions for user "guest" ...
/ .* .* .*
<your_vhost> .* .* .*
...done.
b) What do the rabbitmq connection logs say?
On a Mac OS installation of rabbitmq (using brew), the logs can be found in /usr/local/var/log/rabbitmq, but your log location could be elsewhere depending on OS and installation preferences.
You may see the following lines in the rabbit#localhost.log file. Not a lot of help...and so proceed to step (c). Otherwise, investigate as per what you see in the log.
=INFO REPORT==== 15-Feb-2013::00:42:21 ===
accepting AMQP connection <0.691.0> (127.0.0.1:53108 -> 127.0.0.1:5672)
=WARNING REPORT==== 15-Feb-2013::00:42:21 ===
closing AMQP connection <0.691.0> (127.0.0.1:53108 -> 127.0.0.1:5672):
connection_closed_abruptly
c) Is rabbitmq's listener (Erlang client) alive. Default port = 5672. Simplest way to check is to send a garbage message to that port and look for an 'AMQP' response:
$ telnet localhost 5672
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
asdasd
AMQP
Connection closed by foreign host.
(d) Is the event loop reactor closing prematurely, before the AMQP.connect (or AMQP.start) actions have had a chance to complete authentication?
EM.run
connection = AMQP.connect(:host => 'localhost', :vhost => '/') do
# your code here
end
EM.stop
end
With all 'your code' sitting in a callback, the EM.stop runs instantaneously after the AMQP.connect instruction. This gives no time for the connection to be suitably established.
What worked for me here was to add a timer and handle disconnects gracefully.
EM.run
connection = AMQP.connect(:host => 'localhost', :vhost => '/')
# your code here
end
graceful_exit = Proc.new {
connection.close { EM.stop }
}
EM.add_timer(3, graceful_exit)
end
The reason I put the EM.stop block in a Proc is so that I can reuse it for other graceful exits (say, when trapping 'TERM' and 'INT' signals)
Hope this helps.

Ruby IMAP login error exception

With my Ruby script:
imap = Net::IMAP.new('imap.gmail.com')
imap.login("some_email#host.com", password)
I get the following exception:
A connection attempt failed because
the connected party did not properly
respond after a period of time, or
established connection failed because
connected hos has failed to respond. -
connect(2)
What's wrong?
You need to connect using SSL on port 993.
Therefore your code should be this:
imap = Net::IMAP.new('imap.gmail.com', 993, true)
imap.login("user#host.com", "password")

Resources