📄 06 - reading mail with pop3.rb
字号:
require 'net/pop'conn = Net::POP3.new('mail.myhost.com')conn.start('username', 'password')#---require 'net/pop'conn = Net::POP3.new('mail.myhost.com')conn.start('username', 'password')conn.mails.each do |msg| File.open(msg.uidl, 'w') { |f| f.write msg.pop } # msg.deleteendconn.finish#---conn = Net::POP3.new('mail.myhost.com')conn.start('username', 'password')ids = conn.mails.collect {|msg| msg.uidl if msg.pop.match('jabba')}conn.finish# => ["UID2-1141260595", "UID3-1141260595"]#---conn2 = Net::POP3.new('mail.myhost.com')conn.start('username', 'password')conn.each_mail {|msg| puts msg.pop if msg.uidl=='UID3-1141260595'}conn.finish# Return-Path: <jabba@huttfoundation.org># X-Original-To: username@my.mailhost.com# Delivered-To: username@localhost# ...#---#!/usr/bin/env ruby# forward_important_messages.rbrequire 'net/pop'require 'net/smtp'$address = 'huttfoundation.org'$from = 'myhomeemail@my.mailhost.com'$to = '5555555555@mycellphoneprovider.com'smtp_server = 'my.mailhost.com'pop_server = 'my.mailhost.com'username = 'username'password = 'password'$found = Hash.newdef send_msg (text) count = 1 while(text.size > 0) do # SMS messages limited to 160 characters msg = text.slice!(0, 159) full_msg = "From: #{$from}\n" full_msg += "To: #{$to}\n" full_msg += "Subject: Found message from #{$address} (#{count})!\n" full_msg += "Date: #{Time.now}\n" full_msg += msg + "\n" Net::SMTP.start(smtp_server, 25) do |smtp| smtp.send_message full_msg, $from, $to end count += 1 endendloop do conn = Net::POP3.new(pop_server) conn.start('username', 'password') uidls = conn.mails.collect do |msg| msg.uidl if msg.pop.match(/#{$address}/) end uidls.each do |one_id| if ! $found.has_key? one_id $found[one_id] = true conn.each_mail do |msg| send_msg(msg.uidl) if msg.uidl==one_id end end end conn.finish # Sleep for 5 minutes. sleep (60*60*5)end#---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -