我有一个脚本,其中语法使用irb但不通过ruby程序。 该脚本在message1.Send行失败。但是,当我在irb。
下执行它时,此语法有效**P:/RubyAutoEmail.rb:23:in `method_missing': (in OLE method `Send': ) (WIN32OLERuntimeError)**
OLE error code:4096 in Microsoft Outlook
Outlook does not recognize one or more names.
HRESULT error code:0x80020009
Exception occurred.
from P:/RubyAutoEmail.rb:23:in `block in sendemail'
from P:/RubyAutoEmail.rb:12:in `each'
from P:/RubyAutoEmail.rb:12:in `sendemail'
from P:/RubyAutoEmail.rb:35:in `<main>'
例如。在这里你可以看到message1.Send有效
irb(main):002:0> require 'win32ole'
=> true
irb(main):003:0> outlook = WIN32OLE.new('Outlook.Application')
=> #<WIN32OLE:0x0000000327a8c8>
irb(main):004:0> mapi = outlook.GetNameSpace('MAPI')
=> #<WIN32OLE:0x000000030e53a0>
irb(main):005:0> drafts = mapi.GetDefaultFolder(16)
=> #<WIN32OLE:0x000000032a8778>
irb(main):006:0> drafts.Items.each do |message|
irb(main):007:1* if message.Subject =~ /test email/i
irb(main):008:2> message1 = outlook.CreateItem(0)
irb(main):009:2> message1.Subject
irb(main):010:2> message1.To='junkone2@dd.com'
irb(main):011:2> message1.Send
irb(main):012:2> end
irb(main):013:1> end
=> nil
但是,当我使用脚本编写它时,它会失败。
require 'win32ole'
def sendemail(outlook, foldernumber, templatename,nameplaceholder,actualname,emailid)
mapi = outlook.GetNameSpace('MAPI')
drafts = mapi.GetDefaultFolder(foldernumber)
# Iterate over messages in a folder:
drafts.Items.each do |message|
# Your code here...
if message.Subject =~ /test email/i
# if message.Subject.downcase.include?(templatename.downcase)
message1 = outlook.CreateItem(0)
message1.Subject=message.Subject
message1.Body=message.Body
message1.Body=message1.Body.gsub(nameplaceholder,actualname)
message1.To=emailid
message1.Save
message1.Send
end
end
end
outlook = WIN32OLE.new('Outlook.Application')
# constant numbers
#https://msdn.microsoft.com/en-us/library/office/aa219371(v=office.11).aspx
sendemail(outlook,16, "test email","<TBD>","Junkee","junkone97@gmail")
#https://stackoverflow.com/questions/12593166/whats-the-easiest-way-to-send-a-message-through-outlook-with-ruby
答案 0 :(得分:1)
我没有使用正确的电子邮件地址,我修复了它。现在可以使用。
答案 1 :(得分:0)
确保您拥有有效的电子邮件地址,使用您在IRB测试用例中使用的电子邮件地址。看起来这条线应该在循环之前移动:
outlook = WIN32OLE.new('Outlook.Application')
所以试试:
require 'win32ole'
def sendemail(outlook, foldernumber, templatename,nameplaceholder,actualname,emailid)
mapi = outlook.GetNameSpace('MAPI')
drafts = mapi.GetDefaultFolder(foldernumber)
outlook = WIN32OLE.new('Outlook.Application')
# Iterate over messages in a folder:
drafts.Items.each do |message|
# Your code here...
if message.Subject =~ /test email/i
message1 = outlook.CreateItem(0)
message1.Subject=message.Subject
message1.Body=message.Body
message1.Body=message1.Body.gsub(nameplaceholder,actualname)
message1.To=emailid
message1.Save
message1.Send
end
end
end
sendemail(outlook, 16, "test email","<TBD>","Junkee","junkone2@dd.com")