首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

javamail1.4 编写发送邮件失败! !

2013-09-11 
javamail1.4 编写发送邮件失败! 在线等!!!准备工作:tomcat5.5+dk1.5,工作正常,javamail1.4,jaf-1_1-fr.zip

javamail1.4 编写发送邮件失败! 在线等!!!
准备工作:   tomcat   5.5+dk1.5,工作正常,javamail1.4   ,   jaf-1_1-fr.zip
writemail.jsp   在ROOT/mail文件夹下

<form   method= "post "   action= "sendmail.jsp "   name= "form1 ">

收件人地址:   <input   type= "text "   name= "to ">

主             题:     <input   type= "text "   name= "title ">

内             容: <textarea   name= "content "   cols= "70 "   rows= "10 "> </textarea>

  <input   type= "submit "   name= "Submit "   value= "提交 ">

</form>


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

<%@   page   contentType= "text/html;   charset=gb2312 "   language= "java "   import= "java.sql.*,java.util.*,javax.mail.*,javax.mail.internet.*,javax.activation.* "   errorPage= " "   %>
<%
try{
//接收邮件信息
String   to_mail=request.getParameter( "to ");
String   to_title=request.getParameter( "title ");
to_title=new   String(to_title.getBytes( "ISO-8859-1 "));

String   to_content=request.getParameter( "content ");
to_content=new   String(to_content.getBytes( "ISO-8859-1 "));
//接收邮件信息结束

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

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

//--------设置邮件--------
InternetAddress   from=new   InternetAddress( "tytymnty@126.com ");
message.setFrom(from);//设置发件人
InternetAddress   to=new   InternetAddress(to_mail);
message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO
message.setSubject(to_title);//设置主题
message.setText(to_content);//设置信件内容
message.setSentDate(new   Date());//设置发信时间

//--------发送邮件--------
message.saveChanges();//存储邮件信息
Transport   transport=s.getTransport( "smtp ");
//--以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
transport.connect( "smtp.126.com ", "tytymnty ", "ty11111111 ");
//发送邮件,其中第二个参数是所有已设好的收件人地址
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
<div   align= "center ">
<p> 向 <%=to_mail%> 发送邮件成功! <br>
邮件主题: <%=to_title%> <br>
邮件内容: <%=to_content%> </p>
</div>
<%
}catch(MessagingException   e){
out.println( "发送失败! ");
}
%>

-------------------------------------
localhost.log中的错误信息如下:
严重:   Servlet.service()   for   servlet   jsp   threw   exception
org.apache.jasper.JasperException:   Unable   to   compile   class   for   JSP:  

An   error   occurred   at   line:   10   in   the   jsp   file:   /mail/sendmail.jsp
The   type   Date   is   ambiguous


7:   </head>
8:  
9:   <body>
10:   <%
11:   try{
12:   //接收邮件信息
13:   String   to_mail=request.getParameter( "to ");


Stacktrace:
at   org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
at   org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
at   org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:435)
at   org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)
at   org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)
at   org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)
at   org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
at   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:299)
at   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
at   org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
at   javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
at   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at   org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:834)
at   org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640)
at   org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1286)
at   java.lang.Thread.run(Unknown   Source)


------------------------------------

以上代码按照书上原封未动录入,但还是出错了。请大家帮忙看看是哪里出问题了!   小弟在此谢过!


[解决办法]
message.setSentDate(new Date());//设置发信时间
改成:
message.setSentDate(new java.util.Date());//设置发信时间
试试
[解决办法]
看样子是Jsp编的有问题

[解决办法]
你现用java测试,然后自jsp测试
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html

热点排行