Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SendGrid Web API in ASP.NET 3.5

I am using SendGrid Email Service to send an email through Web API (HttpWebRequest). This is the code am using but I am getting a 400 response.

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();
like image 800
user1547639 Avatar asked Dec 27 '22 20:12

user1547639


1 Answers

Your main issue is that you're sending us something with a text/xml content type, but we only accept form encoded content or query strings. Here's a fully working example of what you're trying to do.

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 = "[email protected]";
    string toName = "To Name";
    string subject = "A message from SendGrid";
    string text = "Delivered by your friends at SendGrid.";
    string fromAddress = "[email protected]";

    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);
    }
  }
}
like image 164
Swift Avatar answered Jan 15 '23 05:01

Swift