Use JSP to develop WebMail system _ MySQL

Source: Internet
Author: User
Tags webmail server
E-mail is one of the most widely used services on the Internet. the traditional Email application mode is based on the CS structure, that is, the user uses the client's email sending and receiving tools (such as Outlook and Foxmail) to communicate with the server providing the mail service (such as,, before using the client's email tool, the user needs to make some necessary settings, WebMail

   E-mail is one of the most widely used services on the Internet. the traditional Email application mode is based on the C/S structure, that is, the user uses the client's email sending and receiving tools (such as Outlook and Foxmail) to communicate with the server providing the mail service (such as,, before using the client's email tool, users need to make some necessary settings, such as specifying the host address and communication port of the email server. these work will be difficult for users who just access the Internet, if we combine E-mail and Web, that is, through Web programming and appropriate system settings, users can only access the Web to obtain and use the complete mail service, this will greatly facilitate Internet users. this system is called WebMail. WebMail is one of the most popular services on the Internet and one of the essential functions of many websites. In addition, WebMail is also applicable to enterprise or campus network applications.

Generally, the WebMail system is implemented after the backend server is set up and configured. the front-end development mainly involves the interaction between development tools and backend databases and email servers. The stability and reliability of various server software running on the Linux platform have been good, and the cross-platform Java development tools have been selected to make the system more stable and more scalable.

JSP performance

Although JSP provides powerful functions built on Servlet, its performance is almost the same as that of Servlet. JSP needs to be compiled into Servlet first, which only adds a small amount of code and only needs to be compiled once and can be pre-compiled. This eliminates the unnecessary cost during running. The performance difference between JSP and Servlet is only manifested in that the returned data is binary. This is because PrintWriter is used for JSP return, and Servlet can be applied to faster OutputStream.

JSP custom tag libraries can encapsulate a large number of complex Java operations in a Form. These predefined tags can be easily called by those without Java knowledge. Therefore, the JSP custom tag library can effectively divide the work of Java programmers and Web designers. However, for each tag applied on the page, the Web container must create a new tag handle object or extract it from the tag buffer. Therefore, too many custom application tags will lead to unnecessary resource waste.

BodyTags is a special custom tag that can extract the encapsulated content or replace the content between them. The content between BodyTags is generally backed up in the memory. Because BodyTags can be nested and repeated, the application of multi-level BodyTags in the program will occupy a large amount of valuable memory and system resources.

Main WebMail functions

The system provides the functions of obtaining, reading, writing, forwarding, replying, printing, deleting, and user management. Taking into account the cross-platform nature of the system, Java and related technical products are used as development tools, especially JSP as service programs, so there is no other requirement on the client, at the same time, the system performance is further improved under high load. The whole WebMail system uses pure Java code. the server starts a thread every time it responds to a service request, rather than starting a process like CGI. This can save system resources and improve system performance.

Implementation code

Obtain user input information

The getParameter method is used to obtain user input content. the following code can be used to obtain the input text content on the server. 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");

The program code is as follows:

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

Receiving email code segment

Connect to the server based on user input. the code is:


Obtain the server information using the following code:

       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 data in different formats based on the information on the server:

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

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

{Ch = contentbody. charAt (p); if (ch = '\ n') buf. append ("
"); Else buf. append (ch);} contentbody = buf. toString ();} "; StringBuffer buf = new StringBuffer (contentbody. length () + 6); char ch =''; for (int p = 0; p
       if (message[j].isMimeType("text/plain")) { contentbody=(String)+"

If the information type is text/html, different types of information are processed in a slightly different way (the following code segment). due to limited space, it is not described here.

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

Email sending code segment

The code for obtaining the email 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 ("mail. smtp. host ", host); Session session2 = Session. getInstance (props, null );

The code for setting the mail header information is 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 Mail content. the build procedure is as follows:

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

When sending emails, users often carry attachments, that is, they send local files from the browser client to the POP client. the implementation code is as follows:

       for (int i=0;i

When uploading attachments, you can collect statistics on the number of uploaded files and display them on the screen through out. println ("uploaded" + count + "files.

If an attachment is contained 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:


Delete email code segment

When using emails through the Web interface, you often need to delete emails that have been spam or viewed. this is also an essential function in emails, therefore, we designed the corresponding function for deleting emails on the Web interface. the main program code segment is as follows:

       Folder folder=store.getFolder("INBOX");;Message message[]=folder.getMessages();String msg[]=request.getParameterValues("msg");for(int i=0,n=msg.length;i

User Management

In the process of running the system, add users through the management interface, delete unnecessary users, and modify the user password. this is a necessary module in the process of running the program. The code is as follows:

// Login user runtime.getruntime(cmd.exe c ("/home/vpopmail/bin/vadduser" + request. getParameter ("username") + "@" + request. getParameter ("passwd"); // delete the user runtime.getruntime(cmd.exe c ("/home/vpopmail/bin/vdeluser" + request. getParameter ("username") + "@"); // modify the user's password runtime.getruntime(cmd.exe c ("/home/vpopmail/bin/vpasswd" + request. getParameter ("username") + "@" + request. getParameter ("passwd "));


Java simplifies the development, deployment, and management of enterprise solutions. it is an object-oriented programming language and also a server-side programming language with platform independence and high performance. It provides standard system frameworks and services suitable for group development, with good control and integration with other resources. Using Java as a programming tool to develop a high-performance and high-availability WebMail server is of great significance.

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.