本文介绍了如何处理.NET山魈网络挂接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名学生试图与山魈工作和老实说,我还没有什么线索我在。
我能够发送电子邮件在.NET中使用山魈没有问题
我想现在要做的就是利用网络挂接搭上反弹的电子邮件的那一刻,也许更一旦我做到了这一点。

I'm a student trying to work with mandrill and to be quite honest, I haven't a clue what I'm at.I'm able to send emails no problem using mandrill in .netWhat I want to do now is use webhooks to catch bounce emails at the moment and maybe more once I have accomplished that.

下面是code我至今(来自互联网)

Here is the code I have so far (from the internet)

public ActionResult HandleMandrillWebhook(FormCollection fc)
    {
        string json = fc["mandrill_events"];

        var events = JsonConvert.DeserializeObject<IEnumerable<Mandrill.MailEvent>>(json);
        foreach (var mailEvent in events)
        {
            var message = mailEvent.Msg;
            // ... Do stuff with email message here...
        }

        // MUST do this or Mandrill will not accept your webhook!
        return new HttpStatusCodeResult((int)HttpStatusCode.OK);

然后我有这个

public class MailEvent
{
    [JsonProperty(PropertyName = "ts")]
    public string TimeStamp { get; set; }

    [JsonProperty(PropertyName = "event")]
    public string Event { get; set; }

    [JsonProperty(PropertyName = "msg")]
    public Message Msg { get; set; }
}

public class Message
{
    [JsonProperty(PropertyName = "raw_msg")]
    public string RawMessage { get; set; }

    [JsonProperty(PropertyName = "headers")]
    public Header Header { get; set; }

    [JsonProperty(PropertyName = "text")]
    public string Text { get; set; }

    [JsonProperty(PropertyName = "html")]
    public string Html { get; set; }

    [JsonProperty(PropertyName = "from_email")]
    public string FromEmail { get; set; }

    [JsonProperty(PropertyName = "from_name")]
    public string FromName { get; set; }

    // Not sure why Mandrill sends an array of arrays here...
    [JsonProperty(PropertyName = "to")]
    public string[][] To { get; set; }

    [JsonProperty(PropertyName = "email")]
    public string Email { get; set; }

    [JsonProperty(PropertyName = "subject")]
    public string Subject { get; set; }

    [JsonProperty(PropertyName = "tags")]
    public string[] Tags { get; set; }

    [JsonProperty(PropertyName = "sender")]
    public string Sender { get; set; }

    [JsonProperty(PropertyName = "dkim")]
    public DKIM DKIM { get; set; }

    [JsonProperty(PropertyName = "spf")]
    public SPF SPF { get; set; }

    [JsonProperty(PropertyName = "spam_report")]
    public SpamReport SpamReport { get; set; }
}

[JsonDictionary()]
public class Header : Dictionary<string, object>
{
    // Need to find a nicer way of doing this... Dictionary<string, object> is kinda dumb
}

public class SpamReport
{
    [JsonProperty(PropertyName = "score")]
    public decimal Score { get; set; }

    [JsonProperty(PropertyName = "matched_rules")]
    public SpamRule[] MatchedRules { get; set; }
}

public class SpamRule
{
    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "score")]
    public decimal Score { get; set; }

    [JsonProperty(PropertyName = "description")]
    public string Description { get; set; }
}

public class DKIM
{
    [JsonProperty(PropertyName = "signed")]
    public bool Signed { get; set; }

    [JsonProperty(PropertyName = "valid")]
    public bool Valid { get; set; }
}

public class SPF
{
    [JsonProperty(PropertyName = "result")]
    public string Result { get; set; }

    [JsonProperty(PropertyName = "detail")]
    public string Detail { get; set; }
}

有没有人能告诉我如何处理山魈网络挂接响应,那么。
它的JSON。

Could somebody please show me how to process the mandrill webhook response then.Its in json.

我以前从来没有做过这样的事。我失去了太多code?
是JSON传过来的文件或裸code?

I have never done anything like this before. Am I missing much code?Is json passed in as a file or raw code?

谢谢你们。
我真的AP preciate它。

Thanks guys.I really appreciate it.

推荐答案

我正在我的山魈网络挂接处理作为VS API项目,这是我如何得到它运行起来:

I am running my Mandrill Webhook handling as an API project in VS and this is how I am have gotten it up and running:

    [HttpPost]
    public string Post()
    {
        /* Every Mandrill webhook uses the same general data format, regardless of the event type.
         * The webhook request is a standard POST request with a single parameter (currently) - 'mandrill_events'. */

        string validJson = HttpContext.Current.Request.Form["mandrill_events"].Replace("mandrill_events=", ""); //"mandrill_events=" is not valid JSON. If you take that out you should be able to parse it. //http://stackoverflow.com/questions/24521326/deserializing-mandrillapp-webhook-response
        List<MandrillEvent> mandrillEventList = JsonConvert.DeserializeObject<List<MandrillEvent>>(validJson);

        foreach (MandrillEvent mandrillEvent in mandrillEventList)
        {
            if (mandrillEvent.msg.email != null)
            {
                DataLayer.ReportingData.EmailSave(mandrillEvent); //Saves MandrillEvent email to database and sets a messageId for datalayer
            }
        }

        foreach (MandrillEvent mandrillEvent in mandrillEventList)
        {
            DataLayer.ReportingData.MandrillEventSave(mandrillEvent); //Saves MandrillEvent object to database
        }

        return "DONE";
    }

我然后拿着的mandrill_event的记载(和无证)JSON参数和使用产生C#的属性。我创建了一个名为MandrillEvent.cs级和把这些内:

I then took the documented (and undocumented) JSON parameters of "mandrill_event" and used json2csharp.com to generate the C# properties. I created a class called "MandrillEvent.cs" and put these within:

public class SmtpEvent
    {
        public int ts { get; set; }
        public DateTime SmtpTs { get; set; }
        public string type { get; set; }
        public string diag { get; set; }
        public string source_ip { get; set; }
        public string destination_ip { get; set; }
        public int size { get; set; }
        public int smtpId { get; set; } //added for datalayer
    }

    public class Msg
    {
        public int ts { get; set; }
        public DateTime MsgTs { get; set; }
        public string _id { get; set; }
        public string state { get; set; }
        public string subject { get; set; }
        public string email { get; set; }
        public List<object> tags { get; set; }
        public List<object> opens { get; set; } //an array of containing an item for each time the message was opened. Each open includes the following keys: "ts", "ip", "location", "ua"
        public List<object> clicks { get; set; } //an array containing an item for each click recorded for the message. Each item contains the following: "ts", "url"
        public List<SmtpEvent> smtp_events { get; set; }
        public List<object> resends { get; set; } //not currently documented on http://help.mandrill.com/entries/58303976-Message-Event-Webhook-format
        public string _version { get; set; }
        public string diag { get; set; } //for bounced and soft-bounced messages, provides the specific SMTP response code and bounce description, if any, received from the remote server
        public int bgtools_code { get; set; } //Is it this? for bounced and soft-bounced messages, a short description of the bounce reason such as bad_mailbox or invalid_domain. (not currently documented but in JSON response)
        public string sender { get; set; }
        public object template { get; set; }
        public string bounce_description { get; set; }

        public Msg()
        {
            tags = new List<object>();
            opens = new List<object>();
            clicks = new List<object>();
            smtp_events = new List<SmtpEvent>();
            smtp_events.Add(new SmtpEvent());
            resends = new List<object>();
        }
    }

    public class MandrillEvent
    {
        public string @event { get; set; }
        public string _id { get; set; }
        public Msg msg { get; set; }
        public int ts { get; set; }
        public DateTime MandrillEventTs { get; set; }
        public int messageId { get; set; } //added for datalayer
        public List<string> SingleMandrillEventData { get; set; } //added for Reporting

        public MandrillEvent()
        {
            SingleMandrillEventData = new List<string>();
            msg = new Msg();
        }
    }

您现在有你mandrill_eventsJSON对象作为一个正常运作的C#对象!这样做帮助或者你需要一些更多的澄清/帮助?

You now have your "mandrill_events" JSON object as a functioning C# object!! Did this help or do you need some more clarification/help?

这篇关于如何处理.NET山魈网络挂接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 08:55