简体   繁体   English

Gmail API读取/解码消息C#

[英]Gmail api read/decode message c#

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? 我正在C#应用程序中使用新的gmail api,我想知道在使用get方法获取消息后如何读取消息正文? 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? 我可以从该消息的“原始”属性得到一个对象MAILMESSAGE(从我MAILMESSAGE使用创建一个原始 ,有没有办法将其转换回?),或者我需要使用“有效载荷”的财产?

This is my code: (the ListMessages and the GetMessage methods are from the API Reference on google's site) 这是我的代码:( ListMessages和GetMessage方法来自Google网站上的API参考)

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

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

Now what? 怎么办?

Please help. 请帮忙。

Thanks. 谢谢。

From the API, your Message (1) has a Payload property of type MessagePart (2) . 通过API,您的Message (1)具有类型为MessagePart (2)Payload属性。 MessagePart has a Body property of type MessagePartBody (3) which (finally) has a string Data property. MessagePart具有类型为MessagePartBody (3)Body属性,该属性(最终)具有字符串Data属性。

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. 从那里开始,如何使用它取决于您自己,尽管您必须知道该值中可能有HTML,也可能没有。 From the API reference, we also see this for the Parts property of the Payload: 从API参考中,我们还可以看到有效载荷的Parts属性:

For non- container MIME message part types, such as text/plain, this field is empty 对于非容器的MIME邮件部分类型,例如文本/纯文本,此字段为空

So you could make the assumption that if msg.Payload.Parts contains no elements then it is a plain-text message. 因此,您可以假设msg.Payload.Parts不包含任何元素,则它是纯文本消息。

The Gmail API is not super easy to use. Gmail API并非超级易于使用。 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. JSON的结构将非常不同,具体取决于消息的格式(如果有附件和发送客户端)。

This guide goes over exactly how to handle extracting the HTML and Plain text versions of the body. 指南详细介绍了如何处理提取正文的HTML和纯文本版本。

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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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