Use JSP to develop a WebMail System

Source: Internet
Author: User
Tags mail code 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 mail sending and receiving tools (such as Outlook, Foxmail, etc.) and the server providing the mail service. (Such as 163.net, 263.net, 371.net) communication, before using the client mail tool, the user must make some necessary settings, such as specifying the host address and communication port of the mail server. Users will have certain difficulties. If E-mail is combined with the Web, that is, through Web programming and appropriate system settings, users can get and use complete mail services just by accessing the Web. The earth is convenient for Internet users. This system is called WebMail. WebMail is currently one of the most popular services on the Internet and one of the essential functions of many websites. In addition, WebMail is also suitable for corporate or campus network applications. [@ more @]
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 mail sending and receiving tools (such as Outlook, Foxmail, etc.) and the server providing the mail service. (Such as 163.net, 263.net, 371.net) communication, before using the client mail tool, the user must make some necessary settings, such as specifying the host address and communication port of the mail server. Users will have certain difficulties. If E-mail is combined with the Web, that is, through Web programming and appropriate system settings, users can get and use complete mail services just by accessing the Web. The earth is convenient for Internet users. This system is called WebMail. WebMail is currently one of the most popular services on the Internet and one of the essential functions of many websites. In addition, WebMail is also suitable for corporate or campus network applications.

The WebMail system is usually implemented after the background server is set up and set up, and the front-end development work is mainly the interaction between the development tools and the background database and mail server. The stability and reliability of various server software running on the Linux platform has been very good, and the choice of cross-platform Java development tools has made the system more stable and highly scalable.

JSP performance
Although JSP provides powerful functions based on Servlet, the performance of JSP is similar to Servlet. JSP must first be compiled into a servlet, which will only add a small amount of code. It only needs to be compiled once and can be precompiled, which eliminates unnecessary burden on runtime. The difference in performance between JSP and Servlet is only reflected in the binary data returned. This is because PrintWriter is used when JSP returns, and Servlet can be applied to the faster OutputStream.

JSP custom tag library can encapsulate a large number of complex Java operations in a Form, these predefined tags can be easily called by those who do not have Java knowledge. Therefore, JSP custom tag library can effectively achieve the division of work between Java programmers and Web designers. However, for every tag applied on the page, the web container must create a new tag handle object or extract it from the tag buffer. Therefore, excessive application of custom tags will cause unnecessary waste of resources.

BodyTags is a special custom tag that can extract the content encapsulated between it or replace those content. The content between BodyTags is generally backed up in memory. Because BodyTags can be nested and repeated, applying multiple levels of BodyTags in the program will consume a lot of valuable memory and system resources.

Realize the main functions of WebMail

The system provides functions of obtaining, reading, writing, forwarding, replying, printing, deleting, and user management. Considering the cross-platform nature of the system, Java and related technology products are used as development tools, especially JSP is used as the service program, so there are no other requirements for the client, and the system performance is further improved under high load. The entire WebMail system uses pure Java code, and the server starts a thread every time it responds to a service request, instead of starting a process like CGI. This can save system resources and improve system performance.

Implement the main code

Get information entered by the user

The user input content acquisition function is implemented by the getParameter method. For the input text content, it can be obtained on the server side through the following code, 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");


连接 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 does not match password";}


Receive mail code snippet

连接 Connect to the server according to the information entered by the user, the code is:


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

.getParameter ("password"));


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


读取 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) + "";

StringBuffer buf = new StringBuffer (contentbody.length () + 6);

char ch =;

for (int p = 0; p

{ch = contentbody.charAt (p);

if (ch ==) buf.append ("
");

else buf.append (ch);

}

contentbody = buf.toString ();

}


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


else if (message [j] .isMimeType ("text / html"))

contentbody = (String) o + "";


Send mail snippet

According to the content entered by the user, the code for obtaining the mail 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 up mail service

props.put ("mail.smtp.host", 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 part

MimeBodyPart messageBodyPart = new MimeBodyPart ();


Set the email content, and build the program as follows:


messageBodyPart.setText (content);

Multipart multipart = new MimeMultipart ();

multipart.addBodyPart (messageBodyPart);


When users send emails, they often have attachments, which is to transfer the local file of the browser client user to the POP client. The implementation code is as follows:


for (int i = 0; i

{

com.jspsmart.upload.File myFile = mySmartUpload.getFiles (). getFile (i);

if (! myFile.isMissing ()) {

myFile.saveAs ("/ upload /" + myFile.getFileName ());

count ++;

}


上传 When 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 message

message.setContent (multipart);


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


Transport.send (message);


Remove 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, so we have designed the corresponding deletion of e-mail in the web interface Function, 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

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 the user's password. This is a necessary module during the program running process, the code is as follows:


//Add user

Runtime.getRuntime (). Exec ("/ home / vpopmail / bin / vadduser" + request.getParameter ("user

name ") +" @ nyist.net "+ request.getParameter (" passwd "));

//delete users

Runtime.getRuntime (). Exec ("/ home / vpopmail / bin / vdeluser" + request.getParameter ("user

name ") +" @ nyist.net ");

// Change user password

Runtime.getRuntime (). Exec ("/ home / vpopmail / bin / vpasswd" + request.getParameter ("usern

ame ") +" @ nyist.net "+ request.getParameter (" passwd "));


  to sum up

Java simplifies the development, deployment, and management of complex solutions related to 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.

From "ITPUB Blog", link: http://blog.itpub.net/10294935/viewspace-922313/, if you need to reprint, please indicate the source, otherwise legal responsibility will be investigated.


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.