简体   繁体   中英

MimeMessage.Load from Gmail API stream

We are using .NET Core 3.1 , MimeKit 2.11.0 and Google.Apis.Gmail.v1 1.51.0.2251 to fetch Gmail messages. We would like to retrieve the message from REST API as a stream and then pass the stream to MimeMessage.Load method.

We defined the following function for getting email message:

private Stream GetMessageDetails(string messageId, FormatEnum format)
{
    var req = gmailService.Users.Messages.Get("test@gmail.com", messageId);
    req.Format = format;
    return req.ExecuteAsStream();
}

And then we call it like this:

using (var messageStream = GetMessageDetails("MESSAGE_ID", FormatEnum.Raw))
{
    MimeMessage mimeMsg = MimeMessage.Load(messageStream);
}

When we run the code above, we get an exception:

Failed to parse message headers.

We tried with both FormatEnum.Raw and FormatEnum.Full , but we got the same exception. How can we load the message using MimeKit without loading the whole message into memory first? We are aware of the approach to base 64 decode message's Raw content, but it requires loading whole message into memory, which is not what we would like.

using (var rawStream = GetMessageDetails("MESSAGE_ID", FormatEnum.Raw))
{
    using (var filtered = new FilteredStream(rawStream))
    {
        filtered.Add(DecoderFilter.Create(ContentEncoding.Base64));

        MimeMessage mimeMsg = MimeMessage.Load(filtered);
    }
}

I have not figured out how to load this from a stream, because the stream return JSON and not just the raw base64 Mime.

With that said, you can take the Message that is returned and use the Raw property to load a MimeMessage. Please note that Gmail includes pesky underscores in their encoding. If someone can figure out the stream that would be great, but for now, my working solution is below. Please keep in mind it is not using streams and has been written to be descriptive and not optimized.

using (var gmail = await GetGmailService(providerInfo, refreshToken).ConfigureAwait(false))
{
    var request = gmail.Users.Messages.Get(emailAddress, GoogleId);
    request.QuotaUser = emailAddress;
    request.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Raw;

    var msg = await request.ExecuteAsync().ConfigureAwait(false);

    var base64Encoded = msg.Raw.Replace(",", "=").Replace("-", "+").Replace("_", "/");

    using (var memStream = new System.IO.MemoryStream(base64Encoded.ToBytes()))
    {
        var streamWriter = new System.IO.StreamWriter(memStream);
        streamWriter.Write(base64Encoded);
        streamWriter.Flush();

        memStream.Position = 0;

        using (var filtered = new MimeKit.IO.FilteredStream(memStream))
        {
            filtered.Add(MimeKit.IO.Filters.DecoderFilter.Create(ContentEncoding.Base64));

            MimeMessage mimeMsg = MimeMessage.Load(filtered);
            Console.WriteLine(mimeMsg.MessageId);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM