将 Mandrill JSON webhooks 入站电子邮件附件映射到 C# 类

Mapping Mandrill JSON webhooks inbound email attachments to C# classes

我可以处理来自 Mandrill 的 JSON webhook 入站电子邮件,但不能处理 JSON 邮件的附件部分。

这是我创建的 C# classes:

public class MandrillInbound
{
    public string @event { get; set; }
    public int ts { get; set; }
    public InboundMsg msg { get; set; }
}

public class InboundMsg
{
    public string raw_msg { get; set; }
    //public Headers headers { get; set; }
    public string text { get; set; }
    public bool text_flowed { get; set; }
    public string html { get; set; }
    public List<Attachment> attachments { get; set; }
    public string from_email { get; set; }
    public string from_name { get; set; }
    public List<List<string>> to { get; set; }
    public string subject { get; set; }
    //public Spf spf { get; set; }
    //public SpamReport spam_report { get; set; }
    //public Dkim dkim { get; set; }
    public string email { get; set; }
    public List<object> tags { get; set; }
    public object sender { get; set; }
    public object template { get; set; }
}

public class Attachment
{
    public string name { get; set; }
    public string type { get; set; }
    public string content { get; set; }
    public bool base64 { get; set; }
}

问题似乎出在 Mandrill 附件的 JSON 格式上,如下所示:

"attachments":{
"Attachment1.txt":{"name":"Attachment1.txt","type":"text\/plain","content":"Test attachment 1","base64":false},
"Attachment2.txt":{"name":"Attachment2.txt","type":"text\/plain","content":"Test attachment 2","base64":false}
}

我已经联系了 Mandrill,他们提供了很大帮助,但作为 PHP 机构,他们还不能提供解决方案。 Mandrill 入站电子邮件 API 的规范在这里: https://mandrill.zendesk.com/hc/en-us/articles/205583207-What-is-the-format-of-inbound-email-webhooks-

人们认为 class 结构应该是什么来映射到 JSON 附件?我使用 JSON.Net 来解析 JSON.

非常感谢

蒂姆

您应该按如下方式定义您的 InboundMsg class:

public class InboundMsg
{
    // Other properties as before
    Dictionary<string, Attachment> attachments { get; set; }
}

Json.NETcan deserialize a JSON object to a .Net generic dictionary, making the property names the dictionary keys and the property values the dictionary values,这就是你想要做的。