Backport Mail's encoding code from 2.5 for Ruby 1.9

Decoding messages in Ruby 1.9 was screwing up but not dying like 1.8. Backporting this fixes the problem.