C# to Ruby how to send an Email message

July 15, 2014

Why?

Many applications need to send emails. Signing up to a website? You get an email. Signing up to a new newsletter? You get an email. There are many other reasons and uses such as sending emails for alerts and so forth.

How?

Let’s compare the different ways to implement the sending of emails using .NET and specifically C# and Ruby

using System.Net;
using System.Net.Mail;

namespace MailSender
{
    class Program
    {
        static void Main(string[] args)
        {
            var message = new MailMessage(from: "someone@gmail.com", to: "someone2@gmail.com", subject: "This is from C#", body: "This email was sent using .NET");
            var smtp = new SmtpClient("smtp.gmail.com", 587);
            smtp.EnableSsl = true;
            smtp.Credentials = new NetworkCredential(userName: "someone@gmail.com", password: "a_password");
            smtp.Send(message);

        }
    }
}

We need 2 using statements, 1 to System.Net the other to System.Net.Mail. We instantiate a new MailMessage object with our content. We use the built-in Smtp class and set the host and port number. Since we are using Gmail to send this email, we need to ensure we have a secure connection and do so by setting the EnableSsl flag to true. We then go ahead and set our Credentials and call Send passing in our MailMessage object.

Now in Ruby

# http://ruby-doc.org/stdlib-2.0/libdoc/net/smtp/rdoc/Net/SMTP.html

require 'net/smtp'

from = someone@gmail.com
to   = someone2@gmail.com

message = <<EOM
From: Someone <#{from}>
To: Someone2 <#{to}>
Subject: This is from Ruby
Date: #{Time.now.strftime("%d/%m/%Y %H:%M")}

This email was sent using Ruby
EOM

smtp = Net::SMTP.new('smtp.gmail.com', 587)
smtp.enable_starttls

smtp.start(Socket.gethostname, '#{from}', 'a_password', :login) do |smtp|
  smtp.send_message(message, '#{from}', '#{to}')
end

Again I prefer to use built-in functionality, so there is a require to ‘net/smtp’ to pull in the classes we are going to need. The code is pretty straight forward with the exception of having to create the message format by hand including the From, To and Subject fields. The only thing I really got stumped by was how to enable the secure tls transportation which is accomplished by

smtp.enable_starttls

To figure that out I just had to read the SMTP docs which are pretty helpful.

Conclusion

Both approaches are pretty straightforward the only thing I really had a hard time with was enabling tls in the Ruby implementation but I was able quickly to sort that out.

Hopefully, this will help someone in the future and again even if that someone is me!


Discussion, links, and tweets

My name is Deon Heyns and I am a developer learning things and documenting them in realtime. Python, Ruby, Scala, .NET, and Groovy are all languages I have written code in. I appeared in the New York Post once. I host my code up at GitHub and Bitbucket so have a look at my code, fork it and send those pull requests.

comments powered by Disqus