Web Development with JavaServer Pages

Скачать в pdf «Web Development with JavaServer Pages»

For use in a JSP error page, however, sending a plain text message is sufficient. To this end, the sendEmaii() method is defined as follows:

public void sendEmail (String mailServer, String subject,

String to[], String from, String messageText) throws AddressException, MessagingException {

// Create session

Properties mailProps = new Properties(); mailProps.put(«mail.smtp.host», mailServer);

Session mailSession = Session.getDefaultInstance(mailProps, null);

// Construct addresses int toCount = to.length;

InternetAddress[] toAddrs = new InternetAddress[toCount]; for (int i = 0; i < toCount; ++i)    {

toAddrs[i] = new InternetAddress(to[i]);


InternetAddress fromAddr = new InternetAddress(from);

// Create and initialize message

Message message = new MimeMessage(mailSession);


message.setRecipients(Message.RecipientType.TO, toAddrs); message.setSubject(subject);

message.setContent(messageText.toString(), «text/plain»);

// Send message Transport.send(message);

All of the arguments to this method are instances of the Java String class, with the exception of the to argument, representing the intended recipients, which is an array of strings. The maiiserver parameter is the name of a network host running an SMTP server that will handle the actual sending of the message. The subject argument represents the subject line for the message. The from parameter identifies the email address from which the message is being sent. The validity of this return address may or may not be confirmed, depending upon how the SMTP server has been configured. The final argument, messageText, should be a string containing the text to be sent as the body of the email message.

A central concept of the JavaMail API is that of a mail session, representing a set of interactions with a mail server. Mail sessions are represented by an instance of the javax.mail.Session class, which is initialized from an instance of the java.util.Properties class. For our purposes here, the only information that needs to be in the property list for this mail session is the identity of the SMTP host, as indicated by a property named mail.smtp.host. The next step is to convert the email addresses passed in as String values via the to and from arguments into instances of the javax.mail.internet.InternetAddress class. Next, an instance of the javax.mail.Message class is constructed. This is an abstract class, however, so the actual object created is an instance of the javax.mail.internet.MimeMes-sage class, whose constructor takes a MailSession instance as its sole argument. The properties of this message are then set to identify the sender, subject, recipients, and body. Note that in the call to setContent () the MIME type of the message body is set to «text/plain», indicating that the text of the message is standard ASCII text. Finally, the static send() method of the javax.mail.Transport class is called to actually deliver the message.

Скачать в pdf «Web Development with JavaServer Pages»