package control;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Mail;

/**
 * Servlet implementation class SendMail
 */
public class Contact extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Mail mailUtil;

	/**
	 * Default constructor.
	 */
	public Contact() {
		mailUtil = new Mail();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String smtpServer = "smtp.gmail.com";
			String to = "luanxt.info@yahoo.com";
			String from = "mr.luanxt@gmail.com";
			String password = "khongbiet";
			String subject = request.getParameter("subject");
			String realname = request.getParameter("realname");
			String email = request.getParameter("email");
			String body = request.getParameter("content");
			body = body + " " + realname + " " + email;
			Mail.send(smtpServer, to, from, password, subject, body);
			response.sendRedirect("index.jsp?customer=contact&stt=ok");
		} catch (Exception e) {
			e.printStackTrace();
			response.sendRedirect("index.jsp?customer=contact&stt=fail");
		}
	}
}
