period (.) character are contained in
the value.
■ ■ If they aren’t, the method sets the
component’s valid property to false
and it sets the error message to E-mail
address is invalid.
Then, the error message is sent to the
FacesContext instance, which associates
it to the invoking Web component.
The second method handles the logic
for sending an e-mail with JavaMail.
This navigation handling method is triggered by the action of clicking the Send
E-mail button (see Listing 3).
This is known as an action method.
It is a public method that takes no
argument and
returns a string
that corresponds
to the page that
the Web applica-
tion will navigate
to. In this case,
the method
produces and
sends an e-mail. If the e-mail transmis-
sion is successful, the method returns
g_response (for “good response”), which
displays the page g_response.xhtml in
the browser (see Figure 2). If the e-mail
transmission fails, b_response (for “bad
response”) is returned and the browser
displays the page b_response.xhtml
(see Figure 3).
In order to send an e-mail using
JavaMail, we first initiate an e-mail ses-
sion instance with the Session class. The
e-mail session is the starting point for
JavaMail. It uses the
java.util.Properties
class to get information, such as the
e-mail server, the username, and the
password, which can be shared across
the rest of the application. In this case,
we create a default instance of the
Session class:
session =
Session.getDefaultInstance(props, null);
public String submitEmail() {
// create e-mail and send
/*** Initialize variables ***/
props = new Properties();
// fill props with session and message information
session = Session.getDefaultInstance(props, null);
message = new MimeMessage(session);
try {
message.setContent( this.getDescr(), "text/plain");
message.setSubject( this.getSubject());
fromAddress = new InternetAddress( this.getFrom());
message.setFrom(fromAddress);
toAddress = new InternetAddress( this.get To());
message.setRecipient(Recipient Type. TO, toAddress);
JAVA IN ACTION
JavaMail is platform
and protocol
independent.
Second, through the session, we
produce the e-mail using the Message
class. However, considering that
Message is an abstract class, we choose
instead its subclass MimeMessage,
which allows us to create messages that
understand MIME types and headers,
as defined in the different Requests For
Comments. The message is constructed
with the session as an argument:
MimeMessage message =
new MimeMessage(session)
// Transport message
message.saveChanges(); // implicit with send()
Transport transport = session.get Transport("smtp");
transport.connect( this.smtp, this.port, this.username, this.password);
if( transport.isConnected() == false)
return "b_response";
transport.sendMessage(message, message.getAllRecipients());
transport.close();
Then, we send the e-mail by manipulating an object of type Transport. The
message is sent via the transport protocol SMTP. The transmission is handled
by the Transport class, and an object is
instantiated as follows:
}
catch (MessagingException me) {
// handle catch
return "b_response";
}
ABOUT US
return "g_response";
}
Transport transport =
session.getTransport("smtp");
Download all listings in this issue as text
Then, the transport object attempts
to connect to the SMTP server using the
suggested credentials (the SMTP server
address, the port number that accepts
SMTP connections, the username, and
the password) to pass authentication on
the server.
transport.connect( this.smtp,
this.port, this.username,
this.password);
Finally, we close the transportation
service by invoking the close command:
blog
If the SMTP server accepts the connection, the e-mail is sent via send.
transport. sendMessage(message,
message.getAllRecipients());
transport.close();