jsp实现发邮件

要用jsp实现发邮件,其实就是利用java里面提供的javax.mail包,现在我写的这个是基于1.4版本的包,java1.5以上不支持,不知道为什么,而且在要在tomcat下面运行还必须自己放入两个包,mail.jar和activation.jar,后面提供两个包的下载,不多说了,上代码

<!--mailform.html-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>mailform.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form name="form1" method="post" action="sendmail.jsp">
<p align="center">填写邮件内容</p>
<div align="center">
<table width="75%" border="1">
<tr>
<td><div align="center">收件人</div></td>
<td><div align="center">
<input name="to" type="text" id="to" size="30" maxlength="30">
</div></td>
</tr>
<tr>
<td><div align="center">发件人</div></td>
<td><div align="center">
<input name="from" type="text" id="from" size="30" maxlength="30">
</div></td>
</tr>
<tr>
<td><div align="center">抄送</div></td>
<td><div align="center">
<input name="cc" type="text" id="cc" size="30" maxlength="100">
</div></td>
</tr>
<tr>
<td><div align="center">暗送</div></td>
<td><div align="center">
<input name="bcc" type="text" id="bcc" size="30" maxlength="100">
</div></td>
</tr>
<tr>
<td><div align="center">主题</div></td>
<td><div align="center">
<input name="subject" type="text" id="subject" size="30" maxlength="30">
</div></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<textarea name="body" cols="40" rows="10" id="body"></textarea>
</div></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="发送">
    
<input name="Reset" type="submit" id="Reset" value="清空">
</div></td>
</tr>
</table>
</div>
<p align="center"> </p>
</form>
<p align="center"> </p>
</body>
</html>


<!--sendmail.jsp-->
<%@page contentType="text/html;charset=gb2312"%>
<%@page errorPage="errpage.jsp"%>
<%@page import="java.util.*"%>
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.internet.*"%>
<%@page import="javax.activation.*"%>
<html>
<head>
<title>sendmail.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
subject = new java.lang.String(subject.getBytes("iso-8859-1"));
//以上方法等同于以下两句,原理相同
//byte[] subjectTemp=request.getParameter("subject").getBytes("ISO8859_1");
//String subject=new String(subjectTemp);
byte[] messageTextTemp = request.getParameter("body").getBytes(
"ISO8859_1");
String messageText = new String(messageTextTemp);
boolean sessionDebug = false;
//验证所填写的邮件是否正确
if ((to.trim().indexOf("@") == -1) || (to.trim().length() < 5)) {
%>
<jsp:forward page="err.jsp" />
<%
}
if ((from.trim().indexOf("@") == -1) || (from.trim().length() < 5)) {
%>
<jsp:forward page="err.jsp" />
<%
}
String host = "smtp.sina.com.cn";//这里放的是邮箱的主机
String username = "邮箱的用户名";
String password = "邮箱的秘密";

// Get system properties
// Properties props = System.getProperties(); 很多例子中是这样的,其实下面这句更好,可以用在applet中
Properties props = new Properties();

// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true"); //这样才能通过验证

// Get session
Session mailsession = Session.getDefaultInstance(props);

// watch the mail commands go by to the mail server
mailsession.setDebug(sessionDebug);

// Define message
MimeMessage message = new MimeMessage(mailsession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.setSubject(subject);
message.setText(messageText);
// Send message
message.saveChanges();
Transport transport = mailsession.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
%>
<jsp:forward page="sendok.jsp" />
</body>
</html>


<!--sendok.jsp-->
<%@ page contentType="text/html;charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>sendok.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<center>
<h4>发送成功! </h4>
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td> 收件人</td><td><%=request.getParameter("to")%></td>
</tr>
<tr>
<td>主题:</td><td><%byte[]
subjectTemp=request.getParameter("subject").getBytes("ISO8859_1");
String subject =new String(subjectTemp);
out.println(subject);
%>
</tr>
<tr>
<td colspan = "2" align=center><a href ="mailform.html" style="color:rgb(ff,0,0)">
继续发送</a>
</td>
</tr>
</table>
</center>
</body>
</html>

把这三个jsp用web服务器直接发布就可以了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值