我正在使用SendGrid电子邮件服务通过Web API(HttpWebRequest)发送电子邮件.这是我正在使用的代码,但我收到400响应.
string url = "https://sendgrid.com/api/mail.send.xml";
string parameters = "api_user=" + api_user + "&api_key=" + api_key + "&to=" + toAddress + "&toname=" + toName + "&subject=" + subject + "&text=" + text + "&from=" + fromAddress;
// Create Request
myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myHttpWebRequest.Method = "POST";
// myHttpWebRequest.ContentType = "application/json; charset=utf-8";
myHttpWebRequest.ContentType = "text/xml";
CookieContainer cc = new CookieContainer();
myHttpWebRequest.CookieContainer = cc;
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] postByteArray = encoding.GetBytes(parameters);
myHttpWebRequest.ContentLength = postByteArray.Length;
System.IO.Stream postStream = myHttpWebRequest.GetRequestStream();
postStream.Write(postByteArray, 0, postByteArray.Length); …Run Code Online (Sandbox Code Playgroud)