E-mail is one of the most wide services on the Internet, and traditional EMAIL application modes are based on C / S structures, that is, user-friendly mail transceiver tools (such as Outlook, Foxmail, etc.) and servers. (Such as 163.NET, 263.NET, 371.NET) communication, before using the client mail tool, the user wants some necessary settings, such as the host address and communication port of the specified mail server, etc. Users will have certain difficulties. If you combine E-mail and web, you can get and use the full mail service with the user's way to access the web through web programming and appropriate system settings, so that Easy convenient Internet users, this system is called Webmail. Webmail is one of the most popular services on the Internet, and is also one of the necessary features of many websites. In addition, WebMail is also applicable to the application of enterprises or campus networks.
The Webmail system is usually implemented after the construction and setting of the background server, while the development of the front desk is mainly the interaction between the development tool and the background database and the mail server. The stability and reliability of various server software running on the Linux platform have always been good, and the Java development tools across the platform make the system more stable and have higher scalability.
JSP performance
Although JSP provides powerful features to be built on servlet, the performance of JSP is different from servlets. JSP first compiles to servlet, which only increases a small amount of code, just compiles once and can be precompiled, which eliminates the unnecessary burden spent at runtime. The difference in the performance of JSP and Servlet is only manifested in the returned data is binary. This is because JSP returns to PrintWrit, while servlets can be applied to faster OutputStream.
JSP custom label libraries can package large numbers, complex Java operations inside an Form, which can be easily called by those without Java knowledge. Therefore, JSP custom label libraries can effectively implement the Java programmers and WEB designers' work division. However, every tab applied on the page, both the web container must create a new label handle object or extract it from the label buffer. Therefore, excessive application custom labels will bring unnecessary resource waste.
BodyTags is a special custom label that extracts content encapsulated between it or replaces those content. The content between the bodytags generally backed up in memory. Since the bodyTags can nearest and repeat, multi-level BodyTags are applied in the program to occupy a lot of valuable memory and system resources.
Realize the main features of Webmail
The system provides access, reading, writing, forwarding, replying, printing, deleting, and user management. Considering the cross-platform sex of the system, Java and related technical products are used for development tools, especially JSP as a service program, so that there is no other requirement for the client, and the performance of the system is further improved under high load. All the entire WebMail system uses pure Java code, and the server side starts a thread every response with a service request, and does not start a process like CGI. This saves system resources to improve system performance.
Implement the main code
Get information input by user
For the user input content acquisition function is implemented by the getParameter method, for the input text content, by the following code, the server code is as follows:
String username = request.getParameter ( "login"); String password = request.getParameter ( "password"); Session session2 = Session.getInstance (System.getProperties (), null); Store store = session2.getStore ( "pop3" ); According to the information input by the user, the program code is as follows:
Try {store.connect (host, username "% nyist.net", password);} catch (javax.mail.authenticationfailedException e) {content = "username and password do not match";
Receive mail code segment
Connect the server according to the information entered by the user, the code is:
Store.connect ("NYIST.NET", - 1, Request.GetParameter ("UserName") "% NYIST.NET", Request.GetParameter ("password"));
Get information on the server side, the code is as follows:
Folder folder = store.getFolder ( "INBOX"); Folder.open (Folder.READ_WRITE); Message message [] = folder.getMessages (); FetchProfile fp = new FetchProfile (); fp.add (FetchProfile.Item.ENVELOPE) fp.add (FetchProfile.Item.flags); fp.add ("x-mailer"); Folder.Fetch (Message, FP);
According to the different formats of information on the server, use different ways to read:
String contentBody = ""; object o = message [j] .getcontent ();
If the type of Type is TEX / Plain, you can read it directly, the code is as follows:
IF (Message [J])) {contentBody = (String) " td>"; StringBuffer BUF = New StringBuffer (ContentBody.Length () 6); char CH = ' ; For (int P = 0; p
"); else buf.append (ch);} contentBody = buf.tostring ();
If the information type is Text / HTML, the different information type processing is slightly different (as shown in the following code), since the space is limited, it is no longer hereby.
Else IF (Message [J])) ContentBody = (String) O " TD>";
Send an email code segment
Depending on the content input by the user, the message header code is obtained as follows: string host = "nyist.net"; string from = request.getParameter ("from"); string to = request.getParameter ("to"); string subject = request .GetParameter ("Subject"); string content = request.getParameter ("content"); Properties PROPS = system.getProperties (); // Set the mail service Props.PUT ("mail.smtp.host", Host); session Session2 = session.getInstance (PrOPS, NULL);
Set the mail header information code as follows:
MimeMessage message = new MimeMessage (session2); message.setFrom (new InternetAddress (from)); message.addRecipient (Message.RecipientType.TO, new InternetAddress (to)); message.setSubject (subject); message.setSentDate (new Date ()); // Create the message partmimebodypart messagebodypart = new mimebodypart ();
Set the message content, the build block is as follows:
MessageBodyPart.Settext (Content); Multipart Multipart = new mimeMultipart (); Multipart.Addbodypart (MessageBodypart);
The user often has an attachment when sending an email, is transmitted to the POP client local file to the POP client, and implements the code as follows:
For (int i = 0; i At the same time, simultaneously, the number of uploaded files is statistically, and it is displayed on the screen by Out.println ("upload" count "file"). If there is an attachment in the sender, use the following code to send: For (INT i = 0; Request.GetParameter ("file" i)! = null; i ) {messagebodypart = new mimebodypart (); file file = new file ("/ home / mengyu / root / upload /", request .getParameter ( "file" i)); DataSource source = new FileDataSource (file); messageBodyPart.setDataHandler (new DataHandler (source)); messageBodyPart.setFileName (request.getParameter ( "file" i)); multipart.addBodyPart (MessageBodypart);} // put parts in messageMessage.setContent (Multipart); calling Transport's Send method, sending the MIME Message object, the code is as follows: TRANSPORT.SEND (Message); Delete email code segments During using an email through the web interface, you are often deleted to receive spam or viewed messages, which is also an essential feature in your email, so we design the corresponding web interface to delete emails. Function, the main program code segment is as follows: Folder folder = store.getfolder ("inbox"); folder.Open (Folder.Read_write); message message [] = folder.getMess (); string msg [] = Request.getParameterValues ("msg"); for (int i); for = 0, n = msg.Length; i User Management In the process of using the system run, add a user to the management interface, delete unnecessary users, modify the user's password, which is the necessary modules during the program run, the code is as follows: // Add user runtime.getRuntime (). EXEC ("/ home / vpopmail / bin / vadduser" Request.getParameter ("UserName") "@ nyist.net" Request.GetParameter ("passwd")); / / Delete the user runtime.getruntime (). EXEC ("/ home / vpopmail / bin / vdeluser" Request.getParameter ("UserName") "@ nyist.net"); // Modify user password runtime.getRuntime (). Exec ("/ Home / vpopmail / bin / vpasswd" Request.getParameter ("UserName" "@ nyist.net" Request.getParameter ("passwd")); to sum up