
please send me the java code to send email using gmail smtp server. and how to send verification code..?

package servlets;
import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class JavaEmailServlet extends HttpServlet {
Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String toMail = request.getParameter("email");
String password = request.getParameter("password");
try {
JavaEmailServlet javaEmail = new JavaEmailServlet();
javaEmail.setMailServerProperties();
javaEmail.createEmailMessage(toMail) ;
javaEmail.sendEmail();
} catch (AddressException ex) {
System.out.println("ADDRESS EXCEPTION "+ex);
} catch (MessagingException ex) {
System.out.println("MESSAGING EXCEPTION "+ex);
}
out.println("EMAIL SENT TO YOU MAIL");
}
public void sendEmail() throws AddressException, MessagingException {
String emailHost = "smtp.gmail.com";
String fromUser = "Your Mail id";//just the id alone without @gmail.com
String fromUserEmailPassword = "your gmail passowrd";
Transport transport = mailSession.getTransport("smtp");
transport.connect(emailHost, fromUser, fromUserEmailPassword);
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();
System.out.println("Email sent successfully.");
}
public void createEmailMessage(String toEmails) throws AddressException,
MessagingException {
String emailSubject = "no reply-Verification Mail";
String emailBody = "This is the link from dexter labs<br><a href='http://192.168.3.9:8080/WebApplication1/mail.jsp'>click here</a>";
mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails));
emailMessage.setSubject(emailSubject);
emailMessage.setContent(emailBody, "text/html");//for a html email
//emailMessage.setText(emailBody);// for a text email
}
public void setMailServerProperties() {
String emailPort = "587";//gmail's smtp port
emailProperties = System.getProperties();
emailProperties.put("mail.smtp.port", emailPort);
emailProperties.put("mail.smtp.auth", "true");
emailProperties.put("mail.smtp.starttls.enable", "true");
} }
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.