Skip to content

Instantly share code, notes, and snippets.

@jtuberville
Created May 11, 2012 01:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtuberville/2656965 to your computer and use it in GitHub Desktop.
Save jtuberville/2656965 to your computer and use it in GitHub Desktop.
How to urlencode using HttpClient
public static String sendViaHttpClient(String userName, String apiKey, String from, String fromName, String subject, String body, String to) {
NameValuePair[] data = {
new BasicNameValuePair("userName", userName),
new BasicNameValuePair("api_key", apiKey),
new BasicNameValuePair("from", from),
new BasicNameValuePair("from_name", fromName),
new BasicNameValuePair("subject", subject),
new BasicNameValuePair("body_html", body),
new BasicNameValuePair("to", to)
};
try {
org.apache.http.client.HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://api.elasticemail.com/mailer/send");
post.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = client.execute(post);
System.out.println(response);
} catch (URISyntaxException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (HttpException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment