public
Created

SendGrid StackMob - Build URL and POST

  • Download Gist
gistfile1.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
//Encode any parameters that need encoding (i.e. subject, toname, text)
try {
subject = URLEncoder.encode(subject, "UTF-8");
text = URLEncoder.encode(text, "UTF-8");
toname = URLEncoder.encode(toname, "UTF-8");
 
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage(), e);
}
String queryParams = "api_user=" + API_USER + "&api_key=" + API_KEY + "&to=" + to + "&toname=" + toname + "&subject=" + subject + "&text=" + text + "&from=" + from;
 
url = "https://www.sendgrid.com/api/mail.send.json?" + queryParams;
Header accept = new Header("Accept-Charset", "utf-8");
Header content = new Header("Content-Type", "application/x-www-form-urlencoded");
Set<Header> set = new HashSet();
set.add(accept);
set.add(content);
try {
HttpService http = serviceProvider.getHttpService();
PostRequest req = new PostRequest(url,set,body);
HttpResponse resp = http.post(req);
responseCode = resp.getCode();
responseBody = resp.getBody();
} catch(TimeoutException e) {
...
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.