返回顶部

收藏

ruby发送、接收server-ex2007邮件

更多
#Ruby发送、接收server-ex2007邮件
require 'net/smtp'
require 'net/pop'

EXServer = 'server-ex2007.xxx.com.cn' #填写EX2007Server地址

def send_mail(addressees, subject, body, sender, password)
  begin
    if addressees.empty?
      return nil
    else
      addressees = addressees.respond_to?(:to_ary) ? addressees : addressees.to_a
      content = "Content-Type:Text/HTML\r\nDate:#{Time.now}\r\nFrom:#{sender}\r\nTo:#{addressees.join(';')}\r\nSubject:#{subject.to_s}\r\n\r\n#{body}"
      Net::SMTP.start(EXServer) do |i|
        i.auth_login(sender, password)
        i.sendmail(content, sender, addressees)
      end
    end
  rescue => ex
    puts "Error:#{ex}"
  end
end

def read_mail(login, password)
  begin
    Net::POP3.start(EXServer, 110, login, password) do |i|
      if i.mails.empty?
        return nil
      else
        i.each_mail do |m|
          yield(m) if block_given?
        end
      end
    end
  rescue => ex
    puts "Error:#{ex}"
  end
end

标签:ruby

收藏

0人收藏

支持

0

反对

0

发表评论