View Problem
Send an email
Use library functions, classes or objects to create a short email addressed to your own email address. The subject should be,
Submit a new solution for ruby, erlang, csharp, or fsharp
There are 9 other solutions in additional languages (fantom, groovy, java, ocaml ...)
"Greetings from langref.org", and the user should be prompted for the message body, and whether to cancel or proceed with sending the email.
ruby
require 'net/smtp'
require 'webrick/utils'
body = <<END_OF_MESSAGE
This is a message from langref.org
END_OF_MESSAGE
SMTPSRV = 'smtp.somewhere.com'
USER = 'me' ; DOMAIN = 'somewhere.com' ; FROM = USER + '@' + DOMAIN
TO = 'dest@somewhereelse.com'
SUBJECT = 'Greetings from langref.org'
DATE = Time.now.rfc2822()
MSGID = '<' + WEBrick::Utils.random_string(21) + '.' + WEBrick::Utils.random_string(21) + '@' + SMTPSRV + '>'
Net::SMTP.start(SMTPSRV, 25) do |smtp|
smtp.open_message_stream(FROM, [TO]) do |msg|
msg.puts "From: #{FROM}"
msg.puts "To: #{TO}"
msg.puts "Subject: #{SUBJECT}"
msg.puts "Date: #{DATE}"
msg.puts "Message-ID: #{MSGID}"
msg.puts
msg.puts body
end
end
require 'webrick/utils'
body = <<END_OF_MESSAGE
This is a message from langref.org
END_OF_MESSAGE
SMTPSRV = 'smtp.somewhere.com'
USER = 'me' ; DOMAIN = 'somewhere.com' ; FROM = USER + '@' + DOMAIN
TO = 'dest@somewhereelse.com'
SUBJECT = 'Greetings from langref.org'
DATE = Time.now.rfc2822()
MSGID = '<' + WEBrick::Utils.random_string(21) + '.' + WEBrick::Utils.random_string(21) + '@' + SMTPSRV + '>'
Net::SMTP.start(SMTPSRV, 25) do |smtp|
smtp.open_message_stream(FROM, [TO]) do |msg|
msg.puts "From: #{FROM}"
msg.puts "To: #{TO}"
msg.puts "Subject: #{SUBJECT}"
msg.puts "Date: #{DATE}"
msg.puts "Message-ID: #{MSGID}"
msg.puts
msg.puts body
end
end
fsharp
open System
open System.Net
open System.Net.Mail
open System.Net.Mime
let subject = "Greetings from langref.org"
let from = "username@gmail.com"
let destination = "username@gmail.com"
printfn "Write mail body (press 'Enter' when finished):"
let mutable body = Console.ReadLine()
if body = null || body.Length = 0 then
body <- "Hello World"
let mail = new MailMessage(from, destination, subject, body)
let smtpHost = "smtp.gmail.com"
let clientCredentials = new NetworkCredential("username@gmail.com", "password")
let smtpClient = new SmtpClient(smtpHost,587)
smtpClient.EnableSsl <- true
smtpClient.UseDefaultCredentials <- false
smtpClient.Credentials <- clientCredentials
printfn "Send email? y/n"
match Console.ReadLine() with
| "y" -> smtpClient.Send(mail);mail.Dispose();printfn "email delivered"
| "n" -> smtpClient.Dispose()
| _ -> smtpClient.Dispose()
open System.Net
open System.Net.Mail
open System.Net.Mime
let subject = "Greetings from langref.org"
let from = "username@gmail.com"
let destination = "username@gmail.com"
printfn "Write mail body (press 'Enter' when finished):"
let mutable body = Console.ReadLine()
if body = null || body.Length = 0 then
body <- "Hello World"
let mail = new MailMessage(from, destination, subject, body)
let smtpHost = "smtp.gmail.com"
let clientCredentials = new NetworkCredential("username@gmail.com", "password")
let smtpClient = new SmtpClient(smtpHost,587)
smtpClient.EnableSsl <- true
smtpClient.UseDefaultCredentials <- false
smtpClient.Credentials <- clientCredentials
printfn "Send email? y/n"
match Console.ReadLine() with
| "y" -> smtpClient.Send(mail);mail.Dispose();printfn "email delivered"
| "n" -> smtpClient.Dispose()
| _ -> smtpClient.Dispose()
Submit a new solution for ruby, erlang, csharp, or fsharp
There are 9 other solutions in additional languages (fantom, groovy, java, ocaml ...)


