jsp邮件

debug148 2004-10-22 12:35:28
求一发邮件jsp页面,发邮件者地址固定即可,收邮件者不需要固定。能实现发邮件功能即可。不需功能有多强大。急用。
...全文
155 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tyong 2004-10-23
学习中....
帮你顶一下
回复
debug148 2004-10-23
这些都有啊,也都设置了
回复
zhz586 2004-10-22
<%@ page contentType="text/html; charset=GB2312" import="register.*,java.io.*,java.util.*,
javax.mail.*,javax.mail.internet.*,javax.activation.*"%>
<html>
<head>
<title>找回密码第三步:</title>
</head>
<body bgcolor="#ffffff">
<%
registersqlbean rbean = registersqlbean.Instance();
String answer = request.getParameter("answer");
String name = (String)session.getAttribute("user_name");

if(rbean.isUserVaild(name,answer,"pswkey"))
{
out.println("成功找回,密码将发送到您的E-mail!");

rbean.DBquery("user_name",name);
Vector findpass_info;
findpass_info = rbean.getOut();
String password = ((registerinfobean)findpass_info.get(0)).getPassword();
String email = ((registerinfobean)findpass_info.get(0)).getEmail();

String to = email;
String subject = "您的密码";
String content = password;

try{
Properties props = new Properties();
props.put("mail.smtp.host", "localhost"); //存储发送邮件服务器的信息
props.put("mail.smtp.auth", "true"); //同时通过验证
Session s = Session.getInstance(props);//根据属性新建一个邮件会话
s.setDebug(true);
MimeMessage message = new MimeMessage(s);//由邮件会话新建一个消息对象
InternetAddress fromAddress = new InternetAddress("webmaster@dairycross.com");
message.setFrom(fromAddress);//设置发件人
InternetAddress toAddress = new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO,toAddress);

message.setSubject(subject);//设置主题
message.setText(content);//设置信件内容
message.setSentDate(new Date());//设置发信时间
message.saveChanges();//存储邮件信息
Transport transport=s.getTransport("smtp");
transport.connect("localhost", "webmaster@dairycross.com", "web");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("邮件发送成功!");
}
catch(Exception ex){
System.out.println(ex.getMessage());
}

out.println("<p><a href=\"index.jsp\">3秒钟后自动跳转首页</a></p>");
out.println("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;URL=index.jsp\">");
}
else
{
out.println("找回密码失败!");
out.println("<p><a href=\"head.jsp\">重登陆</a></p>");
//out.println("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;URL=head.jsp\">");
}
%>
</body>
</html>
回复
opencsdn 2004-10-22
对,下载两个东东,
activation.jar
javamail.jar,
回复
28569050 2004-10-22
要配制邮件服务器,然后才用那些代码的!
还要下个javamail.jar
回复
twenth 2004-10-22
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>撰写邮件</title>
</head>

<body>
<form name="form1" method="post" action="sendmail.jsp">
<table width="75" border="0" align="center" cellspacing="1" bgcolor="#006600" class="black">
<tr bgcolor="#FFFFFF">
<td width="24%">收信人地址:</td>
<td width="76%">
<input name="to" type="text" id="to"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>主题:</td>
<td>
<input name="title" type="text" id="title"></td>
</tr>
<tr>
<td height="107" colspan="2" bgcolor="#FFFFFF">
<textarea name="content" cols="50" rows="5" id="content"></textarea></td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFFFFF">
<input type="submit" name="Submit" value="发送">
<input type="reset" name="Submit2" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>




------------sendmail.jsp

<%@ page contentType="text/html;charset=gb2312" %>
<%request.setCharacterEncoding("gb2312");%><!--中文处理代码-->

<!--引入要用到的类库-->
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送成功</title>
</head>

<body>
<%
try{

//从html表单中获取邮件信息
String tto=request.getParameter("to");
String ttitle=request.getParameter("title");
String tcontent=request.getParameter("content");

Properties props=new Properties();//也可用Properties props = System.getProperties();
props.put("mail.smtp.host","smtp.163.com");//存储发送邮件服务器的信息
props.put("mail.smtp.auth","true");//同时通过验证
Session s=Session.getInstance(props);//根据属性新建一个邮件会话
s.setDebug(true);

MimeMessage message=new MimeMessage(s);//由邮件会话新建一个消息对象

//设置邮件
InternetAddress from=new InternetAddress("你的邮箱@163.com");
message.setFrom(from);//设置发件人
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO
message.setSubject(ttitle);//设置主题
message.setText(tcontent);//设置信件内容
message.setSentDate(new Date());//设置发信时间

//发送邮件
message.saveChanges();//存储邮件信息
Transport transport=s.getTransport("smtp");
transport.connect("smtp.163.com","你的邮箱(不用@和后面的)","你的邮箱密码");//以smtp方式登录邮箱
transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有
//已设好的收件人地址
transport.close();

%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p>
<p><a href="recmail.jsp">去看看我的信箱</a><br>
<br>
<a href="sendmail.htm">再发一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>



另外一定要有把mail.jar(sun网站下)
放到
C:\Tomcat 5.0\common\lib下
回复
debug148 2004-10-22
registerinfobean好象无法解析啊。我用的tomcat。错误如:org.apache.jasper.JasperException: Unable to compile class for JSP

Generated servlet error:
E:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\luntan\huanxiangmailgei_jsp.java:6: package register does not exist
import register.*;
^


An error occurred at line: 8 in the jsp file: /luntan/huanxiangmailgei.jsp
Generated servlet error:
E:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\luntan\huanxiangmailgei_jsp.java:55: cannot resolve symbol
symbol : class registersqlbean
location: class org.apache.jsp.luntan.huanxiangmailgei_jsp
registersqlbean rbean = registersqlbean.Instance();
^


An error occurred at line: 8 in the jsp file: /luntan/huanxiangmailgei.jsp
Generated servlet error:
E:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\luntan\huanxiangmailgei_jsp.java:55: cannot resolve symbol
symbol : variable registersqlbean
location: class org.apache.jsp.luntan.huanxiangmailgei_jsp
registersqlbean rbean = registersqlbean.Instance();
^


An error occurred at line: 8 in the jsp file: /luntan/huanxiangmailgei.jsp
Generated servlet error:
E:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\luntan\huanxiangmailgei_jsp.java:66: cannot resolve symbol
symbol : class registerinfobean
location: class org.apache.jsp.luntan.huanxiangmailgei_jsp
String password = ((registerinfobean)findpass_info.get(0)).getPassword();
^


An error occurred at line: 8 in the jsp file: /luntan/huanxiangmailgei.jsp
Generated servlet error:
E:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\luntan\huanxiangmailgei_jsp.java:67: cannot resolve symbol
symbol : class registerinfobean
location: class org.apache.jsp.luntan.huanxiangmailgei_jsp
String email = ((registerinfobean)findpass_info.get(0)).getEmail();
^
5 errors



org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:332)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:412)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:472)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告