Java Email Contact us page using Gmail
download mail jar file:-
java mail jar 1.4 or above references website www.java2s.com
Pom.xml
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
FileName.jsp
<%@ page import="com.memento.mail.JavaEmail"%>
<%@ page import="javax.mail.MessagingException"%>
<%@ page import=" java.util.*, java.io.*" %>
<%
String message = null;
String status = null;
if (request.getParameter("submit") != null) {
JavaEmail javaEmail = new JavaEmail();
javaEmail.setMailServerProperties();
String emailSubject = "Contact Form using Java JSP GMail By satish dodia";
String emailBody = "";
if (request.getParameter("name") != null) {
emailBody = "User Name: " + request.getParameter("name")
+ "<br>";
}
if (request.getParameter("email") != null) {
emailBody = emailBody + "User Email: "
+ request.getParameter("email") + "<br>";
}
if (request.getParameter("phone") != null) {
emailBody = emailBody + "User Phone: "
+ request.getParameter("phone") + "<br>";
}
if (request.getParameter("message") != null) {
emailBody = emailBody + "Message: " + request.getParameter("message")
+ "<br>";
}
javaEmail.createEmailMessage(emailSubject, emailBody);
try {
javaEmail.sendEmail();
status = "success";
message = "Email sent Successfully!";
} catch (MessagingException me) {
status = "error";
message = "Error in Sending Email!";
System.out.println(me);
}
}
%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Memento Tech :- Contact Us</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id="central">
<div class="content">
<h1>Contact Form</h1>
<p>easily contact using java email.</p>
<div id="message">
<!-- take one id for form
also action put blank or you can used servlet in action pass value in servlet methode like GET,POST etc...-->
<form id="frmContact" name="frmContact" action="" method="POST"
novalidate="novalidate">
<div class="label">Name:</div>
<div class="field">
<input type="text" id="pp-name" name="name"
placeholder="enter your name here" title="Please enter your name"
class="required" aria-required="true" required>
</div>
<div class="label">Email:</div>
<div class="field">
<input type="text" id="pp-email" name="email"
placeholder="enter your email address here"
title="Please enter your email address" class="required email"
aria-required="true" required>
</div>
<div class="label">Phone Number:</div>
<div class="field">
<input type="text" id="pp-phone" name="phone"
placeholder="enter your phone number here"
title="Please enter your phone number" class="required phone"
aria-required="true" required>
</div>
<div class="label">Message:</div>
<div class="field">
<textarea id="about-project" name="message"
placeholder="enter your message here"></textarea>
</div>
<div id="mail-status"></div>
<input type="submit" name="submit" value="Send Message"
id="send-message" style="clear: both;">
<%
if (null != message) {
out.println("<div class='" + status + "'>"
+ message + "</div>");
}
%>
</form>
</div>
</div>
<!-- content -->
</div>
<!-- memento tech -->
</body>
</html>
Now create java file:-
JavaEmail.java
package com.memento.mail;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaEmail {
Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;
String emailHost = "smtp.gmail.com";
String emailPort = "587";// gmail's smtp port
String fromUser = "sb.dodia@gmail.com";// your gmail id
String fromUserEmailPassword = "********";
String[] toEmails = { "sb.dodia@gmail.com" };
public void setMailServerProperties() {
emailProperties = System.getProperties();
emailProperties.put("mail.smtp.port", emailPort);
emailProperties.put("mail.smtp.auth", "true");
emailProperties.put("mail.smtp.starttls.enable", "true");
}
public void createEmailMessage(String emailSubject, String emailBody)
throws AddressException, MessagingException {
mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);
for (int i = 0; i < toEmails.length; i++) {
emailMessage.addRecipient(Message.RecipientType.TO,
new InternetAddress(toEmails[i]));
}
emailMessage.setSubject(emailSubject);
emailMessage.setContent(emailBody, "text/html");// for a html email
// emailMessage.setText(emailBody);// for a text email
}
public void sendEmail() throws AddressException, MessagingException {
Transport transport = mailSession.getTransport("smtp");
transport.connect(emailHost, fromUser, fromUserEmailPassword);
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();
}
}
also you got error message:-
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbuM
534-5.7.14 a942khC3ExHbAg8jBHffBJnLvgo917aIAn57pkeFiayvFuI-rHzt6AZGbsRHTTQdBUj39l
534-5.7.14 HFD1Uht-Iuf827RAglAeGSwIapNFg5E02BOfhukUbBtr_lw3bQ5opoSFRUzklqmvVKy2a8
534-5.7.14 XVKpYbytk5XNRV9lhBv_ycN_wKhoA2OXN2IrKCRkV7KfG17ZxYunCARbWaFaHt3CzXLwPy
534-5.7.14 OHPqflUZKlejx_BG__EJh6LVVvNpU> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 p26sm27693116pfj.23 - gsmtp
Solution:-
step:- 1 go to gmail setting
step 2:- allowing access to less secure apps
step 3:- Turn on
Step 4:- save
0 Comments