Mailkit: allow fetch from some bad imap server, which give envelope without the messageid field

Created on 9 Mar 2018  路  4Comments  路  Source: jstedfast/MailKit

Some imap server response envelop wrong when the mail has no messageid, for example:

("Mon, 30 Oct 2017 11:05:21 +0800" "=?GBK?B?u7bTrcq508PM2tG2xvPStdPKz+Q=?=" (("=?GBK?B?zNrRtsbz0rXTys/k?=" NIL "10000" "qq.com")) (("=?GBK?B?zNrRtsbz0rXTys/k?=" NIL "10000" "qq.com")) (("=?GBK?B?zNrRtsbz0rXTys/k?=" NIL "10000" "qq.com")) NIL NIL NIL NIL)

which should be

("Mon, 30 Oct 2017 11:05:21 +0800" "=?GBK?B?u7bTrcq508PM2tG2xvPStdPKz+Q=?=" (("=?GBK?B?zNrRtsbz0rXTys/k?=" NIL "10000" "qq.com")) (("=?GBK?B?zNrRtsbz0rXTys/k?=" NIL "10000" "qq.com")) (("=?GBK?B?zNrRtsbz0rXTys/k?=" NIL "10000" "qq.com")) NIL NIL NIL NIL NIL)

Because these server just exists, MailKit users may just encounter one, and these will cause fetch operation fail. I think we can make a little change to allow fetch from thes bad servers.

Code in Envelope.cs line 512:

if (!TryParse (text, ref index, out messageid))
    return false;

We can change to

//allow parse response from some bad server which have no messageid for some message
var oldIndex = index;
if (!TryParse(text, ref index, out messageid))
{
    index = oldIndex;
}

And I make a new test for this:

[Test]
public void TestSerialization2()
{

    var text =
        "('Mon, 30 Oct 2017 11:05:21 +0800' '=?GBK?B?u7bTrcq508PM2tG2xvPStdPKz+Q=?=' (('=?GBK?B?zNrRtsbz0rXTys/k?=' NIL '10000' 'qq.com')) (('=?GBK?B?zNrRtsbz0rXTys/k?=' NIL '10000' 'qq.com')) (('=?GBK?B?zNrRtsbz0rXTys/k?=' NIL '10000' 'qq.com')) NIL NIL NIL NIL)";
    text = text.Replace("'", "\"");


    var result = Envelope.TryParse(text, out var envelope);

        Assert.IsTrue(result);
        Assert.IsNull(envelope.MessageId);
    var text2 = envelope.ToString();

    Assert.AreNotEqual(text, text2);
}

compatibility server-bug

All 4 comments

This is why we can't have nice things :-

Wow, your response is so fast, that's great!

MailKit 2.0.2 has been released with this work-around.

Thanks, I've upgraded, and it's work perfectly.

Was this page helpful?
0 / 5 - 0 ratings