【问题标题】:Mandrill : The remote server returned an error: (500) internal server errorMandrill:远程服务器返回错误:(500)内部服务器错误
【发布时间】:2015-03-27 15:50:49
【问题描述】:

我正在使用下面的代码使用 Mandrill 发送邮件

    public bool sendMessage(string from, List<To> to, string subject, string body,out string rsText)
    {
        var host = "https://mandrillapp.com/api/1.0";

        if (key == "")
        {
            throw new Exception("No key provided");
        }

        bool b = false;

        System.Net.WebClient client = new System.Net.WebClient();

        client.Headers.Add("content-type", "application/json");

        MandrillMessage msg = new MandrillMessage()
        {
            from_email = from,
            html = body,
            to = to,
            subject = subject
        };

        this.message = msg;

        var jsonMsg = Newtonsoft.Json.JsonConvert.SerializeObject(this);

        var rs = client.UploadData(host + "/messages/send.json", "POST", System.Text.Encoding.Default.GetBytes(jsonMsg));

        var responseText = System.Text.Encoding.ASCII.GetString(rs);

        //TODO Reponse With Generic Class

        if (responseText.Contains("sent"))
        {
            b = true;
        }
        rsText = responseText;
        return b;
    }
}

当我调用uploadData 方法时出现异常:

“远程服务器返回错误:(500) 内部服务器错误。”

json 对象包含以下值

{\"key\":\"aW1KVbcpVIQpPoSQgDkZ_Q\",\"message\":{\"html\":\"\\r\\n\\t\\t                    <table style=\\\"border:0;color:#868789;font-family: verdana;\\\">\\r\\n\\t\\t                        <tr>\\r\\n\\t\\t                            <td rowspan=\\\"3\\\" style=\\\"vertical-align:middle;height:1083px;\\\" id=\\\"imagePrincipale\\\">\\r\\n\\t\\t                                <img src=\\\"http://vm0024:8080/captain/iso_album/ah13-promo_mc_sept-web-va.jpg\\\" alt=\\\"\\\"/>\\r\\n\\t\\t                            </td>\\r\\n\\t\\t                            <td style=\\\"vertical-align:top;text-align:right;height:65px;\\\"><img src=\\\"http://vm0024:8080/captain/iso_album/test2.png\\\" alt=\\\"\\\" width=\\\"auto\\\" height=\\\"65\\\"/></td>\\r\\n\\t\\t                        </tr>\\r\\n\\t\\t                        <tr>                          \\r\\n\\t\\t                            <td style=\\\"vertical-align:middle;padding-left:10px;height:968px;\\\" id=\\\"tdSizable\\\">\\r\\n\\t\\t                                <p><b>test Start a workshop</b><br/>\\r\\n\\t\\t                                <span style=\\\"font-size:14px;font-weight: bold;\\\"><i style=\\\"font-size: 18px;font-family: serif;\\\">\\\"</i> Révélez votre style !<i style=\\\"font-size: 18px;font-family: serif;\\\">\\\"</i></span></p>\\r\\n\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t<br/>\\r\\n\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t<br/>\\r\\n\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t<br/>\\r\\n\\t\\t                                <p style=\\\"color:#E20055;font-weight: bold;\\\">\\r\\n\\t\\t                                 On the Saturday 12 December 2015<br/>\\r\\n\\t\\t                                From 15h00 à 18h00\\r\\n\\t\\t                                </p>\\r\\n\\t\\t                                <br/>\\r\\n\\t\\t                                <p>\\r\\n\\t\\t                                  <b> To: </b> test\\r\\n\\t\\t                                </p>\\r\\n\\t\\t                                <p>\\r\\n\\t\\t                                  <b> Tel number</b> 0123456789\\r\\n\\t\\t                                </p>\\r\\n\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t<br/>\\r\\n\\t\\t                                <p style=\\\"color:#E20055;font-weight: bold;\\\">\\r\\n\\t\\t                                 Your friends are welcome<br/>\\r\\n\\t\\t                                Please feel free to bring your friends\\r\\n\\t\\t                                </p>\\r\\n\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t<br/><br/><br/>\\r\\n\\t\\t                                <b>Pippa SMITH,</b><br/>\\r\\n\\t\\t                                Administrateurs  Captain tortue group\\r\\n\\t\\t                            </td>                            \\r\\n\\t\\t                        </tr>\\r\\n\\t\\t                        <tr>\\r\\n\\t\\t                            <td style=\\\"text-align:right;vertical-align:bottom;height: 50px; border-top: solid 1px #868789\\\">\\r\\n\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t<img width=\\\"248\\\" height=\\\"auto\\\" src=\\\"http://vm0024:8080/captain/iso_icons/cpTortue_mailFooterLogo.jpg\\\" alt=\\\"logo\\\"/>\\r\\n\\t\\t                            </td>\\r\\n\\t\\t                        </tr>\\r\\n\\t\\t                    </table>\\r\\n\\t\\t                \",\"text\":null,\"subject\":\"Invitation à un atelier mode Révélez votre style !\",\"from_email\":\"p.smith731@captaintortue.com; Pippa SMITH\",\"from_name\":null,\"to\":[{\"email\":\"karenclakre@btinternet.com\",\"name\":\"karenclakre@btinternet.com\",\"type\":\"bcc\"},{\"email\":\"katkydaly@googlemail.com\",\"name\":\"katkydaly@googlemail.com\",\"type\":\"bcc\"}],\"important\":false,\"track_opens\":null,\"track_clicks\":null,\"auto_text\":null,\"auto_html\":null,\"inline_css\":null,\"url_strip_qs\":null,\"preserve_recipients\":null,\"view_content_link\":null,\"bcc_address\":null,\"tracking_domain\":null,\"signing_domain\":null,\"return_path_domain\":null,\"merge\":false,\"global_merge_vars\":null,\"merge_vars\":null,\"tags\":null,\"subaccount\":null,\"google_analytics_domains\":null,\"google_analytics_campaign\":null,\"metadata\":null,\"recipient_metadata\":null,\"attachments\":null,\"images\":null},\"async\":false,\"ip_pool\":null,\"send_at\":null}"

有人可以帮忙吗?

【问题讨论】:

  • 那真的是你的json吗?看起来很无效。
  • 是的,它是通过 Newtonsoft.Json.JsonConvert.SerializeObject(this) 指令生成的。你觉得问题出在这儿吗?

标签: c# json json.net mandrill


【解决方案1】:

尝试寻找非转义的 html 字符,例如 © 。我遇到了同样的错误,结果我需要使用 & #169; (&和#之间没有空格)

【讨论】:

    【解决方案2】:

    您需要添加以下标题。对于同样的错误,它对我有用

    client.Encoding = Encoding.UTF8;
    

    【讨论】:

    • 是的! mandrillapp.com 行为改变 c. 2020 年 11 月 2 日(至少那是我们注意到的时候),现在这是必需的。