Sending emails from a JSP page is very useful for contact forms, notifications, and automation in Java web applications. In this guide, you’ll learn how to send a simple text email using JSP + JavaMail API, step by step.
✅ Requirements
Before you begin, you need the following:
- JavaMail API Library Download from the official GitHub:
JavaMail (javax.mail.jar):
[https://repo1.maven.org/maven2/com/sun/mail/javax.mail/1.6.2/javax.mail-1.6.2.jar]
- JavaBeans Activation Framework (activation.jar) Download:
activation.jar:
[https://repo1.maven.org/maven2/com/sun/mail/javax.mail/1.6.2/javax.mail-1.6.2.jar]
📁 Place both JARs in your project’s WEB-INF/lib folder.
📝 Instructions to Use:
Download both .jar files.
Place them in your project’s WEB-INF/lib folder if using a Java web app.
Make sure your server (e.g., Apache Tomcat) loads them at runtime.
📄 Step-by-Step Implementation in JSP
Here’s a complete working example of sending an email from a sendmail.jsp file.
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<%
final String senderEmail = "your_email@gmail.com";
final String senderPassword = "your_app_password"; // App Password, not Gmail password
String recipient = "recipient@example.com";
String subject = "Test Mail from JSP";
String messageText = "This is a test email sent from JSP page.";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(senderEmail, senderPassword);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(senderEmail));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
message.setSubject(subject);
message.setText(messageText);
Transport.send(message);
out.println("<h3>Email sent successfully!</h3>");
} catch (MessagingException e) {
out.println("<h3>Error: " + e.getMessage() + "</h3>");
e.printStackTrace(out);
}
%>
🔐 Gmail App Password Setup
Since Google doesn't allow you to use your Gmail password directly, follow these steps:
Turn on 2-Step Verification for your Google account.
Go to: [https://myaccount.google.com/apppasswords]
Generate a new App Password.
Use this password in place of your Gmail password in the JSP code.
📌 Tips
Never use real credentials in public code.
Always handle exceptions gracefully.
For real-world projects, separate business logic (email sending) into a Servlet or Java Bean instead of putting it directly in JSP.
🧾 Summary
| Component | Description |
| -------------- | --------------------------------- |
| JSP Page | Sends email using JavaMail API |
| JavaMail Jar | javax.mail-1.6.2.jar
|
| Activation Jar | activation-1.1.1.jar
|
| SMTP Server | Gmail SMTP (smtp.gmail.com:587
) |
| Authentication | Gmail App Password |
Top comments (0)