邮戳入站电子邮件和ASP.Net序列化

时间:2013-02-22 05:54:46

标签: json asp.net-mvc-4 postmark

我正在使用Postmark处理出站电子邮件,但最近我尝试将其用于入站邮件。邮戳以JSON字符串的形式向定义的端点发送POST请求。

我有一个端点设置和一个表示JSON对象的对象模型。在本地测试中,我可以使用jquery将Postmark JSON消息发布到我的端点,只有在JSON消息上使用JSON.stringify时,它才会保留我的对象模型。如果我不使用stringify,则任何子对象(集合)都具有null属性。

如何在ASP.Net MVC 4中获取Web请求以在不使用JSON.stringify的情况下水合我的对象模型?以下代码:

对象模型(如果我JSON.stringify下面的JSON,则工作正常):

public class Email
{
    public string Cc { get; set; }
    public string From { get; set; }
    public string HtmlBody { get; set; }
    public string MailBoxHash { get; set; }
    public string MessageID { get; set; }
    public string ReplyTo { get; set; }
    public string Subject { get; set; }
    public string Tag { get; set; }
    public string TextBody { get; set; }
    public string To { get; set; }
    public List<Attachment> Attachments { get; set; }
    public List<Header> Headers { get; set; }
}

public class Attachment
{
    public string Content { get; set; } //null without stringifying JSON
    public string ContentType { get; set; }//null without stringifying JSON
    public string Name { get; set; }//null without stringifying JSON
}

public class Header
{
    public string Name { get; set; }//null without stringifying JSON
    public string Value { get; set; }//null without stringifying JSON
}

控制器方法:

[HttpPost, ValidateInput(false)]
    [WebMethod]
    public void Post(Email email)
    {
        InboundEmailService service = new InboundEmailService();
        service.ProcessEmail(email);
    }

邮戳的JSON:

    {
  "From": "myUser@theirDomain.com",
  "FromFull": {
    "Email": "myUser@theirDomain.com",
    "Name": "John Doe"
  },
  "To": "451d9b70cf9364d23ff6f9d51d870251569e+ahoy@inbound.postmarkapp.com",
  "ToFull": [
    {
      "Email": "451d9b70cf9364d23ff6f9d51d870251569e+ahoy@inbound.postmarkapp.com",
      "Name": ""
    }
  ],
  "Cc": "\"Full name\" <sample.cc@emailDomain.com>, \"Another Cc\" <another.cc@emailDomain.com>",
  "CcFull": [
    {
      "Email": "sample.cc@emailDomain.com",
      "Name": "Full name"
    },
    {
      "Email": "another.cc@emailDomain.com",
      "Name": "Another Cc"
    }
  ],
  "ReplyTo": "myUsersReplyAddress@theirDomain.com",
  "Subject": "This is an inbound message",
  "MessageID": "22c74902-a0c1-4511-804f2-341342852c90",
  "Date": "Thu, 5 Apr 2012 16:59:01 +0200",
  "MailboxHash": "ahoy",
  "TextBody": "[ASCII]",
  "HtmlBody": "[HTML(encoded)]",
  "Tag": "",
  "Headers": [
    {
      "Name": "X-Spam-Checker-Version",
      "Value": "SpamAssassin 3.3.1 (2010-03-16) onrs-ord-pm-inbound1.wildbit.com"
    },
    {
      "Name": "X-Spam-Status",
      "Value": "No"
    },
    {
      "Name": "X-Spam-Score",
      "Value": "-0.1"
    },
    {
      "Name": "X-Spam-Tests",
      "Value": "DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_PASS"
    },
    {
      "Name": "Received-SPF",
      "Value": "Pass (sender SPF authorized) identity=mailfrom; client-ip=209.85.160.180; helo=mail-gy0-f180.google.com; envelope-from=myUser@theirDomain.com; receiver=451d9b70cf9364d23ff6f9d51d870251569e+ahoy@inbound.postmarkapp.com"
    },
    {
      "Name": "DKIM-Signature",
      "Value": "v=1; a=rsa-sha256; c=relaxed\/relaxed;        d=wildbit.com; s=google;        h=mime-version:reply-to:date:message-id:subject:from:to:cc         :content-type;        bh=cYr\/+oQiklaYbBJOQU3CdAnyhCTuvemrU36WT7cPNt0=;        b=QsegXXbTbC4CMirl7A3VjDHyXbEsbCUTPL5vEHa7hNkkUTxXOK+dQA0JwgBHq5C+1u         iuAJMz+SNBoTqEDqte2ckDvG2SeFR+Edip10p80TFGLp5RucaYvkwJTyuwsA7xd78NKT         Q9ou6L1hgy\/MbKChnp2kxHOtYNOrrszY3JfQM="
    },
    {
      "Name": "MIME-Version",
      "Value": "1.0"
    },
    {
      "Name": "Message-ID",
      "Value": "<CAGXpo2WKfxHWZ5UFYCR3H_J9SNMG+5AXUovfEFL6DjWBJSyZaA@mail.gmail.com>"
    }
  ],
  "Attachments": [
    {
      "Name": "myimage.png",
      "Content": "[BASE64-ENCODED CONTENT]",
      "ContentType": "image/png",
      "ContentLength": 4096
    },
    {
      "Name": "mypaper.doc",
      "Content": "[BASE64-ENCODED CONTENT]",
      "ContentType": "application/msword",
      "ContentLength": 16384
    }
  ]
}

0 个答案:

没有答案