Pr.Pg Next Pg

Java Mail API tutorials

  • You will need JavaMail API installed on your machine for sending mail from JSP program.

  • You can download latest version of JavaMail (Version 1.2) from Java's standard website.

  • Download and unzip these files, in the newly created top level directories you will find a number of jar files for the applications. You need to add mail.jar in your CLASSPATH.

  • If you are using Netbeans then below is the steps to add mail.jar:

  1. Open Netbeans then go to File Menu à New Project à Now from category tab Select “Java Web” then from project category select “Web Application” and click Next, then give appropriate Project Name and click on Next then select “Server” and click Finish, now the opened Netbeans web application project will look like below:

Library installation


  1. Now Right click on “Libraries” and select “Add JAR/Folder..” from Projects tab as below:

Library installation

 

  1. Now in “Add JAR/Folder” dialog box locate your javax.mail.jar file from dropdown list and then select the javax.mail.jar file and click “Open” as below:

Library Installation


 

JSP program to send e-mail and it’s data coming from Mail.html

Sam Sir

<%--Program name MailProcess.jsp --%>

<%-- JSP program to send e-mail and it’s data coming from Mail.html --%>

 

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="javax.mail.*"%>

<%@page import="javax.mail.internet.*" %>

<%@page import="java.io.*" %>

<%@page import="java.util.Properties" %>

<%@page import="Authentication.SMTPAuthenticator" %>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Mail Result</title>

</head>

<body>

<%

String from = request.getParameter("from");

String to = request.getParameter("to");

String subject = request.getParameter("subject");

String message = request.getParameter("message");

String login = request.getParameter("login");

String password = request.getParameter("password");

try {

Properties props = new Properties();

props.setProperty("mail.host", "smtp.gmail.com");

props.setProperty("mail.smtp.port", "587");

props.setProperty("mail.smtp.auth", "true");

props.setProperty("mail.smtp.starttls.enable", "true");

 

Authenticator auth = new SMTPAuthenticator(login, password);

Session sess=Session.getInstance(props, auth);

MimeMessage msg = new MimeMessage(sess);

msg.setText(message);

msg.setSubject(subject);

msg.setFrom(new InternetAddress(from));

msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

Transport.send(msg);

out.println("Send Successfully..");

} catch (Exception ex) {

out.println("Error :"+ex.getMessage());

}

%>

</body>

</html>

 

 

 


 

Java class for authenticating user for sending e-mail

Sam Sir

//Program name SMTPAuthenticator.java

//Java class for authenticating user for sending e-mail

package Authentication;

 

import javax.mail.*;

public class SMTPAuthenticator extends Authenticator {

private PasswordAuthentication authentication;

 

public SMTPAuthenticator(String login, String password) {

authentication = new PasswordAuthentication(login, password);

}

protected PasswordAuthentication getPasswordAuthentication() {

return authentication;

}

 

}

 

 

 

Note: Create a Java package Authentication and placed SMTPAuthenticator class in Authentication package.

 

 

HTML program for designing UI for sending e-mail

Sam Sir

<!-- Mail.html -->

<!-- HTML program for designing UI for sending e-mail -->

<html>

<head>

<title>Sending email</title>

</head>

<body>

<h3>JavaMail API</h3>

<hr>

<form method="post" action="MailProcess.jsp">

<table>

<tr>

<td>Login</td>

<td><input type="text" name="login"></td>

</tr>

<tr>

<td>Password</td>

<td><input type="password" name="password"></td>

</tr>

<tr>

<td>From</td>

<td><input type="text" name="from"></td>

</tr>

<tr>

<td>To</td>

<td><input type="text" name="to"></td>

</tr>

<tr>

<td>Subject</td>

<td><input type="text" name="subject"></td>

</tr>

<tr>

<td>Message</td>

<td><textarea cols="25" rows="8" name="message"></textarea></td>

</tr>

</table>

<br>

<input type="reset" value="Reset"> &nbsp; <input type="submit" value="submit">

</form>

</body>

</html>

Output

JSP Mail

 

JSP Mail

Pr.Pg border                                              Next Pg