Use JSP to develop a WebMail System

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 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 163.net, 263.net, 371.net). 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 + "% nyist.net", 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:

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


Obtain the server information using the following code:

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


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:

      
       
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 ++) // convert to <br> {ch = contentbody if a line break occurs. charAt (p); if (ch = '\ n') buf. append ("<br>"); else buf. append (ch);} contentbody = buf. toString ();}
      


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+"</td>";
      


Email sending code segment

The code for obtaining the email header information is 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 );
      


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<mySmartUpload.getFiles().getCount();i++){ com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i); if (!myFile.isMissing()) { myFile.saveAs("/upload/" + myFile.getFileName());  count ++;}
      


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:

      
       Transport.send(message);
      


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");folder.open(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 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") + "@ nyist.net" + request. getParameter ("passwd"); // Delete the user runtime.getruntime(cmd.exe c ("/home/vpopmail/bin/vdeluser" + request. getParameter ("username") + "@ nyist.net"); // modify the user's password runtime.getruntime(cmd.exe c ("/home/vpopmail/bin/vpasswd" + request. getParameter ("username") + "@ nyist.net" + request. getParameter ("passwd "));
      


Summary

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.

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: info-contact@alibabacloud.com 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.