Backport newer Mail code to fix decoding problems. #850