【问题标题】:email driven image upload on imgurimgur 上的电子邮件驱动图像上传
【发布时间】:2014-01-02 13:39:18
【问题描述】:

我正在使用 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 检索附件时遇到问题。

请尽快帮助我。

【问题讨论】:

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


    【解决方案1】:

    我已经按照您在 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);
        }
    

    【讨论】:

      猜你喜欢
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 2016-04-25
      • 1970-01-01
      相关资源
      最近更新 更多