- #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
- #该片段来自于http://www.codesnippet.cn/detail/200520133456.html
来源: http://www.codesnippet.cn/detail/200520133456.html