How to use JSP to develop webmail system

Source: Internet
Author: User
Tags count header interface mail mail code connect webmail server stringbuffer
Js|web e-mail (e-mail) is one of the most widely used services on the Internet, and the traditional email application mode is based on the C/s structure, that is, users use the client's mail transceiver tools (such as Outlook, Foxmail, etc.) communicate with the server that provides the mail service (such as 163.net, 263.net, 371.net), 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, etc. These jobs will be difficult for users who have just started to surf the web, and if the combination of e-mail and the Internet, that is, through web programming and proper system settings, users can get and use the full mail service just by accessing the web, which will greatly facilitate the Internet users, This system is 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");




Depending on the information entered by the user to connect to the server, 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 Message Code Snippets

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


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




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




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 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 you encounter a line break, switch 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 a message code snippet

Depending on what the user has entered, 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 ();
Setting Up mail services
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 (To) (Message.recipienttype.to,new internetaddress);
Message.setsubject (subject);
Message.setsentdate (New Date ());
Create the message part
MimeBodyPart Messagebodypart =new MimeBodyPart ();




Set up the contents of the message, build the following paragraph:


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, send it using the following code:


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
Message.setcontent (multipart);




Call the transport Send method and send the MIME message object to be constructed with the following code:


Transport.send (message);




Delete e-mail code Snippets

When using e-mail through the Web interface, you often have to delete spam or viewed messages, which is an essential feature of e-mail, so we designed the corresponding function of removing e-mail in the Web interface, the main program code snippet 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 use of the system running process, through the management interface to add users, delete unnecessary users, modify the user's password, this is the program running process of the necessary modules, code as follows:


Add user
Runtime.getruntime (). EXEC ("/home/vpopmail/bin/vadduser" +request.getparameter ("User
Name ") +" @nyist. Net "+request.getparameter (" passwd "));
Delete User
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 "));





Summarize


Java simplifies the development, deployment, and management of enterprise solutions, which are object-oriented programming languages and platform-Independent, High-performance server-side programming languages. 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.