c# - ASP.NET 3.5 中的 SendGrid Web API

标签 c# asp.net sendgrid

我正在使用 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);
postStream.Close();

// Get Response
string result="";
HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();

最佳答案

您的主要问题是您向我们发送了内容类型为 text/xml 的内容,但我们只接受表单编码内容或查询字符串。这是您正在尝试执行的操作的完整示例。

using System;
using System.IO;
using System.Net;

public class SendGridWebDemo
{
  static public void Main ()
  {
    string api_user = "your_sendgrid_username";
    string api_key = "your_sendgrid_key";
    string toAddress = "to@example.com";
    string toName = "To Name";
    string subject = "A message from SendGrid";
    string text = "Delivered by your friends at SendGrid.";
    string fromAddress = "from@example.com";

    string url = "https://sendgrid.com/api/mail.send.json";

    // Create a form encoded string for the request body
    string parameters = "api_user=" + api_user + "&api_key=" + api_key + "&to=" + toAddress + 
                        "&toname=" + toName + "&subject=" + subject + "&text=" + text + 
                        "&from=" + fromAddress;

    try
    {
      //Create Request
      HttpWebRequest myHttpWebRequest = (HttpWebRequest) HttpWebRequest.Create(url);
      myHttpWebRequest.Method = "POST";
      myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";

      // Create a new write stream for the POST body
      StreamWriter streamWriter = new StreamWriter(myHttpWebRequest.GetRequestStream());

      // Write the parameters to the stream
      streamWriter.Write(parameters);
      streamWriter.Flush();
      streamWriter.Close();

      // Get the response
      HttpWebResponse httpResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();

      // Create a new read stream for the response body and read it
      StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream());
      string result = streamReader.ReadToEnd();

      // Write the results to the console
      Console.WriteLine(result);
    }
    catch(WebException ex)
    {
      // Catch any execptions and gather the response
      HttpWebResponse response = (HttpWebResponse) ex.Response;

      // Create a new read stream for the exception body and read it
      StreamReader streamReader = new StreamReader(response.GetResponseStream());
      string result = streamReader.ReadToEnd();

      // Write the results to the console
      Console.WriteLine(result);
    }
  }
}

关于c# - ASP.NET 3.5 中的 SendGrid Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11624722/

相关文章:

asp.net - 使用 asp :HiddenField value in JavaScript on page load?

c# - 邮政添加自定义 header

grails - Sendgrid将电子邮件添加到列表,grails中收到错误的请求

c# - LINQ to Entities 无法识别 MVC5 中的方法 'System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)' 方法错误

c# - 棋盘游戏棋子移动算法

c# - 异步等待调用长时间运行的同步和异步方法

c# - 查看 MVC 2 中无法正常工作的数据

c# - 将分隔文件的读取从 F# 转换为 C#

c# - string.ToLower() 和 string.ToLowerInvariant()

javascript - API key 不以 "SG."SendGrid 开头