Pr.Pg Next Pg

JavaMail API tutorials

  • You will need JavaMail API installed on your machine for sending mail from servlet 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.

 

 

Example: Servlet program for sending Email

Sam Sir

//Program name EmailServlet

// Program for sending Email

 

import java.io.*;

import java.net.*;

import java.util.Properties;

import javax.mail.AuthenticationFailedException;

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

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;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class EmailServlet extends HttpServlet {

 

protected void processRequest(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException {

 

final String err = "/error.html";

final String succ = "/success.html";

 

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 session = Session.getInstance(props, auth);

 

MimeMessage msg = new MimeMessage(session);

msg.setText(message);

msg.setSubject(subject);

msg.setFrom(new InternetAddress(from));

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

Transport.send(msg);

 

} catch (AuthenticationFailedException ex) {

request.setAttribute("ErrorMessage", "Authentication failed");

 

RequestDispatcher dispatcher = request.getRequestDispatcher(err);

dispatcher.forward(request, response);

 

} catch (AddressException ex) {

request.setAttribute("ErrorMessage", "Wrong email address");

 

RequestDispatcher dispatcher = request.getRequestDispatcher(err);

dispatcher.forward(request, response);

 

} catch (MessagingException ex) {

request.setAttribute("ErrorMessage", ex.getMessage());

 

RequestDispatcher dispatcher = request.getRequestDispatcher(err);

dispatcher.forward(request, response);

}

RequestDispatcher dispatcher = request.getRequestDispatcher(succ);

dispatcher.forward(request, response);

}

private class SMTPAuthenticator extends Authenticator {

private PasswordAuthentication authentication;

public SMTPAuthenticator(String login, String password) {

authentication = new PasswordAuthentication(login, password);

}

protected PasswordAuthentication getPasswordAuthentication() {

return authentication;

}

}

protected void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

 

protected void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

}

 

 

 

 

Error page for failed sending

Sam Sir

<!--error.html - ->

<html>

<head>

<title>Error Page</title>

<head>

<body>

<h2>Error</h2>

<p> Sending Fail</p>

</body>

</html>

 

 

 

 

Success page for mail sending

Sam Sir

<!--success.html- ->

<html>

<head>

<title>Success Page</title>

<head>

<body>

<h2>Success</h2>

<p> Mail Send</p>

</body>

</html>

 

 

 

 

HTML Program for Mail User Interface

Sam Sir

<!—MailUI.html- ->

<html>

<head>

<title>Sending email</title>

</head>

<body>

<center>

Mail Sending<BR>

<form action="EmailServlet">

<table>

<tr>

<td>From</td>

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

</tr>

<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>

<tr>

<td>Login</td>

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

</tr>

<tr>

<td>Password</td>

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

</tr>

</table>

<br>

<input type="submit" value="submit">

</form>

</center>

</body>

</html>

Output

Sending Mail using Servlets

 

Servlet Mail API

Pr.Pg border                                              Next Pg