【问题标题】:cannot send email via outlook in ruby无法通过 ruby​​ 中的 Outlook 发送电子邮件
【发布时间】:2018-04-02 16:37:29
【问题描述】:

我有一个脚本,其中语法使用 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

【问题讨论】:

  • 请注意,从 Ruby 直接向 SMTP 服务器发送电子邮件可能比通过 Outlook 容易得多。
  • 在下面查看我的更新答案

标签: ruby email outlook ole


【解决方案1】:

我没有使用正确的电子邮件地址,我已修复它。它现在可以工作了。

【讨论】:

    【解决方案2】:

    确保您有一个有效的电子邮件地址,使用与您在 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")
    

    【讨论】:

      猜你喜欢
      • 2011-09-14
      • 2012-03-03
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 2022-07-20
      • 2020-09-19
      相关资源
      最近更新 更多