I'm sorry, I forgot to add test(email)
in the condition that send the email.
package net.sourceforge.subsonic.controller;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.*;
import javax.mail.internet.*;
public class ContactServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
String email = request.getParameter("email");
String subject = request.getParameter("subject");
String message = request.getParameter("message");
request.setAttribute("emailState", email);
request.setAttribute("subState", subject);
request.setAttribute("msgState", message);
if (email.isEmpty()){
request.setAttribute("err_email", "Please proivde a email");
}
if (!test(email)){
request.setAttribute("err_email", "Provided incorrect email");
}
if (message.isEmpty()){
request.setAttribute("err_msg", "Please provide a message");
}
if (subject.isEmpty()){
request.setAttribute("err_sub", "Please provide a subject");
}
if(!email.isEmpty() && !message.isEmpty() && !subject.isEmpty() && test(email)){
Properties props = new Properties();
props.put("mail.smtp.host", "mysmptserver.com");
props.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(props, null);
try {
Message m = new MimeMessage(session);
m.setFrom(new InternetAddress(email, ""));
m.addRecipient(Message.RecipientType.TO, new InternetAddress("myadress@mydomain.com", ""));
m.setSubject(subject);
m.setText(message);
Transport.send(m);
} catch (AddressException e) {
e.printStackTrace();
} catch (javax.mail.MessagingException e) {
e.printStackTrace();
}
RequestDispatcher view = request.getRequestDispatcher("success.jsp");
view.forward(request, response);
}else{
RequestDispatcher view = request.getRequestDispatcher("contact.jsp");
view.forward(request, response);
}
}
public static boolean test(String str){
String reg = "[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
while(m.find())
{
String s1 = m.group(0);
if (str == s1) return true;
}
return false;
}
}
Contact form:
<form action="Contact.do" method="get">
<p>Your email address: <input name="email" value="<% if(request.getAttribute("emailState") != null) out.println(request.getAttribute("emailState")); %>"></p><% if(request.getAttribute("err_email") != null) out.println(request.getAttribute("err_email")); %>
<p>Mail subject: <input name="subject" value="<% if(request.getAttribute("subState") != null) out.println(request.getAttribute("subState")); %>"><% if(request.getAttribute("err_sub") != null) out.println(request.getAttribute("err_sub")); %></p>
<p>Mail message: <textarea name="message"><% if(request.getAttribute("msgState") != null) out.println(request.getAttribute("msgState")); %></textarea><% if(request.getAttribute("err_msg") != null) out.println(request.getAttribute("err_msg")); %></p>
<p><input type="submit"></p>
</form>