电子邮件驱动的图像上传imgur

时间:2014-01-02 13:39:18

标签: email asp.net-mvc-4 mandrill

我正在使用mandrill来管理电子邮件服务,它是用于检索附加图像的入站电子邮件webhook(HTTP POST)的功能。

Mandrill入站webhook的详细信息。

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

我试图获取HTTP入站webhook但无法将其反序列化为json,并且无法检索附加的图像。

我使用了跟随github链接的类和方法。

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

获取附加图像后,我需要使用API​​将其上传到imgur网站, 能够将图像上传到imgur网站,但是我从mandrill检索来自入站webhook的附件时遇到问题。

请尽快帮助我。

1 个答案:

答案 0 :(得分:0)

我已经按照你在github中的说法完成了课程,我在课堂上又增加了一个字段,以便从电子邮件中获取附件值。 这是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; }
}
}

你必须像这样打电话给imgur api。

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