
/*
 *  Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
*/
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package oracle.javacloud.emailpunchout;

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

/**
 *
 * @author Shyamant Hegde
 */
public class JavaMailServlet extends HttpServlet {


    String recipient;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response, String param)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet JMailServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            if(param.contains("successfully")){
                out.println("<h1>Mail sent successfully to "+ recipient+".</h1>");
	    }
            else{
                out.println("<h1>Mail sending failed due to below error:</h1>");
		out.println("<br/>");
		out.println("<h2>"+param+"</h2>");
            out.println("</body>");
            out.println("</html>");
        }}catch(Exception e){
		
	} finally {            
            out.close();
        }
        
    }
    
    protected void processRequest(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SampleMailServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            
            String user = req.getParameter("user"), recp = req.getParameter("rcp"), 
                    msg = req.getParameter("msg"), subj = req.getParameter("subj");
	    //String replyTo = req.getParameter("reply-to");              
            String messageText = "This is a sample mail to test the functionality of java mail API.\nGood Luck!!!";
            String retVal;
            //Put the actual password here
            String passWord = req.getParameter("pwd");
	      recipient = recp;
	      JavaMail JM = new JavaMail(msg, "stbeehive.oracle.com", "465", user, recp, subj);
              retVal  = JM.service(passWord);          
              processRequest(req, resp, retVal);
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }


}
