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 = '[email protected]'  
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("北京奥运会!")

你可能感兴趣的:(C++,c,Yahoo,C#,Ruby)