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.