package com.github.davidmoten.email;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.sendgrid.SendGrid;

public class EmailServlet extends HttpServlet {

	private static final long serialVersionUID = -4925620975592532511L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String username = System.getProperty("SENDGRID_USERNAME");
		String password = System.getProperty("SENDGRID_PASSWORD");

		StringBuilder s = new StringBuilder();
		@SuppressWarnings("unchecked")
		Enumeration<String> en = req.getParameterNames();
		while (en.hasMoreElements()) {
			String key = en.nextElement();
			String[] values = req.getParameterValues(key);
			s.append(key);
			s.append("=");
			for (String value : values) {
				s.append(value);
				s.append(" ");
			}
			s.append("\n");
		}
		s.append("sent via SendGrid");

		SendGrid sendgrid = new SendGrid(username, password);

		sendgrid.addTo("davidmoten@gmail.com");
		sendgrid.setFrom("davidmoten@gmail.com");
		sendgrid.setSubject("hello from cloudbees-email app");
		sendgrid.setText(s.toString());

		sendgrid.send();
	}

}

@@ -1486,15 +1486,470 @@
 .send();
+%0A%0A%09%09// Set to expire far in the past.%0A%09%09resp.setHeader(%22Expires%22, %22-1%22);%0A%09%09// Set standard HTTP/1.1 no-cache headers.%0A%09%09resp.setHeader(%22Cache-Control%22, %22no-store, no-cache, must-revalidate%22);%0A%09%09// Set IE extended HTTP/1.1 no-cache headers (use addHeader).%0A%09%09resp.addHeader(%22Cache-Control%22, %22post-check=0, pre-check=0%22);%0A%09%09// Set standard HTTP/1.0 no-cache header.%0A%09%09resp.setHeader(%22Pragma%22, %22no-cache%22);%0A%0A%09%09resp.getWriter().write(%22sid=somebody&status=1%22);
 %0A%09%7D%0A%0A%7D%0A

return something from email servlet