Using JSP to develop webmail system

Source: Internet
Author: User
Tags count header mail mail code connect client webmail server stringbuffer
Js|web



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 163.net, 263.net, 371.net) 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 





Gets the 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: 





ffffff cellpadding=2 width=550 align=center bordercolorlight=black
 
   String username=request.getparameter ("login"); String password=request.getparameter ("password"); Session session2=session.getinstance (System.getproperties (), NULL); Store Store=session2.getstore ("POP3");







to connect to the server based on the information entered 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 does not match";}







receive mail code snippets 





to connect to the server based on obtaining information entered by the user, the code is: 





 
     Store.connect ("Nyist.net", -1,request.getparameter ("username") + "%nyist.net", Request.getparameter ("password"));







get the server-side information, 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);







is read in different ways depending on the format of the information on the server: 





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







If the type is Tex/plain 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 you encounter a line change to <br> {Ch=contentbody.charat (p);  if (ch== ' \ n ') buf.append ("<br>");  else  buf.append (CH);} contentbody=buf.tostring ();}







If the type of information is text/html, different types of information are handled slightly differently (as in the following code) and are no longer one by one descriptive due to space limitations. 





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







Send mail code snippet 





according to the user input, get the message header 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 Mail Service props.put ("Mail.smtp.host", host); Session Session2 =session.getinstance (props, null);







set the message 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 up the message content, build the program paragraph as follows: 





 
            Messagebodypart.settext (content); Multipart Multipart = new Mimemultipart (); Multipart.addbodypart (Messagebodypart);







users often send messages with attachments, that is, the browser client user local files to the POP client, implementation code 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 + +;}







Upload the attachment at the same time, the number of uploaded files to the statistics, and through the Out.println ("uploaded" +count + "file") to display it on the screen. 





If there is an attachment in the sent letter, 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);







calls the Send method of transport, which sends the MIME message object to be constructed with the following code: 





 
               transport.send (message);


Delete e-mail snippet 

When you use e-mail through the Web interface, you often delete spam or viewed messages, which is an essential feature of e-mail. So we designed the Web interface to delete the corresponding function of e-mail, the main program code snippet is as follows: 

 
                Folder folder=store.getfolder ("in Box "); 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 admin interface, delete unnecessary users, modify the user's password, this is the necessary module in the program operation, the code is as follows: 

 
                 //Add User Runtime.getruntime (). EXEC ("/home/vpopmail/bin/vadduser" +r Equest.getparameter ("username") + "@nyist. Net" +request.getparameter ("passwd");//delete User runtime.getruntime (). EXEC (" /home/vpopmail/bin/vdeluser "+request.getparameter (" username ") +" @nyist. NET ");//ModifyUser Password Runtime.getruntime (). EXEC ("/home/vpopmail/bin/vpasswd" +request.getparameter ("username") + "@nyist. Net" +
    Request.getparameter ("passwd"));


Summary 

Java simplifies development, deployment, and management of enterprise solutions Related to complex issues, it is an object-oriented programming language, but also platform-independent, High-performance server-side programming language. It provides a standard system framework and services that are suitable for group development, have good control and good integration with other resources. It is very important to develop high performance and high availability webmail server with Java for programming tools. 





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.