Develop Webmail system using JSP
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;
Connect the server 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] .ismtype ("text / place")))
{
CONTENTBODY = (String) " td>";
Stringbuffer buf = new stringbuffer (contentBody.Length () 6);
CHAR CH = '';
For (int P = 0; p
{CH = ContentBody.Charat (p);
IF (CH == '/ n') buf.append ("
");
Else Buf.Append (CH);
}
ContentBody = buf.toTString ();
}
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] .ismtype ("Text / HTML"))
CONTENTBODY = (String) O " td>";
Send an email code segment
Obtaining the mail head information code 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 InternetDress (to));
Message.setSubject (Subject);
Message.setSentDate (New Date ());
// Create The Message Part
MIMEBODYPART 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 { com.jspsmart.upload.file myfile = mysmartupload.getfiles (). getfile (i); IF (! myfile.ismissing ()) { myfile.saveas ("/ upload /" myfile.getfilename ()); COUNT ; } 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 ("File" i); Multipart.AddBodypart (MessageBodypart); } // Put Parts in Message Message.setContent (Multipart); Calling the Transport's Send method, sending the configuration 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.getMessages (); String msg [] = Request.getParameterValues ("msg"); For (int i = 0, n = msg.Length; i Message [Double.Valueof (MSG [I]). INTVALUE ()]. setflag (flash.flag.deleted, true); Folder.close (True); 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 ("User Name ") " @ nyist.net " Request.getParameter (" passwd ")); //delete users Runtime.Getruntime (). EXEC ("/ Home / vpopmail / bin / vdeluser" Request.GetParameter ("User Name ") " @ nyist.net "); / / Modify user password Runtime.getRuntime (). EXEC ("/ Home / vpopmail / bin / vpasswd" Request.GetParameter ("Usern AME ") " @ nyist.net " Request.getParameter (" passwd ")); to sum up Java simplifies the development, deployment and management of business solutions, is a complex issue of object-oriented programming languages, and is also a platform independent, high-performance server-side programming language. It offers standard system frameworks and services suitable for group development, good control, and good integration with other resources. It is very important to develop high-performance, high-availability Webmail servers with Java.