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.getAttributeNames();
		while (en.hasMoreElements()) {
			String key = en.nextElement();
			s.append(key);
			s.append("=");
			s.append(req.getParameter(key));
			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();
	}

}

@@ -931,17 +931,17 @@
 .get
-Attribu
+Parame
 te
+r
 Name
@@ -1019,76 +1019,168 @@
 %0A%09%09%09
-s.append(key);%0A%09%09%09s.append(%22=%22);%0A%09%09%09s.append(req.getParameter(key));
+String%5B%5D values = req.getParameterValues(key);%0A%09%09%09s.append(key);%0A%09%09%09s.append(%22=%22);%0A%09%09%09for (String value : values) %7B%0A%09%09%09%09s.append(value);%0A%09%09%09%09s.append(%22 %22);%0A%09%09%09%7D
 %0A%09%09%09

fix looping of parameter names