win32ole操作outlook

通过win32ole我们可以操作outlook,例如平时我们所常用到的发送邮件和建立日程提醒等。这样的话,我们就可以用程序的功能而取代了实际上对outlook的操作。

1.发送邮件

#Send Email with Ruby
#Before you use ruby to send your email, please configure your outlook
require 'win32ole'
outlook = WIN32OLE('Outlook.Application')
message = outlook.CreateItem(0)
message.Subject = 'Subject line here'
message.Body = 'This is the body of your message.'
message.To = 'xiaofan2350@yahoo.com.cn'
message.Attachments.Add('c:\really。txt', 1)
message.Send


2.建立日程提醒

#Outlook Appointments with Ruby
#We can use ruby to add appointmengs to the outlook calender,then read on.
require 'win32ole'
require 'date'
class Outlook
OLFolderCalendar = 9
OLAppointmentItem = 1
OLFree = 0
OLTentative = 1
OLBusy = 2
SEC = 1
MIN = 60 * SEC
HOUR = MIN * 60

def initialize( busy_default = Outlook::OLBusy )
@ol = WIN32OLE.new( "Outlook.Application" )
@busy_default = busy_default
end

def appointment(opts)
opts[:busy_status] ||= @busy_default
ol_appt = @ol.CreateItem(OLAppointmentItem)
ol_appt.Start = opts[:start]
ol_appt.End = opts[:end]
ol_appt.Subject = opts[:subject]
ol_appt.Location = opts[:location]
ol_appt.Body = opts[:body]
ol_appt.BusyStatus = opts[:busy_status]
ol_appt.ReminderSet = opts[:set_reminder?]
ol_appt.ReminderMinutesBeforeStart = opts[:reminder_minutes]
ol_appt.Save
end

def self.add_appointment
outlook = Outlook.new
start_time = Time.local( 2008, 8, 8, 19, 0, 0 )
end_time = start_time + ( 120 * Outlook::MIN )
ol.add_appointment :start => start_time, #接受的是Timer对象
:end => end_time ,
:subject => 'Welcome to Beijing!', #主题
:location => "Beijing", #地点
:body => 'One World One Dream', #内容
:set_reminder? => true, #是否提醒
:reminder_minutes => 15 #提醒时间周期

end
end


由于在我的项目中程序字符集是UTF-8,所以发送邮件或设置提醒的时候要转换字符集(gbk)。否则的话,发送的中文是乱码。eg.
require 'iconv'
cov = Iconv.new( 'gbk', 'utf-8')
puts cov.iconv("北京奥运会!")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值