Using JSP to develop webmail system _jsp programming

Source: Internet
Author: User
Tags webmail server stringbuffer
E-Mail is one of the most widely used services on the Internet, the traditional email application mode is based on C/s structure, that is, the user uses the client's Mail transceiver tool (such as Outlook, Foxmail, etc.) and the server that provides the mail service (such as,, communication, before using the Client Mail tool, users need to make some necessary settings, such as specifying the host address and communication port of the mail server, these jobs will have some difficulties for the users who have just started to surf the internet, if the e-mail and the web are combined, That is, through web programming and appropriate system settings, users can get and use the full mail service just by accessing the web, which is a great convenience for users of the Internet, a system called webmail. Webmail is one of the most popular services on the internet and one of the most essential features of many Web sites. In addition webmail also applies to the application of enterprise or campus network.

Usually after the establishment of the background server and the completion of the implementation of the webmail system, and the development of the front desk is mainly the development of tools and background database and mail server interaction problem. The stability and reliability of the various server software running on the Linux platform has been good, and the choice of Cross-platform Java Development Tools makes the system more stable and scalable.

JSP performance

Although JSP provides a powerful feature built on top of the servlet, the performance of the JSP is comparable to that of the servlet. JSP is first compiled into a servlet, which will only add a small amount of code, only need to compile once and can be precompiled, which eliminates the unnecessary burden of running. The difference between JSP and servlet performance is only reflected in the binary of the returned data. This is because the JSP returns with PrintWriter, and the servlet can be applied to faster outputstream.

JSP custom tag libraries can encapsulate a large number of complex Java operations in a form where predefined tags can be easily invoked by people who do not have Java knowledge. Therefore, the JSP custom label library can effectively achieve the Java Programmer and Web designer work division. However, for each label applied on the page, the Web container must create a new label handle object or extract it from the tag buffer. As a result, excessive application of custom labels will result in unnecessary waste of resources.

Bodytags is a special custom label that can extract or replace content that is encapsulated between it. The content between bodytags is typically backed up in memory. Because of the ability to nest and repeat between Bodytags, the application of multi-level bodytags in a program consumes a significant amount of valuable memory and system resources.

Realize the main function of webmail

This system provides the function of acquiring, reading, writing, forwarding, replying, printing, deleting and user management. Considering the cross-platform nature of the system, the use of Java and related technology products for development tools, especially the use of JSP as a service program, so there is no other requirements on the client, at the same time the performance of the system under the high load has been further improved. The entire webmail system uses pure Java code, which starts a thread each response to a service request instead of starting a process like CGI. This can save system resources and improve system performance.

Implementing the main code

get information entered by the user

For user input content acquisition function is achieved through the GetParameter method, for the input text content, through the following code can be obtained on the server side, the program 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 entered by the user to connect to the server, the program code is as follows:

try {store.connect (host, username + "%", password);} catch (javax.mail.AuthenticationFailedException e) {content = "username and password do not match";}

Receive mail snippet

Connect to the server according to the information entered by the user. The code is:

store.connect ("",-1, request.getParameter ("username") + "%", request.getParameter ("password"));

Get the server-side information, the code is as follows:

Folder folder = store.getFolder ("INBOX"); (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);

Read in different ways depending on the format of the information on the server:

String contentbody = ""; Object o = message [j] .getContent ();

If its Type is tex / plain, it can be read directly. The code is as follows:

if (message [j] .isMimeType ("text / plain")) {contentbody = (String) + "</ td>"; StringBuffer buf = new StringBuffer (contentbody.length () + 6); char ch = '' ; for (int p = 0; p <contentbody.length (); p ++) // If it encounters a newline, it will be converted to <br> (ch = contentbody.charAt (p); if (ch == '\ n') buf.append ("<br>"); else buf.append (ch);} contentbody = buf.toString ();}

If the information type is text / html, different information types are handled slightly differently (as in the following code), and will not be explained one by one due to space limitations.

else if (message [j] .isMimeType ("text / html")) contentbody = (String) o + "</ td>";

Send mail snippet

According to the content entered by the user, the code for obtaining the header information is as follows:

String host = ""; 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 ("", host); Session session2 = Session.getInstance (props, null);

Set the message header 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 up the email content and build the program as follows:

messageBodyPart.setText (content); Multipart multipart = new MimeMultipart (); multipart.addBodyPart (messageBodyPart);

Users often send attachments when sending emails, that is, the files that are local to the browser client user are transferred to the POP client. The implementation code is as follows:

for (int i = 0; i <mySmartUpload.getFiles (). getCount (); i ++) {com.jspsmart.upload.File myFile = mySmartUpload.getFiles (). getFile (i); if (! myFile.isMissing () ) {myFile.saveAs ("/ upload /" + myFile.getFileName ()); count ++;}

While uploading attachments, count the number of uploaded files and display them on the screen through out.println ("uploaded" + count + "files").

If there is an attachment in the sent letter, use the following code to send it:

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);

Call the Send method of Transport to send the constructed MIME Message object, the code is as follows:

Transport.send (message);

Delete email snippet

In the process of using e-mail through the web interface, it is often necessary to delete the received spam or viewed emails. This is also an essential feature in e-mail. Function, the main program code segment is as follows:

Folder folder = store.getFolder ("INBOX"); (Folder.READ_WRITE); Message message [] = folder.getMessages (); String msg [] = request.getParameterValues ("msg"); for (int i = 0, n = msg.length; i <n; i ++) message [Double.valueOf (msg [i]). IntValue ()]. SetFlag (Flags.Flag.DELETED, true); folder.close (true);

User Management

In the process of using the system, add users through the management interface, delete unnecessary users, and modify user passwords. This is a necessary module during the program running process, and the code is as follows:

// Add user Runtime.getRuntime (). Exec ("/ home / vpopmail / bin / vadduser" + request.getParameter ("username") + "@" + request.getParameter ("passwd")); / / Delete user Runtime.getRuntime (). Exec ("/ home / vpopmail / bin / vdeluser" + request.getParameter ("username") + "@"); // Modify user password Runtime.getRuntime (). exec ("/ home / vpopmail / bin / vpasswd" + request.getParameter ("username") + "@" + request.getParameter ("passwd"));

to sum up

Java simplifies complex problems related to the development, deployment, and management of enterprise solutions. It is an object-oriented programming language and a platform-independent, high-performance server-side programming language. It provides a standard system framework and services suitable for group development, good controllability, and good integration with other resources. It is of great significance to use Java as a programming tool to develop a high-performance, high-availability WebMail server.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.