servlet代码如下所示:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.net.URLEncoder;
public class SendMail extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public void init() throws ServletException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
request.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
//取得接收邮件的地址
String to = request.getParameter("to");
if(to.trim().length() == 0){
response.sendRedirect("MessageShow.jsp?message=" + "接收邮件的地址不能为空.");
return;
}
//取得发送邮件的地址
String from = request.getParameter("from");
if(from.trim().length() == 0){
response.sendRedirect("MessageShow.jsp?message=" + "发送邮件的地址不能为空.");
return;
}
//取得SMTP邮件服务器的地址
String host = request.getParameter("host");
if(host.trim().length() == 0){
response.sendRedirect("MessageShow.jsp?message=" + "SMTP邮件服务器不能为空.");
return;
}
//联接SMTP邮件服务器是否需要输入用户名和密码
boolean auth = Boolean.valueOf(request.getParameter("auth")).booleanValue();
//取得邮件的内容
String msgText = request.getParameter("msgText");
if(msgText.trim().length() == 0){
response.sendRedirect("MessageShow.jsp?message=" + "邮件的内容不能为空.");
return;
}
//取得邮件的标题
String subject = request.getParameter("subject");
if(subject.trim().length() == 0){
response.sendRedirect("MessageShow.jsp?message=" + "邮件的标题不能为空.");
return;
}
//取得联接SMTP服务器的用户名
String username = request.getParameter("username");
//取得联接SMTP服务器的密码
String password = request.getParameter("password");
//在服务器显示发送邮件信息
boolean debug = true;
//创建邮件服务器的属性类
Properties props = new Properties();
//加入SMTP服务器的名字
props.put("mail.smtp.host", host);
//加入联接SMTP服务器是否需要用户检证的变量
props.put("mail.smtp.auth", String.valueOf(auth));
//加入是否显示发送邮件时信息的变量
if (debug) props.put("mail.debug", String.valueOf(debug));
//创建发送邮件进程
Session session = Session.getInstance(props, null);
//定义是否显示发送邮件时的信息
session.setDebug(debug);
try {
//创建邮件发送包
Message msg = new MimeMessage(session);
//设置邮件发送包的发送地址
msg.setFrom(new InternetAddress(from));
//创建邮件接收地址类
InternetAddress[] address = {new InternetAddress(to)};
//设置邮件发送包的接收地址
msg.setRecipients(Message.RecipientType.TO, address);
//设置邮件主题
msg.setSubject(subject);
//设置邮件的发送时间
msg.setSentDate(new Date());
//设置邮件的内容
msg.setText(msgText);
//联接SMTP服务器时需要输入用户名和密码的执行代码
if(auth){
Transport transport = session.getTransport("smtp");
//使用用户名和密码联接SMTP服务器
transport.connect(host, username, password);
//发送邮件
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}else{
//联接SMTP服务器时不需要输入用户名和密码的执行代码
Transport.send(msg);
}
response.sendRedirect("MessageShow.jsp?message='" + "成功发送邮件至" + to + "信箱'");
}
catch (Exception ex) {
out.println(ex.getMessage());
return;
}
}
public void destroy() {
}
}
jsp分为两个文件:
表单填写:SendMail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>发送单个邮件示例</title>
</head>
<body>
<center>
<h3>发送单个邮件示例</h3>
<form method="post" action="SendMail">
<table cellspacing="0" cellpadding="3" border="1" width="500">
<!-- 加入编辑框 -->
<tr>
<td width="150">SMTP服务器:</td>
<td width="350"><input TYPE="text" name="host" value=""></td>
</tr>
<!-- 加入复选框 -->
<tr>
<td width="150">用户检证:</td>
<td><input TYPE="CHECKBOX" name="auth" value="true">需要</td>
</tr>
<!-- 加入编辑框 -->
<tr>
<td width="150">用户名:</td>
<td width="350"><input TYPE="text" name="username" value=""></td>
</tr>
<!-- 加入编辑框 -->
<tr>
<td width="150">密码:</td>
<td width="350"><input TYPE="password" name="password" value=""></td>
</tr>
<!-- 加入编辑框 -->
<tr>
<td width="150">接收地址(to):</td>
<td width="350"><input TYPE="text" name="to" value=""></td>
</tr>
<!-- 加入编辑框 -->
<tr>
<td width="150">发送地址(from):</td>
<td width="350"><input TYPE="text" name="from" value=""></td>
</tr>
<!-- 加入编辑框 -->
<tr>
<td width="150">标题:</td>
<td width="350"><input TYPE="text" name="subject" value=""></td>
</tr>
<!-- 加入文本框 -->
<tr>
<td width="150">内容:</td>
<td width="350"><textarea name="msgText" cols="50" rows="4" value=""></textarea></td>
</tr>
<!-- 加入提交按钮 -->
<tr>
<td colspan="2" width="500" align="center">
<input TYPE="submit" name="submit" value="发送邮件">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
显示页面:MessageShow.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>信息显示页面</title>
</head>
<body>
<center>
<%request.setCharacterEncoding("GBK");%>
<h3><%=request.getParameter("message")%></h3>
<p><a href="SendMail.jsp">继续发邮件.</a></p>
</center>
</body>
</html>
发送成功后
在ShowMessage.jsp中文显示乱码:
'????óplayto0826@sina.comá±'