Thursday, June 04, 2009

Ruby net/smtp Gmail

smtpでGmailのメールサーバを使って簡単にメールが送れる!
require "net/smtp"
require "rubygems"
require "tlsmail"


class Gmail
MESSAGE = <<-EndOfMail
From: Kyo Endo <postagie@gmail.com>
To: Kyo Endo <postagie@gmail.com>
Subject: hello, kyo in class

This is test mail.
join us!

http://192.168.1.3:1234/
EndOfMail

  def initialize
    @smtpserver = Net::SMTP.new('smtp.gmail.com', 587)
    @smtpserver.enable_tls(OpenSSL::SSL::VERIFY_NONE)
  end
  def send_mail
    @smtpserver.start('gmail.com', 'account', 'password', :login) do |smtp|
      smtp.send_message(MESSAGE, 'postagie@gmail.com', 'postagie@gmail.com')
    end
  end
end

Gmail.new.send_mail

No comments: