Question

I'm using mandrill for managing email service, and it's features of inbound email webhook (HTTP POST) for retrieving the attached image.

Details of Mandrill inbound webhook.

http://help.mandrill.com/forums/21092258-Inbound-Email-Basics

I've tried to get the HTTP inbound webhook but unable to deserialize it into json, and unable to retrieve the attached image.

I've used class and method from following github link.

https://github.com/martydill/mandrill-inbound-classes

after fetching the attached image I need to upload it to imgur website using API, Am able to upload images to imgur website but i'm facing problem while retrieving attachment from inbound webhook from mandrill.

kindly help me as soon as possible.

Was it helpful?

Solution

I've gone through the class as you stated in github and I've added one more field in the class to get the attachment value from the email. here is the required class for mandrill webhook.

using System.Collections.Generic;
using Newtonsoft.Json;

namespace Mandrill
{
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; }

    //[JsonProperty(PropertyName = "attachments")]
    //public attachments attachments { get; set; }

    [JsonProperty(PropertyName = "attachments")]
    public IDictionary<string, IDictionary<string,string>> attachments { 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 attachments
{
    [JsonProperty(PropertyName = "name ")]
    public string name { get; set; }

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

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

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

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; }
}
}

and you have to call the imgur api like this.

[HttpPost]
    [ValidateInput(false)]
    public ActionResult past_mandrill(FormCollection fc)
    {

        string json = fc["mandrill_events"];

        //SqlConnection con = new SqlConnection(WebConfigurationManager.AppSettings[0]);
        var events = JsonConvert.DeserializeObject<IEnumerable<Mandrill.MailEvent>>(json);

        foreach (var mailEvent in events)
        {
            //Label2.Text = Label2.Text + mailEvent.Msg.To[0][0] + "<br>";
            try
            {

                foreach (KeyValuePair<string, IDictionary<string, string>> attch in mailEvent.Msg.attachments)
                {
                    //Label2.Text = Label2.Text + attch.Key + "<br>";
                    byte[] temp;
                    string albumid = "zBBCDbRcNhE493I"; //use your own album id where you want to store the image.
                    foreach (KeyValuePair<string, string> attchcnt in attch.Value)
                    {
                        //Label2.Text = Label2.Text + attchcnt.Key + "  " + attchcnt.Value + "<br>";

                        if (attchcnt.Key.Equals("content"))
                        {
                            using (var w = new WebClient())
                            {

                                string base64String = "";
                                base64String = attchcnt.Value.ToString();
                                var values = new NameValueCollection
                            {
                                {"image",  base64String},
                                {"album", albumid}
                            };

                                w.Headers.Add("Authorization", "Client-ID dac37a6b08b4974"); // user your own client-id of imgur website
                                byte[] response = w.UploadValues("https://api.imgur.com/3/upload.xml", values);
                                temp = response;

                            }
                        }


                    }

                }
            }
            catch (Exception e)
            {
            }

            //Label2.Text = mailEvent.Msg.attachments.Values.ToString();



        }


        return new HttpStatusCodeResult((int)HttpStatusCode.OK);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top