<%@ page language="java" contentType="text/html" %>
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<%

  // Get the parameter values from the form
  String email = request.getParameter("email");
  String name = request.getParameter("realname");
  String comment = request.getParameter("Comment");
  String content = request.getParameter("Message");
  String contact = request.getParameter("Contact");

  Properties props = new Properties();
  props.put("mail.smtp.host", "Enter your Mail Host here");
  Session s = Session.getInstance(props,null);

  MimeMessage message = new MimeMessage(s);

  // Error Vector for invalid name & email
  Vector error = new Vector();

  // Check if a name was transmitted, else error
  if (name.equals("")) {
    error.addElement(new String("Your name is missing"));
  }

  InternetAddress to = new InternetAddress("Enter Mail recipient here");
  message.addRecipient(Message.RecipientType.TO, to);

  // Check email
  if (!email.equals("")){
    if(email.indexOf("@") >0 && email.indexOf(".", email.indexOf("@"))!=-1) {
      try{
         message.setFrom(new InternetAddress(email));
      }
      catch(AddressException e){
        // email still invalid (java test)
        error.addElement(new String("Your email address isn't valid"));
      }
    }
    else error.addElement(new String("Your email address isn't valid"));
  }
  // No email
  else message.setFrom(new InternetAddress("feedback@akadia.com"));

  message.setSubject("Akadia Contact Feedback");

  // Generate email body
  StringBuffer text = new StringBuffer();
  text.append("Below is the result of your feedback form. \nIt was submitted by " + name
  + " (" + email + ") on " + (new java.util.Date().toString()));
  text.append("\n---------------------------------------------------------------------------\n");
  text.append("\nComment: " + comment);
  text.append("\n\nMessage: " + content);
  if (contact != null && contact.equals("Yes")) text.append("\n\nContact: yes");
  text.append("\n\n---------------------------------------------------------------------------\n");

  text.append("\nHTTP_USER_AGENT: " + request.getHeader("User-Agent"));
  text.append("\nREMOTE_ADDR: " + request.getRemoteAddr());

  message.setText(text.toString());

  // Display the response site
  if (error.size() < 1){
   Transport.send(message);
%>
      <%-- Success-Message --%>
      <html>
      <head>
         <link rel=stylesheet href=https://www.akadia.com/docroot/css/website.css type=text/css>
         <title>Thank you for your Feedback!</title>
      </head>
      <body bgcolor="#FFFFFF" link="#000000" vlink="#000000" alink="#000000" text="#000000">
        <img src="../img/red_arrow_2.gif" WIDTH=9 HEIGHT=9>&nbsp;
        <a href=javascript:history.back()>Back</a><br><br>
        <b>Thank you for your Feeback!</b>
        <p class=normal>Message sent: <%= new java.util.Date().toString() %><br>
                    Recipient: <%= to %> </p>
        <b>Comment: </b> <%= comment %> <br><br>
        <b>Message: </b> <%= content %> <br><br>

 <%     if (contact != null && contact.equals("Yes")) { %>
         <b>Contact: </b> <%= contact %>
 <% } %>
       </body>
      </html>
<% }
  else {
%>
      <%-- Error-Message --%>
      <html>
      <head>
         <link rel=stylesheet href=https://www.akadia.com/docroot/css/website.css type=text/css>
         <title>Error: Field(s) not correct!</title>
       </head>
      <body bgcolor="#FFFFFF" link="#000000" vlink="#000000" alink="#000000" text="#000000">
        <img src="../img/red_arrow_2.gif" WIDTH=9 HEIGHT=9>&nbsp;
        <a href=javascript:history.back()>Back</a><br><br>
        <b>Error: Please enter a correct value in the following field</b><br>
        <ul>
<%      // error list
        for(int i = 0; i< error.size(); i++) {  %>
          <li><%= error.elementAt(i) %>
<% } %>
        </ul>
       <p class="normal">Please complete the entry form -- thanks</p>
       </body>
      </html>
<% } %>