search
HomeDatabaseMysql Tutorial使用JSP开发WebMail系统_MySQL

WebMail

   电子邮件(E-mail)是Internet上使用最广泛的服务之一,传统的Email应用模式基于C/S结构,即用户使用客户端的邮件收发工具(如Outlook、Foxmail等)与提供邮件服务的服务器(如163.net、263.net、371.net)通信,在使用客户端邮件工具之前,用户要进行一些必要的设置,如指定邮件服务器的主机地址和通信端口等,这些工作对刚开始上网的用户会有一定的困难,如果把E-mail和Web结合在一起,即通过Web编程和适当的系统设置,使用户仅仅以访问Web的方式就可以得到和使用完整的邮件服务,这样将极大地方便上网用户,这种系统称为WebMail。WebMail是目前Internet上最受欢迎的服务之一,也是很多网站必备功能之一。另外WebMail同样也适用于企业或校园网的应用。

通常在后台服务器的搭建和设置完成后实现WebMail系统,而前台的开发工作主要是开发工具与后台数据库和邮件服务器的交互问题。在Linux平台上运行的各种服务器软件稳定性和可靠性一直很好,而且选择跨平台的Java开发工具使系统更稳定,具有更高的伸缩性。

JSP性能


尽管JSP提供强大的功能是建立在Servlet之上,但JSP的性能和Servlet相差无几。JSP首先要编译成Servlet,这只会增加少量的代码,仅需编译一次且可以预编译,这就消除了运行时花费不必要的负担。JSP与Servlet性能上的差异仅仅表现在返回的数据是二进制的。这是因为JSP返回时用的是PrintWriter,而Servlet可以应用于速度更快的OutputStream。

JSP自定义的标签库可以封装大量的、复杂的Java操作在一个Form里面,这些预先定义好的标签可以很容易的被那些没有Java知识的人调用。因此,JSP自定义的标签库可以有效地实现Java程序员和Web设计人员工作的划分。然而,在页面上应用的每一个标签,Web容器都必须创建一个新的标签句柄对象或从标签缓冲中提取它。因此,过多的应用自定义的标签将会带来不必要的资源浪费。

BodyTags是一种特殊的定制标签,可以提取在它之间封装的内容或者替换那些内容。BodyTags之间的内容一般会备份在内存中。由于BodyTags之间能够嵌套和重复,因此,在程序中应用了多级的BodyTags会占用大量宝贵的内存和系统资源。

实现WebMail的主要功能


该系统提供了获取、阅读、书写、转发、回复、打印、删除及用户管理的功能。考虑到系统的跨平台性,采用Java及相关技术产品为开发工具,特别是采用JSP作为服务程序,这样对客户端也没有其它要求,同时系统的性能在高负荷下得到进一步提高。整个WebMail系统全部采用纯Java代码,服务器端每响应一个服务请求启动一个线程,而不像CGI那样启动一个进程。这样能够节省系统资源,提高系统性能。

实现主要代码


获取用户输入的信息

对于用户输入内容获取功能是通过getParameter方法来实现的,对于输入的文本内容,通过如下代码就能在服务器端获取,程序代码如下:

<ccid_code>String username=request.getParameter("login");
String password=request.getParameter("password");
Session session2=Session.getInstance(System.getProperties() ,null);
Store store=session2.getStore("pop3");</ccid_code>


根据用户输入的信息来连接服务器,程序代码如下:

<ccid_code>try{
 store.connect(host,username+"%nyist.net", password);
}
catch(javax.mail.AuthenticationFailedException e)
{content="用户名与密码不匹配";}</ccid_code>


接收邮件代码段

根据获取用户输入的信息来连接服务器,代码为:

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


获取服务器端的信息,代码如下:

<ccid_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);</ccid_code>


根据服务器上信息的不同格式,使用不同的方式来读取:

<ccid_code>String contentbody="";
Object o=message[j].getContent();</ccid_code>


若其Type为tex/plain就可直接读出,代码如下:

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


如果信息类型为text/html,不同的信息类型处理的方式稍有不同(如下段代码),由于篇幅有限不再一一说明。

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


发送邮件代码段

根据用户输入的内容,获取邮件头信息代码如下:

<ccid_code>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();
//设置邮件服务
props.put("mail.smtp.host", host);
Session session2 =Session.getInstance(props, null);</ccid_code>


设置邮件头信息代码如下:

<ccid_code>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();</ccid_code>


设置邮件内容,构建程序段如下:

<ccid_code>messageBodyPart.setText(content);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);</ccid_code>


用户在发送邮件时常常带有附件,就是将浏览器客户端用户本地的文件传送到POP客户端,实现代码如下:

<ccid_code>for (int i=0;i<mysmartupload.getfiles com.jspsmart.upload.file myfile="mySmartUpload.getFiles().getFile(i);" if myfile.saveas myfile.getfilename count></mysmartupload.getfiles></ccid_code>


在上传附件的同时,对上传文件的数量进行统计,并通过out.println("上传了"+count + "个文件")将其在屏幕上显示出来。

在发送的信件中如果有附件,使用如下代码进行发送:

<ccid_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
message.setContent(multipart);</ccid_code>


调用Transport的send方法,将构造好MIME Message对象发送出去,代码如下:

<ccid_code>Transport.send(message);</ccid_code>


删除电子邮件代码段

在通过Web界面使用电子邮件过程中,经常要对接收到垃圾邮件或已查看过的邮件进行删除,这也是电子邮件中必不可少的一个功能,所以我们设计了Web界面中删除电子邮件的相应功能,主要程序代码段如下:

<ccid_code>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 message folder.close></n></ccid_code>


用户管理

在使用系统运行的过程中,通过管理界面添加用户,删除不必要的用户,修改用户的密码,这是程序运行过程中必要的模块,代码如下:

<ccid_code>//添加用户
Runtime.getRuntime().exec("/home/vpopmail/bin/vadduser"+request.getParameter("user
name")+"@nyist.net "+request.getParameter("passwd"));
//删除用户
Runtime.getRuntime().exec("/home/vpopmail/bin/vdeluser"+request.getParameter("user
name")+"@nyist.net");
//修改用户密码
Runtime.getRuntime().exec("/home/vpopmail/bin/vpasswd"+request.getParameter("usern
ame")+"@nyist.net "+request.getParameter("passwd"));</ccid_code>


总结


Java简化了企业解决方案的开发、部署和管理等相关的复杂问题,它是面向对象的编程语言,同时也是具有平台独立性、高性能的服务器端编程语言。它提供的标准系统框架和服务适合团体开发,可控制性好,与其它资源的集成性好。采用Java为编程工具开发高性能、高可用性的WebMail服务器具有非常重要的意义。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.