-->

What's the easiest way to send a message throu

2019-03-21 12:13发布

问题:

My work requires me to automate e-mail generation for certain tests. I've been looking around but havent been able to find a reasonable solution that can be implemented quickly. It needs to be in outlook and not some other mail server as we have some strange authentication rules in place, and we need the option of saving drafts instead of just sending the message.

Apparently win32ole can do this, but I can't find any reasonably simple examples.

回答1:

Assuming that the Outlook credentials are stored and you are set to autologin to Outlook, WIN32OLE does the trick quite nicely:

require 'win32ole'
outlook = WIN32OLE.new('Outlook.Application')
message = outlook.CreateItem(0)
message.Subject = "Hey look a subject!"
message.Body = "Yes this is dog"
message.Recipients.Add 'dog@dog.com'
message.Recipients.Add 'cat@dog.com'
message.Attachments.Add('C:\Path\To\File.txt')
#Want to save as a draft?
message.Save
#Want to send instead?
message.Send

This is in fact quite well documented in "Automating Outlook with Ruby: Saving Mail Messages To Files", as is automating the rest of windows with Ruby.

You may have an authorization issue, which, if it appears, can be solved using "Advanced Security for Outlook".



回答2:

If the Outlook account has web access (via outlook.com or office365.com) you can also use Mikel Lindsaar's Ruby email library. It works well for many different email providers that allow POP3, IMAP4, or SMTP connections.

I posted an entry with some sample code on sending and receiving Outlook email via Ruby that might help. Sorry, I can't comment on how to save drafts, though.