公司之前的一个项目,东山宾馆,asp写的。。。后来改为HTML,服务器为IIS
有一个在线预订房间功能,填写的信息要发到宾馆前台的邮箱。。。
由于对asp的邮件api不熟悉。。。
想到将html表单提交给JAVA WEB工程,然后调用java mail API来实现
经测试,OK
代码如下
邮箱验证
package com.cw.common;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SMTPAuthenticator extends Authenticator {
private String username; //邮箱用户名
private String password; //邮箱密码
public SMTPAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
}
}
发邮件
package com.cw.common;
import java.util.Date;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.sun.mail.smtp.SMTPTransport;
public class Email {
private static final String username = "[email protected]"; //登陆用户名:[email protected]
private static final String password = "123456"; //密码
private static final String mailhost = "202.173.255.99"; //邮件服务器
private static final String sendPerson = "[email protected]"; //邮件发送者
private static final String contentType = "text/html;charset=gb2312"; //内容类型为html格式
public static boolean send(String mailbox, String subject, String content) {
String html = "<div align=center style='font-weight:bold'>此邮件为系统自动发出,请不要回复!</div>";
html += "<div style='margin-top:10px;margin-bottom:10px'>"+content+"</div>";
Properties properties = System.getProperties(); //获得系统属性
properties.put("mail.smtp.host", Email.mailhost); //设置主机服务器名
properties.put("mail.smtp.auth", "true"); //设置是否需要认证
SMTPAuthenticator auth = new SMTPAuthenticator(Email.username, Email.password); //设置用户名和密码
Session session = Session.getInstance(properties, auth); //根据属性进行认证并获取会话连接
//设置将发送的所有内容
try {
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.connect(); //建立连接
Message message = new MimeMessage(session); //根据会话创建消息
message.setFrom(new InternetAddress(Email.sendPerson)); //设置发送者
message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailbox)); //设置接受者
message.setSubject(subject); //设置主题
message.setSentDate(new Date()); //设置发送日期
BodyPart bodyPart = new MimeBodyPart(); //使用MimeMultipart和MimeBodyPart才能发HTML格式邮件
bodyPart.setContent(html, contentType); //发送HTML格式的内容
Multipart mp = new MimeMultipart();
mp.addBodyPart(bodyPart);
message.setContent(mp);
message.saveChanges(); //保存消息的所有更改
transport.sendMessage(message, message.getAllRecipients()); //发送消息
transport.close();
return true;
} catch (AddressException e) {
System.out.println(e.toString());
} catch (MessagingException e) {
System.out.println(e.toString());
}
return false;
}
public static void main(String arg[]) {
//Email.send("[email protected]", "密码", "您的密码是:123456");
Email.send("[email protected]", "密码", "您的密码是:123456");
}
}
前端
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>东山宾馆-在线预订</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
<link href="css/css.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript">
String.prototype.trim=function(){
return this.replace(/((^\s*)|(\s*$))/gi,"");
}
function sb(){
var msg="";
var ar_datenV=document.getElementsByName("ar_daten")[0].value;
if(ar_datenV=="") msg+="\n\n请选择到达时间年份";
var ar_datemV=document.getElementsByName("ar_datem")[0].value;
if(ar_datemV=="") msg+="\n\n请选择到达时间月份";
var ar_datedV=document.getElementsByName("ar_dated")[0].value;
if(ar_datedV=="") msg+="\n\n请选择到达时间日";
var Le_datenV=document.getElementsByName("Le_daten")[0].value;
if(Le_datenV=="") msg+="\n\n请选择退房时间年份";
var Le_datemV=document.getElementsByName("Le_datem")[0].value;
if(Le_datemV=="") msg+="\n\n请选择退房时间月份";
var Le_datedV=document.getElementsByName("Le_dated")[0].value;
if(Le_datedV=="") msg+="\n\n请选择退房时间日";
var room_typeV=document.getElementsByName("room_type")[0].value;
if(room_typeV=="") msg+="\n\n请选择房间类型";
var room_numV=document.getElementsByName("room_num")[0].value;
if(room_numV=="") msg+="\n\n请填写订房数量";
var Confirm_typeV=document.getElementsByName("Confirm_type")[0].value;
if(Confirm_typeV=="") msg+="\n\n请选择确认方式";
var nameV=document.getElementsByName("name")[0].value;
if(nameV=="") msg+="\n\n请填写姓名";
var sexV=document.getElementsByName("sex")[0].value;
if(sexV=="") msg+="\n\n请选择性别";
var phoneV=document.getElementsByName("phone")[0].value;
if(phoneV=="") msg+="\n\n请填写电话号码";
var EmailV=document.getElementsByName("Email")[0].value;
if(EmailV=="") msg+="\n\n请填写Email";
var IdcardV=document.getElementsByName("Idcard")[0].value;
if(IdcardV=="") msg+="\n\n请填写证件号码";
else if(!/^\d{17}\w{1}$/.test(IdcardV)) msg+="\n\n证件号码格式错误";
if(msg.trim()=="")
document.getElementById("frm").submit();
else
window.alert(msg);
}
</script>
<style type="text/css">
<!--
.style1 {
color: #460000;
line-height: 18px;
font-size: 12px;
font-weight: bold;
}
-->
</style>
</head>
<body>
<form id="frm" method="post" action="/sendMailServlet">
<div style=" width:1000px; margin:0 auto ">
<table width="1000" height="58" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="right" valign="top" background="images/pic_01.jpg"><table height="30" border="0" cellpadding="0" cellspacing="0" style="margin-top:7px ">
<tr>
<td width="78" align="center" class="table_left"><a href="http://www.gzdongshanhotel.com/" class="topmenu">返回首页</a></td>
<td width="78" align="center" class="table_left"><span class="topmenu" style="CURSOR: hand" title="广州市东山宾馆" onclick="window.external.addFavorite('http://www.gzdongshanhotel.com/','广州市东山宾馆')">收藏本站</span></td>
<td width="78" align="center" class="table_left"><a href="http://www.gzdongshanhotel.com/eng/default.htm" class="topmenu">English</a></td>
<td width="16"> </td>
</tr>
</table></td>
</tr>
</table>
<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="248" height="95" rowspan="2"><img src="images/pic_02.jpg" width="248" height="95" alt=""></td>
<td width="670" height="75" align="center" background="images/pic_03.jpg"><table width="88%" height="18" border="0" align="center" cellpadding="3" cellspacing="0">
<tr align="center">
<td><a href="index.htm" class="menu">首页</a></td>
<td class="table_menu"><a href="jianjie.htm" class="menu">酒店简介</a></td>
<td class="table_menu"><a href="wenhua.htm" class="menu">企业文化</a></td>
<td class="table_menu"><a href="kefan.htm" class="menu">客房</a></td>
<td class="table_menu"><a href="ct.htm" class="menu">餐厅</a></td>
<td class="table_menu"><a href="shangwu.htm" class="menu">商务</a></td>
<td class="table_menu"><a href="huiyi.htm" class="menu">会议</a></td>
<td class="table_menu"><a href="yule.htm" class="menu">娱乐</a></td>
<td class="table_menu"><a href="dongtai.htm" class="menu">宾馆动态</a></td>
<td class="table_menu"><a href="yuding.htm" class="menu">在线预订</a></td>
<td class="table_menu"><a href="cgi-bin/lyb/default.asp" class="menu">访客留言</a></td>
</tr>
</table></td>
<td width="82" height="95" rowspan="2"><img src="images/pic_04.jpg" width="82" height="95" alt=""></td>
</tr>
<tr>
<td height="20" bgcolor="#F1F1F1"> </td>
</tr>
</table><div style=" width:968px; margin:0 auto ">
<table width="968" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="232" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="images/pic_07.jpg" width="232" height="43" alt=""></td>
</tr>
<tr>
<td><img src="images/pic_kfjs.jpg" width="216" height="40" alt=""></td>
</tr>
<tr>
<td height="31" align="center" background="images/pic_11.jpg"><table width="70%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="table_bottom"><a href="fangjia.htm" class="tongdao">房价表 </a></td>
</tr>
<tr>
<td class="table_bottom"><a href="gexing.htm" class="tongdao">个性化服务</a></td>
</tr>
<tr>
<td class="table_bottom"><a href="ct.htm" class="tongdao">中餐厅 </a></td>
</tr>
<tr>
<td class="table_bottom"><a href="xict.htm" class="tongdao">西餐厅</a></td>
</tr>
</table></td>
</tr>
<tr>
<td><img src="images/pic_19.jpg" width="216" height="9" alt=""></td>
</tr>
<tr>
<td><img src="images/pic_20.jpg" width="216" height="27" alt=""></td>
</tr>
<tr>
<td><img src="images/pic_30.jpg" width="216" height="61" alt=""></td>
</tr>
<tr>
<td><img src="images/pic_45.jpg" width="216" height="70" alt=""></td>
</tr>
<tr>
<td><img src="images/pic_52.jpg" width="216" height="70" alt=""></td>
</tr>
<tr>
<td><a href="images/map.jpg" target="_blank"><img src="images/pic_53.jpg" alt="" width="216" height="86" border="0"></a></td>
</tr>
<tr>
<td><img src="images/pic_54.jpg" width="216" height="14" alt=""></td>
</tr>
</table></td>
<td valign="top"><table width="736" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="images/b.jpg" width="736" height="17" alt=""></td>
</tr>
</table> </td>
</tr>
</table>
<TABLE width="730" border=0 cellPadding=0 cellSpacing=0>
<TR>
<TD width="730"> </TD>
</TR>
<TR>
<TD height="60" valign="bottom" background="images/lin3.jpg"><table width="150" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="20" height="43" valign="bottom"> </td>
<td width="130" align="center" class="bai">在 线 预 订</td>
</tr>
</table></TD>
</TR>
<TR>
<TD align="center" background="images/lin1.jpg"><table width="90%" border="0" align="center" cellpadding="4" cellspacing="0">
<tr align="left">
<td colspan="2"><img src="/images/order-4.gif" width="334" height="1"></td>
</tr>
<tr align="left">
<td height="35" colspan="2"><font color="#993333"><b>・您的预订信息:</b></font></td>
</tr>
<form name=order action=submit.asp method=post>
<tr align="left">
<td width="25%" height="30"><font color="red">*</font>到达时间:</td>
<td width="75%"><select name="ar_daten" >
<option value="">
--请选择年份--
</option>
<script language="javascript" type="text/javascript">
var date=new Date();
var year=date.getFullYear();
for(var i=year;i<=year+4;i++){
document.write("<option value="+i+">"+i+"</option>");
}
</script>
</select>
年
<select name="ar_datem">
<option value="" selected>--请选择月份--</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
月
<select name="ar_dated">
<option value="" selected>--请选择日--</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
日 </td>
</tr>
<tr align="left">
<td width="25%" height="30"><font color="red">*</font>退房时间:</td>
<td width="75%"><select name="Le_daten" >
<option value="">
--请选择年份--
</option>
<script language="javascript" type="text/javascript">
var date=new Date();
var year=date.getFullYear();
for(var i=year;i<=year+4;i++){
document.write("<option value="+i+">"+i+"</option>");
}
</script>
</select>
年
<select name="Le_datem">
<option value="" selected>--请选择月份--</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
月
<select name="Le_dated">
<option value="" selected>--请选择日--</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
日 </td>
</tr>
<tr align="left">
<td width="25%" height="30"><font color="red">*</font>房间类型:</td>
<td width="75%"><select name="room_type">
<option selected value="">--请选择房间类型--</option>
<option value="东山轩">--东山轩--</option>
<option value="东山轩--豪华房">豪华房</option>
<option value="东山轩--贵宾房">贵宾房</option>
<option value="东山轩--多功能套房">多功能套房</option>
<option value="一号楼">--一号楼--</option>
<option value="一号楼--单人房">单人房</option>
<option value="一号楼--标准双人房">标准双人房</option>
<option value="一号楼--豪华单人房">豪华单人房</option>
<option value="一号楼--豪华双人房">豪华双人房</option>
<option value="一号楼--豪华行政套房">豪华行政套房</option>
<option value="一号楼--豪华商务套房">豪华商务套房</option>
<option value="一号楼--特色套房">特色套房</option>
<option value="二号楼">--二号楼--</option>
<option value="二号楼--标准双人房">标准双人房</option>
<option value="二号楼--豪华双人房">豪华双人房</option>
<option value="二号楼--豪华单人房">豪华单人房</option>
<option value="二号楼--三人房">三人房</option>
<option value="二号楼--套房">套房</option>
<option value="三号楼">--三号楼--</option>
<option value="三号楼--标准双人房">标准双人房</option>
<option value="三号楼--豪华双人房">豪华双人房</option>
<option value="三号楼--套房">套房</option>
<option value="三号楼--加床">加床</option>
</select>
</td>
</tr>
<tr align="left">
<td width="25%" height="30"><font color="red">*</font>订房数量:</td>
<td width="75%"><input type="text" name="room_num">
间 </td>
</tr>
<tr align="left">
<td width="25%" height="30"><font color="red">*</font>确认方式:</td>
<td width="75%"><select name="Confirm_type">
<option selected value="">--请选择确认方式--</option>
<option value="电子邮件">电子邮件</option>
<option value="电话">电话</option>
</select>
</td>
</tr>
<tr align="left">
<td width="25%">其它要求:</td>
<td width="75%"><textarea name="message" cols="30" rows="4"></textarea>
</td>
</tr>
<tr align="left">
<td colspan="2"><img src="/images/order-4.gif" width="334" height="1"></td>
</tr>
<tr align="left">
<td height="32" colspan="2"><font color="#993333"><b>・您的个人信息:</b></font><font color="#FF6633">(请您正确填写个人信息以方便与您联系,您的个人信息本宾馆将替您严格保密,请您放心填写)</font></td>
</tr>
<tr align="left">
<td width="25%" height="32"><font color="red">*</font>您的姓名:</td>
<td width="75%"><input type="text" name="name">
</td>
</tr>
<tr align="left">
<td width="25%" height="32"><font color="red">*</font>您的性别:</td>
<td width="75%"><select name="sex">
<option selected value="">--请选择性别--</option>
<option value="先生">先生</option>
<option value="女士">女士</option>
</select>
</td>
</tr>
<tr align="left">
<td width="25%" height="32">您的国籍:</td>
<td width="75%"><input type="text" name="nation">
</td>
</tr>
<tr align="left">
<td width="25%" height="32"><font color="red">*</font>您的电话:</td>
<td width="75%"><input type="text" name="phone">
</td>
</tr>
<tr align="left">
<td width="25%" height="32"><font color="red">*</font>您的Email:</td>
<td width="75%"><input type="text" name="Email">
</td>
</tr>
<tr align="left">
<td width="25%" height="32"><font color="red">*</font>证件号码(身份证):</td>
<td width="75%"><input type="text" name="Idcard" SIZE =30 value="">
</td>
</tr>
<tr align="left">
<td colspan="2"><img src="/images/order-4.gif" width="334" height="1"></td>
</tr>
<tr>
<td colspan="2"><a href="javascript:sb();"><img src="images/yd.gif" width="51" height="21" border="0"></a>
<img src="images/qx.gif" width="51" height="21"> </td>
</tr>
</form>
</table></TD>
</TR>
<TR>
<TD background="images/lin1.jpg"><img src="images/lin2.jpg" width="730" height="14" alt=""></TD>
</TR>
</TABLE></td>
</tr>
</table>
<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0" style="margin-top:11px">
<tr>
<td width="11"><div align="right"><img src="images/pic_56.jpg" width="11" height="86" alt=""></div></td>
<td width="274" align="center" background="images/pic_58.jpg"><img src="images/logo1.gif" width="157" height="49" alt=""></td>
<td width="30" background="images/pic_58.jpg"><img src="images/pic_62.jpg" width="2" height="52" alt=""></td>
<td background="images/pic_58.jpg"><p class="topmenu">粤ICT备05013176号 版权所有:东山宾馆 <br>
地址:广州市越秀区三育路44号东山宾馆 邮编:510080<br>
电话:020-87773722 传真:020-87758288 Email:[email protected]</p>
</td>
<td width="12"><img src="images/pic_60.jpg" width="12" height="86" alt=""></td>
</tr>
</table></div></div>
</form>
</body>
</html>
servlet
package com.cw.servlet;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cw.common.Email;
public class SendMailServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public SendMailServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = new PrintWriter(new OutputStreamWriter(response
.getOutputStream(), "gbk"));
response.setCharacterEncoding("gbk");
StringBuffer str = new StringBuffer("");
// 到达时间
String ar_daten = request.getParameter("ar_daten");
// 到达月份
String ar_datem = request.getParameter("ar_datem");
// 到达日
String ar_dated = request.getParameter("ar_dated");
str.append("<br/>到达时间:" + ar_daten + "-" + ar_datem + "-" + ar_dated);
// 退房时间
String Le_daten = request.getParameter("Le_daten");
// 退房月份
String Le_datem = request.getParameter("Le_datem");
// 退房日
String Le_dated = request.getParameter("Le_dated");
str.append("<br/>退房时间:" + Le_daten + "-" + Le_datem + "-" + Le_dated);
// 房间类型
String room_type = request.getParameter("room_type");
if (room_type != null)
room_type = new String(room_type.getBytes("ISO_8859_1"), "GBK");
else
room_type = "";
str.append("<br/>房间类型:" + room_type);
// 订房数量
String room_num = request.getParameter("room_num");
str.append("<br/>订房数量:" + room_num + "间");
// 确认方式
String Confirm_type = request.getParameter("Confirm_type");
if (Confirm_type != null)
Confirm_type = new String(Confirm_type.getBytes("ISO_8859_1"),
"GBK");
else
Confirm_type = "";
str.append("<br/>确认方式:" + Confirm_type);
// 其它要求
String message = request.getParameter("message");
if (message != null)
message = new String(message.getBytes("ISO_8859_1"), "GBK");
else
message = "";
str.append("<br/>其它要求:" + message);
// 姓名
String name = request.getParameter("name");
if (name != null)
name = new String(name.getBytes("ISO_8859_1"), "GBK");
else
name = "";
str.append("<br/>姓名:" + name);
// 性别
String sex = request.getParameter("sex");
if (sex != null)
sex = new String(sex.getBytes("ISO_8859_1"), "GBK");
else
sex = "";
str.append("<br/>性别:" + sex);
// 国籍
String nation = request.getParameter("nation");
if (nation != null)
nation = new String(nation.getBytes("ISO_8859_1"), "GBK");
else
nation = "";
str.append("<br/>国籍:" + nation);
// 电话
String phone = request.getParameter("phone");
str.append("<br/>电话:" + phone);
// Email
String Email_ = request.getParameter("Email");
str.append("<br/>Email:" + Email_);
//证件号码
String Idcard = request.getParameter("Idcard");
if (Idcard != null)
Idcard = new String(Idcard.getBytes("ISO_8859_1"), "GBK");
else
Idcard = "";
str.append("<br/>证件号码:" + Idcard);
if(Email.send("[email protected]", "客户在线预订", str.toString())){
out.write("<script>window.alert('在线预订成功');window.location='http://www.gzdongshanhotel.com';</script>");
}
else{
out.write("<script>window.alert('在线预订失败');window.location='http://www.gzdongshanhotel.com/yuding.htm';</script>");
}
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}