javamailservlet.readme.txt
来自「实例大全」· 文本 代码 · 共 146 行
TXT
146 行
JavaMail Servlet ~~~~~~~~~~~~~~~~Overview:=========JavaMailServlet should not be taken as a demo of how to use the Java Servlet API. It is rather as an example of how the JavaMail APIs could be used in a server in a three-tier environment described by the following diagram: +-----------+ +-----------+ +-----------+ | IMAP | | | | | | Server |<-IMAP->| JavaMail |<-HTTP->| WWW | +-----------+ | Servlet |--HTML->| Browser | | SMTP |<-SMTP->| | | | | Server | | | | | +-----------+ +-----------+ +-----------+The JavaMailServlet supports the following functionality: * login to an IMAP server * list all the messages in the INBOX folder * view the selected message * compose and send a messageSetting up and running the demo:================================Note: These instructions explain how to compile and run the servlet demo with Java Web Server (JWS). The procedure should be similar with other web servers that support the Java Servlet API. 1. Download the latest version of the Java Web Server from http://java.sun.com/products/webserver/index.html and install according to the included documentation. Make sure JWS is listening to requests on port 80 (you may need to modify it from default port 8080; use the JWS Admin Applet). Make sure you can load the Java Web Server welcome page when you connect with your browser to the machine that the server is installed on. Also, make sure your web browser has cookie support turned on. 2. Set your classpath to include the following: * mail.jar: in the JavaMail API distribution * activation.jar: in the JAF distribution * jws.jar: in the /lib/ directory in JWS installation 3. In javamail-1.1/demo/servlet directory, compile the JavaMailServlet.java file. That produces two class files, JavaMailServlet.class and MailUserData.class. Copy these class files to the /servlets/ directory in the JWS installation. 4. Copy the mail.jar and activation.jar to the /lib/ directory in the JWS installation. 5. Copy the JavaMail.html file to the /public_html/ directory in the JWS installation. 6. Restart Java Web Server to pick up the new jar files added to its lib directory. Check again that you can load the default JWS page to verify that the server is working fine. 7. Using a web browser, go to http://<hostname>/JavaMail.html and login to a valid IMAP account. From here on, you can view messages in your INBOX and create and send new messages.JavaMailServlet Design:=======================The following is a brief description of JavaMailServlet class. Itis not intended to serve as an example of how to develop servlets;see http://java.sun.com/products/java-server/servlets/index.html for information on the Java Servlet API. You may find it usefulto refer to JavaMailServlet.java source while reading this.The JavaMailServlet uses two primary methods to process allrequests: doPost() and doGet(). doPost() processes submissionsfrom the login and compose forms. When the user logs in, thedoPost() method gets a JavaMail Session and uses the valuesof the "hostname", "username" and "password" parameters to loginto the IMAP Store and get the INBOX Folder. To preserve statebetween multiple HTTP requests, the necessary information(Session, Store, Folder, URLName) are collected in theMailUserData object which is stored using JWS's Sessiontechnology (don't confuse HttpSession and JavaMail's Session--they are different). Finally, the doPost() method outputs a table listing the INBOX and the number of messages in it.Clicking on the "INBOX" link calls the doGet() methodwhich displays a table of message headers. (See the doGet() and displayHeaders() methods.)Clicking on a message generates a request to the servlet withthe message sequence number as a parameter. The doGet() methodreceives the request and calls the displayMessage() method passing it the message sequence number to display. The displayMessage() method first lists the message headers by calling the displayMessageHeaders() utility method. For text/plain messages, the message content is then displayedas a string wrapped in HTML <pre>...</pre> tags. For multipart messages, each part is passed to the displayPart() method.There are two displayPart() methods. The one with signature displayPart(MailUserData mud, int msgNum, Part part, int partNum, HttpServletRequest req, ServletOutputStream out) is called from the displayMessage() method for each part. Forany part with text/plain content type, the content is outputas a string wrapped in HTML <pre>...</pre> tags. For othercontent types, a link representing the part is displayed,along with the part filename and description, if available.Clicking in the part link generates a request to the servletwith two parameters: the message sequence number and thepart number. The doGet() method interprets the parameters and invokes the second displayPart() method with the signature displayPart(MailUserData mud, int msgNum, int partNum, ServletOutputStream out, HttpServletResponse res) This method retrieves the specified part from the message andstreams it to the web browser, preceded by the MIME content type of this part. For example, if the part has a MIME type image/gif,the method will set the servlet response MIME content type to "image/gif" and then follow it with the bytes of the actual image. This leverages the web browser's content handlingability to display different media types.Message composition and sending is very similar to messageviewing. When the "Compose" link is clicked in the headerlistpage, the servlet outputs the HTML source for the composeform stored in the composeForm variable. The user then fillsin the destination address, subject, text, and pressessend. This sends a POST request to the servlet, which invokes the doPost() method. doPost() calls the servlet's send() method, which creates a new MimeMessage and fills it with data retrieved from the POST request. The messageis then sent to its destination using the Transport.send()static method.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?