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

时间:2015-09-26 11:57:28

标签: c# json json.net mandrill

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

以下是我创建的C#课程:

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-

人们认为类结构应该映射到JSON附件?我使用JSON.Net来解析JSON。

非常感谢

1 个答案:

答案 0 :(得分:0)

您应该按如下方式定义InboundMsg课程:

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

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