c# - Gmail api 读取/解码消息 C#

标签 c# gmail-api

我在 C# 应用程序中使用新的 gmail api,我想知道如何在使用 get 方法获取消息后读取消息正文?我可以从消息的“Raw”属性获取 MailMessage 对象(要从我使用的 mailMessage 创建 Raw this ,有没有办法将其转换回来?),或者我需要使用“Payload”属性?

这是我的代码:(ListMessages 和 GetMessage 方法来自 Google 网站上的 API 引用)

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

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

现在怎么办?

请帮忙。

谢谢。

最佳答案

通过 API,您的消息 (1)具有 MessagePart 类型的 Payload 属性(2) 。 MessagePart 具有 MessagePartBody 类型的 Body 属性 (3)它(最后)有一个字符串 Data 属性。

数据是消息的内容,因此(使用示例代码)要获取消息,您需要执行以下操作:

msg.Payload.Body.Data

从那里开始,如何使用它取决于您,尽管您必须知道该值中可能有也可能没有 HTML。从 API 引用中,我们还看到了 Payload 的 Parts 属性:

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

因此,您可以假设,如果 msg.Payload.Parts 不包含任何元素,则它是纯文本消息。

关于c# - Gmail api 读取/解码消息 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27956885/

相关文章:

python - 修改邮件标签 Python GMAIL API

java - GMail API 批量插入消息抛出 - java.io.IOException : insufficient data written

perl - 如何使用 Perl 脚本将 Gmail 中的邮件标记为 'read' 并使用 Mail::Webmail::Gmail 删除它们?

c# - JArray.Remove(JToken) 不删除

c# - 在 Entity Framework 中将多个外键设置为主键

c# - 关于继承IDisposable的细节?

python - 在没有 html 或原始消息的线程中提取消息

c# - 线程安全的StreamWriter C#怎么做呢? 2个

c# - ASP.Net MVC 删除带有 FK 约束的记录

Gmail API : send text and HTML in one mail