【发布时间】:2014-01-20 19:12:52
【问题描述】:
我一直试图让我的 MVC Web 应用程序使用 Mandrill API 订阅来发送电子邮件,但无济于事。这个 Mandrill API 通过将 JSON 对象发布到特定端点来工作。它看起来非常简单直接,这就是为什么每次我尝试发送电子邮件时都会收到来自 Mandrill 的回复,其中只有“500 内部服务器错误”,没有额外的信息或其他任何内容,这很令人沮丧。
有趣的是,Mandrill API 有一个public test tool,您可以使用它来测试他们的 API。因此,我进入并传递了与我的应用程序在收到烦人的 Http 错误时发送的完全相同的 JSON 对象,并且发送的电子邮件消息没有任何问题。所以基本上我通过他们的Test Tool 发送相同的电子邮件没有问题,但是当我的应用程序尝试发送它时它失败了。
请在下面找到受托人发送电子邮件。它还没有重构...对不起...
public bool Send(string to, string toDisplayName, string subject, string body)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://mandrillapp.com/api/1.0/messages/send.json");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
try
{
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = string.Format(@"{{""key"": ""{0}"",""message"": {{""html"": ""{1}"",""subject"": ""{2}"",""from_email"": ""{3}"",""from_name"": ""AppCompear"",""to"": [{{""email"": ""{4}"",""name"": ""{5}"",""type"": ""to""}}],""important"": false}},""async"": false,""ip_pool"": ""Main Pool""}}"
, _setting.Password, body.Replace(@"""", @"\"""), subject, _setting.UserName, to, toDisplayName);
LogManager.Current.LogError(to, "", "", "", null, json);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
}
catch (WebException wex)
{
LogManager.Current.LogError(to, "", "", "", wex, wex.Message);
}
catch (Exception ex)
{
LogManager.Current.LogError(to, "", "", "", ex, ex.Message);
}
return true;
}
当然,在尝试获取响应时它会失败
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
任何帮助将不胜感激
【问题讨论】:
标签: c# asp.net-mvc mandrill