Sending and receiving Outlook email via Ruby

(disclaimer: this worked for my corporate Outlook web email account. I don’t know if it will work for all. I’m posting this just in case it helps somebody else.)

I recently had a need to detect changes in one application and send them to another application. Ideally, I was looking for “connector” or “glue” software between the two applications, but none existed. So, I needed to write my own.

The first application did not have any callback methods to notify when changes occurred, but it did have a mechanism that could send email whenever a change happened. The second application had an email receiver that could accept updates whenever it received plain-text, CSV formatted emails containing relevant fields. All I needed to do to write this “connector” software was setup a cron job that reads incoming mail from the first application, parse what changed, reformat it, and then send it to the other application.

Using Ruby, I found Mikel Lindsaar’s excellent mail library. His README.md page and github pages gave the following examples for MobileMe and GMail type email accounts:

# Sending via MobileMe
Mail.defaults do
  delivery_method :smtp, { :address              => 'smtp.me.com',
                           :port                 => 587,
                           :domain               => 'your.host.name',
                           :user_name            => '',
                           :password             => '',
                           :authentication       => 'plain',
                           :enable_starttls_auto => true  }
end

#Sending via GMail
Mail.defaults do
  delivery_method :smtp, { :address              => 'smtp.gmail.com',
                           :port                 => 587,
                           :domain               => 'your.host.name',
                           :user_name            => '',
                           :password             => '',
                           :authentication       => 'plain',
                           :enable_starttls_auto => true  }
end

That’s great. I was able to test the library using a GMail account, but my company was using Office 365 or Outlook.com. What were the correct parameters for that?? After a couple days of Googling, telnetting, and trial-and-error, I finally figured it out.

Here is what you need to do:

Step 1:
Log into your Outlook web account. Follow the 3 steps at this link to find the correct POP3 and SMTP addresses for your account: “Settings for POP and IMAP access

My address looked like “podxxxxx.outlook.com” where xxxxx is some magic Microsoft number.

Step 2:
Plug your address, username, and password into the Mail.defaults configuration:

Mail.defaults do
  delivery_method :smtp, { 
                           :address              => 'xxxxxxx.outlook.com',
                           :port                 => 587,
                           :domain               => 'example.com',
                           :user_name            => 'youremailaddress@example.com',
                           :password             => 'yourpassword',
                           :authentication       => :login,
                            :enable_starttls_auto => true  
                           }

  retriever_method :pop3, { :address    => 'xxxxxxx.outlook.com',
                          :port       => 995,
                          :user_name  => 'youremailaddress@example.com',
                          :password   => 'yourpassword',
                          :enable_ssl => true    }
end

That’s it! You’re then set to send and receive Outlook email. IMAP4 also works, if you prefer that. (And obviously, you shouldn’t hard-code your username and password, but this should be enough to make sure you can send & receive email.)

So beyond configuration, here is a basic example script showing how to send and receive Outlook email via Ruby.

Good luck!

require 'mail'  # ruby mail library. https://github.com/mikel/mail

# configure delivery and retrieval methods
Mail.defaults do
  delivery_method :smtp, { 
                           :address              => 'xxxxxxx.outlook.com',
                           :port                 => 587,
                           :domain               => 'example.com',
                           :user_name            => 'youremailaddress@example.com',
                           :password             => 'yourpassword',
                           :authentication       => :login,
                            :enable_starttls_auto => true  
                           }

  retriever_method :pop3, { :address    => 'xxxxxxx.outlook.com',
                          :port       => 995,
                          :user_name  => 'youremailaddress@example.com',
                          :password   => 'yourpassword',
                          :enable_ssl => true    }
end

# retrieve first 5 messages
emails = Mail.find(:what => :first, :count => 5)

# or retrieve and delete from server first 5 emails
#emails = Mail.find_and_delete(:what => :first, :count => 5)

puts "Number of emails retrieved: #{emails.length}"

# loop thru all emails and print content
emails.each do |email|

    # email fields defined at https://github.com/mikel/mail/tree/master/lib/mail/fields
    puts "from     : " + email.from.to_s       #=> 'fromname@example.com'
    puts "to       : " + email.to.to_s         #=> 'toname@example.com'
    puts "cc       : " + email.cc.to_s         #=> 'ccname@example.com'
    puts "bcc      : " + email.bcc.to_s        #=> 'bccname@example.com'    
    
    puts "subject  : " + email.subject         #=> "This is the subject"
    puts "date     : " + email.date.to_s       #=> '26 Nov 2013 10:00:00 -0800'
    puts "messageid: " + email.message_id      #=> '<ABCD1234.12345678@xxx.xxx>'
    puts "body     : " + email.body.decoded    #=> 'This is the body of the email...    

end

# send test message
Mail.deliver do

    from    'youremailaddress@example.com'
    to      'someaddress@example.com'
    subject 'test subject'
    body    'test body'

end
Software automation engineer in Portland, OR. LinkedIn: jantonypdx, Twitter: jantony_pdx
Posts created 10

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Christian Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
Christian
Guest

Thx John, for making this public. Exactly what I was looking for, and I was able to get it running myself within minutes!

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top