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
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 ...
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ìm kiếm theo từ khóa liên quan:
nhập môn lập trình kỹ thuật lập trình lập trình flash lập trình web ngôn ngữ html lập trình hướng đối tượngGợi ý tài liệu liên quan:
-
Đề cương chi tiết học phần Cấu trúc dữ liệu và giải thuật (Data structures and algorithms)
10 trang 316 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 271 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 262 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 203 0 0 -
101 trang 199 1 0
-
Giới thiệu môn học Ngôn ngữ lập trình C++
5 trang 193 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 163 0 0 -
Luận văn: Nghiên cứu kỹ thuật giấu tin trong ảnh Gif
33 trang 152 0 0 -
Luận văn tốt nghiệp Công nghệ thông tin: Xây dựng website bán hàng nông sản
67 trang 138 0 0 -
Giáo trình nhập môn lập trình - Phần 22
48 trang 136 0 0