C# to Ruby how to send an Email message
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.
Let’s compare the different ways to implement the sending of emails using .NET and specifically C# and Ruby
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
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
To figure that out I just had to read the SMTP docs which are pretty helpful.
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!