Book Image

Java EE 5 Development using GlassFish Application Server

By : David R Heffelfinger
Book Image

Java EE 5 Development using GlassFish Application Server

By: David R Heffelfinger

Overview of this book

<p>GlassFish is a free, open-source Java EE 5-compliant application server that is quickly gaining massive popularity.<br /><br />This book explains GlassFish installation and configuration, and then moves on to Java EE 5 application development, covering all major Java EE 5 APIs.<br /><br /><span style="font-weight: bold;">Chapter 1</span> provides an overview of Glassfish, including how to install it, configure it, and verify the installation.<br /><br /><span style="font-weight: bold;">Chapter 2</span> covers how to develop server-side web applications using the Servlet API. &nbsp;<br /><br /><span style="font-weight: bold;">Chapter 3</span> explains how to develop web applications using JavaServer Pages (JSPs), including how to develop and use JSP custom tags.<br /><br /><span style="font-weight: bold;">Chapter 4</span> discusses how to develop Java EE applications that interact with a relational database system through the Java Persistence API (JPA) and through the Java Database Connectivity API (JDBC).<br /><br style="font-weight: bold;" /><span style="font-weight: bold;">Chapter 5</span> explains how to use the JSP Standard Tag Library (JSTL) when developing JavaServer Pages.<br /><br /><span style="font-weight: bold;">Chapter 6</span> covers how to develop applications using the JavaServer Faces (JSF) component framework to build web applications.<br /><br /><span style="font-weight: bold;">Chapter 7</span> explains how to develop messaging applications though the Java Messaging Service (JMS) API.<br /><br /><span style="font-weight: bold;">Chapter 8</span> covers securing J2EE applications through the Java Authentication and Authorization Service (JAAS).<br /><br /><span style="font-weight: bold;">Chapter 9</span> discusses how to develop Enterprise Java Beans that adhere to the EJB 3 specification.<br /><br /><span style="font-weight: bold;">Chapter 10</span> explains how to develop and deploy web services that conform to the JAX-WS 2.1 specification.<br /><br /><span style="font-weight: bold;">Chapter 11</span> covers frameworks that build on top of the Java EE 5 specification, including Seam, Facelets, and Ajax4Jsf.<br /><br /><span style="font-weight: bold;">The appendices</span> cover some of the advanced features of the GlassFish server.</p>
Table of Contents (18 chapters)
Java EE 5 Development using GlassFish Application Server
Credits
About the Author
About the Reviewers
Preface
IDE Integration

Implementing Email Delivery Functionality


Once we have set up a JavaMail session as described in the previous section, implementing the email delivery functionality is fairly simple. The process is illustrated in the following code example:

package net.ensode.glassfishbook;
import javax.annotation.Resource;
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 FeedbackBean
{
private String subject;
private String body;
@Resource(name = "mymailserver")
Session session;

public String sendEmail()
{
try
{
Message msg = new MimeMessage(session);
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress(
"[email protected]"));
msg.setSubject(subject);
msg.setText(body);
Transport.send(msg);
}
catch (AddressException e)
{
e.printStackTrace();
return "failure";
}
catch (MessagingException e)
{
e.printStackTrace();
return "failure";
}
return "success";
}
public String getBody()
{
return body;
}
public void setBody(String body)
{
this.body = body;
}
public String getSubject()
{
return subject;
}
public void setSubject(String subject)
{
this.subject = subject;
}
}

This class is used as a managed bean for a simple JSF application. For brevity, other parts of the application are not shown, as they do not deal with email functionality. The full application can be downloaded from this book's website.

The first thing we need to do is inject an instance of the JavaMail session created as described in the previous section by adding a class-level variable of type javax.mail.Session and decorating it with the @Resource annotation. The value of the name attribute of this annotation must match the JNDI name we gave our JavaMail session when it was created.

We then need to create an instance of javax.mail.internet.MimeMessage, passing the session object as a parameter to its constructor.

Once we create an instance of javax.mail.internet.MimeMessage, we need to add a message recipient by invoking its setRecipient() method. The first parameter for this method indicates if the recipient is to be sent the message (TO), carbon copied (CC), or Blind Carbon Copied (BCC). We can indicate the type of recipient by using Message.RecipientType.TO, Message.RecipientType.CC, or Message.RecipientType.BCC as appropriate. The second parameter to the setRecipient() method indicates the email address of the recipient. This parameter is of type javax.mail.Address. This class is an abstract class, therefore we need to used one of its subclasses, specifically javax.mail.internet.InternetAddress. The constructor for this class takes a String parameter containing the email address of the recipient. The setRecipient() method can be invoked multiple times to add recipients to be sent, copied, or blind copied the message. Only a single address can be specified for each recipient type.

If we need to send a message to multiple recipients, we can use the addRecipients() method of the javax.mail.Message class (or one of its subclasses, like javax.mail.internet.MimeMessage). This method takes the recipient type as its first parameter, and an array of javax.mail.Address as its second parameter. The message will be sent to all recipients in the array. By using this method instead of setRecipient(), we are not limited to a single recipient per recipient type.

Once we have specified the recipient or recipients, we need to add the message subject and text by invoking the setSubject() and setText() methods on the message instance, respectively.

Once we have set the message subject and text, we are ready to send it. This can be accomplished by invoking the static send() method on the javax.mail.Transport class. This method takes the message instance as a parameter.