2
votes

I'm using the new gmail api in c# application, and I want to know how I can read the body of a message after getting the message with the get method? Can i get a MailMessage object from the "Raw" property of the message (to create a Raw from a mailMessage i use this, is there a way to convert it back?), or i need to use the "Payload" property?

This is my code: (the ListMessages and the GetMessage methods are from the API Reference on google's site)

List<Message> msgList = ListMessages(gs, "me", "is:unread");

string id = msgList[0].Id;
Message msg = GetMessage(gs, "me", id);

Now what?

Please help.

Thanks.

2

2 Answers

1
votes

From the API, your Message (1) has a Payload property of type MessagePart (2). MessagePart has a Body property of type MessagePartBody (3) which (finally) has a string Data property.

Data is the content of the message, so (using your example code) to get the message you would do something like:

msg.Payload.Body.Data

From there, how you use it is up to you, although you have to be aware that there may or may not be HTML in that value. From the API reference, we also see this for the Parts property of the Payload:

For non- container MIME message part types, such as text/plain, this field is empty

So you could make the assumption that if msg.Payload.Parts contains no elements then it is a plain-text message.

1
votes

The Gmail API is not super easy to use. They really leave a lot to the user to just figure out.

You're going to need to use recursion to get the correct structure and do some decoding of the message. The structure of the JSON is going to be very different depending on the format of the message, if there are attachments and the sending client.

This guide goes over exactly how to handle extracting the HTML and Plain text versions of the body.

Here part of the code from the guide that shows how to extract the body parts:

public static void ExtractMessagePart(MessagePart part, ref EmailMessageModel message)
{
    if (part == null)
        return;

    var contentDisposition = part.Headers?.FirstOrDefault(h => h.Name == "Content-Disposition");
    if (contentDisposition != null && (contentDisposition.Value.StartsWith("attachment") || contentDisposition.Value == "inline"))
    {
        message.Attachments.Add(new DragnetTech.EventProcessors.Email.EmailMessageModel.Attachment
        {
            AttachmentId = part.Body.AttachmentId,
            Filename = part.Filename,
            ContentID = contentDisposition.Value.StartsWith("inline") || part.Headers?.FirstOrDefault(h => h.Name == "Content-ID") != null ? Utils.UnescapeUnicodeCharacters(part.Headers.FirstOrDefault(h => h.Name == "Content-ID")?.Value) : null,
            Size = part.Body.Size ?? 0,
            ExchangeID = part.Body.AttachmentId,
            Data = part.Body.Data,
            ContentType = part.Headers?.FirstOrDefault(h => h.Name == "Content-Type")?.Value
        });
    }
    else
    {
        if (part.MimeType == "text/plain")
        {
            message.Body = DecodeSection(part.Headers?.FirstOrDefault(h => h.Name == "Content-Transfer-Encoding")?.Value, part.Body?.Data);
            message.IsHtml = false;
        }
        else if (part.MimeType == "text/html")
        {
            message.Body = DecodeSection(part.Headers?.FirstOrDefault(h => h.Name == "Content-Transfer-Encoding")?.Value, part.Body?.Data);
            message.IsHtml = true;
        }
    }


    if (part.Parts != null)
    {
        foreach (var np in part.Parts)
        {
            ExtractMessagePart(np, ref message);
        }
    }
}