Danh mục

Java Server Pages: A Code-Intensive Premium Reference- P17

Số trang: 10      Loại file: pdf      Dung lượng: 278.35 KB      Lượt xem: 9      Lượt tải: 0    
10.10.2023

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Java Server Pages: A Code-Intensive Premium Reference- P17:Before you begin reading Pure JSP Java Server Pages, you might want to take a look at its basicstructure. This should help you outline your reading plan if you choose not to read the text from cover tocover. This introduction gives you an overview of what each chapter covers.
Nội dung trích xuất từ tài liệu:
Java Server Pages: A Code-Intensive Premium Reference- P17Configuring JavaMailBefore we get started, you probably want to know what you will need in order to use the JavaMail API, andwhere to get it. The JavaMail API can be downloaded fromhttp://www.javasoft.com/products/javamail/index.htmlThe archive you will get contains the JavaMail API jar file, all of the javadoc files, the JavaMailSpecification in PDF format, the guide for service providers in PDF format, and a decent collection ofdemo code with documentation.JavaMail makes extensive use of the JavaBeans Activation Framework (JAF). So, you will also need todownload this Java extension. It can be found athttp://www.javasoft.com/beans/glasgow/jaf.htmlThis archive contains a collection of files similar to the JavaMail archive. The two important files you willneed are the mail.jar and activation.jar files. Both of these archives must be added to yourclasspath before you can begin working with JavaMail.A JavaMail ExampleLets walk through a simple example of sending a message using JavaMail. Note The JavaMail API provides an interface to perform many more complex tasks, including sending MIME-encoded attachments to your mail. And, as we discussed earlier, you can retrieve and manipulate messages from your mailboxes. The demo code that accompanies JavaMail gives good examples of some of the other features that you can use. With a little creativity, you are not limited to what you can accomplish.Listing 17.1 contains our JavaMail example.Listing 17.1: SimpleSendMessage.javaimport java.util.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;public class SimpleSendMessage { public static void main(String[] args) { // Collect the necessary information to send a simple message // Make sure to replace the values for host, to, and from with // valid information. // host - must be a valid smtp server that you currently have // access to. - 161 - // to - whoever is going to get your email // from - whoever you want to be. Just remember that many smtp // servers will validate the domain of the from address // before allowing the mail to be sent. String host = server.myhost.com; String to = YourFriend@somewhere.com; String from = MeMeMe@myhost.com; String subject = JSP Rules!; String messageText = I am sending a message using the + JavaMail API. + I can include any text that I want.; boolean sessionDebug = false; // Create some properties and get the default Session. Properties props = System.getProperties(); props.put(mail.host, host); props.put(mail.transport.protocol, smtp); Session session = Session.getDefaultInstance(props, null); // Set debug on the Session so we can see what is going on // Passing false will not echo debug info, and passing true // will. session.setDebug(sessionDebug); try { // Instantiate a new MimeMessage and fill it with the // required information. Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(subject); msg.setSentDate(new Date()); msg.setText(messageText); // Hand the message to the default transport service // for delivery. Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); - 162 - } }}In analyzing Listing 17.1, the first topic we must discuss is the Session class. The Session represents amail session and is typically the first thing that you will set up in code using the JavaMail API. It collectsproperties and defaults that will be used by different pieces throughout the API.In the following code snippet, we retrieve the system properties, add the JavaMail–specific information tothem, and retrieve a default Session using them. The properties we use here are just some of theJavaMail–specific attributes that can be used; however, they are the only ones necessary to accomplishsending a simple message:String host = server.myhost.com;String to = YourFriend@somewhere.com;String from = MeMeMe@myhost.com;String subject = JSP Rules!;String messageText = I am sending a message using the + JavaMail API. I can include any text that I want.; boolean sessionDebug = false;// Create some properties and get the default Session.Properties props = System.getProperties();props.put(mail.host, host);props.put(mail.transport.protocol, smtp);Session session = Session.getDefaultInstance(props, null);The mail.host environment property specifies the default mail server. In many cases the server fortransport and store are the same machine. However, th ...

Tài liệu được xem nhiều: