Skip to content

Instantly share code, notes, and snippets.

@kbohinski
Created October 22, 2015 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbohinski/590b9adb4328f4c65a63 to your computer and use it in GitHub Desktop.
Save kbohinski/590b9adb4328f4c65a63 to your computer and use it in GitHub Desktop.
MailChimp Internship Application Extra Credit
/**
* @author Kevin Bohinski <bohinsk1@tcnj.edu>
* @version 1.0
* @since 2015-10-22
*
* Project Name: MailChimp Internship Application
* Description: "Extra credit for sending us a code sample to
* codesample@mailchimp.com as an attachment using
* the Mandrill API (send the code sample that's
* sending itself as the attachment - don't use SMTP
* or an API wrapper library, make an HTTP request
* all by yourself)"
*
* Filename: MailChimpDriver.java
* Description: Driver class used for sending the email.
* Last Modified: 2015-10-22
*
* Copyright (c) 2015 Kevin Bohinski. All rights reserved.
*/
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Base64;
import javax.net.ssl.HttpsURLConnection;
public class MailChimpDriver {
/**
* Main method, executes program.
*
* @param args
*/
public static void main(String[] args) throws Exception {
String urlStr = "https://mandrillapp.com/api/1.0/messages/send.json";
String key = "";
String body = "Internship Application Extra Credit!";
String subject = body;
String fromEmail = "bohinsk1@tcnj.edu";
String fromName = "Kevin Bohinski";
String toEmail = "bohinsk1@tcnj.edu";
String attachName = "Source.txt";
String attachBody = "Source Availiable At: ";
String attachmentEncoded = Base64.getEncoder()
.encodeToString(attachBody.getBytes("utf-8"));
String urlParams = "{\"key\":\"" + key + "\",\"message\":{\"text\":\""
+ body + "\",\"subject\":\"" + subject + "\",\"from_email\":\""
+ fromEmail + "\",\"from_name\":\"" + fromName
+ "\",\"to\":[{\"email\":\"" + toEmail
+ "\",\"type\":\"to\"}],\"attachments\":[{\"type\":\"text/plain\",\"name\":\""
+ attachName + "\",\"content\":\"" + attachmentEncoded
+ "\"}]}}";
URL url = new URL(urlStr);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(urlParams);
out.flush();
out.close();
int response = conn.getResponseCode();
System.out.println("Sending POST request:");
System.out.println(" URL: " + urlStr);
System.out.println(" Params: " + urlParams);
System.out.println("\nResponse Code: " + response);
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line = "";
StringBuilder sb = new StringBuilder();
while ((line = in.readLine()) != null) {
sb.append(line);
}
System.out.println(sb.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment