Today I am gonna cover how you may use Your Gmail (or other email accounts like Yahoo, Outlook, etc.) to let your website viewers send emails to you.
My Current Approach on My Site
Use Java Servlet
Code Part 1
@WebServlet(“/Mail”)
public class Mail extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String SUCCESS = “/emailsuccess.html”;
private static String FAIL = “/emailerror.html”;
public Mail() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpSer vletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String forward = “”;
// grab input data from index.jsp
String name = request.getParameter(“name”);
String email = request.getParameter(“email”);
String number = request.getParameter(“contact_number”);
String message = request.getParameter(“message”);
String subject = “Email from Name: ” + name
+ “; Email Address ” + email
+ “; Phone Number: ” + number;
System.out.println(“subject is: ” + subject);
System.out.println(“message is: ” + message);
/*EmailThread sentEmail = new EmailThread(subject, message);
sentEmail.start();*/
Runnable r = new EmailThread(subject, message);
Thread sentEmail = new Thread(r);
sentEmail.start();
if(sentEmail.isAlive()) {
forward = SUCCESS;
} else {
forward = FAIL;
}
//response.sendRedirect(“emailsuccess.html”);
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
}
}
Code Part 2
public class EmailThread implements Runnable {
private String subject;
private String message;
public EmailThread(String subject, String message) {
this.subject = subject;
this.message = message;
}
@Override
public void run() {
final String username = “YOUR_SENDER’s_EMAILADDRESS”;
final String pwd = “YOUR_SENDER’s_EMAILADDRESS_PASSWORD”;
// Get a Properties object SSL
Properties props = new Properties();
props.put(“mail.smtp.host”, “smtp.gmail.com”);
props.put(“mail.smtp.socketFactory.port”, “465”);
props.put(“mail.smtp.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”);
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.port”, “465”);
System.out.println(“Mail Server Properties have been setup successfully!”);
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, pwd);
}
});
try {
Message msg = new MimeMessage(session);
// Set the sender
msg.setFrom(new InternetAddress(“YOUR_SENDER’s_EMAILADDRESS”));
// Set the recipient
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(“YOUR_RECIPIENT’s_EMAILADDRESS”));
// Create mail subject
msg.setSubject(subject);
// create mail body
msg.setText(message);
System.out.println(“Mail Session has been created successfully!”);
Transport.send(msg);
System.out.println(“Msg sent!!”);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
} // end of run()
}
Notice
- You should own both the YOUR_SENDER’s_EMAILADDRESS and YOUR_RECIPIENT’s_EMAILADDRESS; one is for sending the viewer’s message to you and the other is for receiving the viewer’s message.
- Library for implementing this feature: please place javax.mail-1.5.1.jar or above in WEB-INF/lib/ folder. I failed many times initially due to using a javax.mail.jar whose version is not high enough.
- If necessary, log onto YOUR_SENDER’s_EMAILADDRESS through your browser and lower the security level set by Google.