高分求助,为何使用javamail发送邮件时出错?
代码是这样的:
<%@ page contentType= "text/html;charset=gb2312 " %>
<%@ page import= "javax.mail.* " %>
<%@ page import= "javax.mail.internet.* " %>
<%@ page import= "java.util.* " %>
<%@ page import= "javax.activation.* " %>
<html>
<head>
<title> 发送邮件 </title>
</head>
<body>
<%
try{
Properties pro=new Properties();
Session sendmailsession;
Transport transport;
sendmailsession=Session.getInstance(pro,null);
pro.put( "mail.smtp.host ", "smtp.126.com ");
Message m=new MimeMessage(sendmailsession);
m.setFrom(new InternetAddress(request.getParameter( "from ")));
m.setRecipient(Message.RecipientType.TO,new InternetAddress(request.getParameter( "to ")));
m.setSubject(request.getParameter( "subject "));
m.setSentDate(new Date());
m.setText(request.getParameter( "content "));
transport=sendmailsession.getTransport( "smtp ");
transport.send(m);
%>
<p> 邮件已经发送成功 </p>
<%
}catch(Exception ex){
out.println(ex.toString());
}
%>
</body>
</html>
不知道为何会错误
错误信息是:
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 12 in the jsp file: /sendmail.jsp
Generated servlet error:
Session cannot be resolved to a type
An error occurred at line: 12 in the jsp file: /sendmail.jsp
Generated servlet error:
Transport cannot be resolved to a type
An error occurred at line: 12 in the jsp file: /sendmail.jsp
Generated servlet error:
Session cannot be resolved
An error occurred at line: 12 in the jsp file: /sendmail.jsp
Generated servlet error:
Message cannot be resolved to a type
An error occurred at line: 12 in the jsp file: /sendmail.jsp
Generated servlet error:
MimeMessage cannot be resolved to a type
An error occurred at line: 12 in the jsp file: /sendmail.jsp
Generated servlet error:
InternetAddress cannot be resolved to a type
An error occurred at line: 12 in the jsp file: /sendmail.jsp
Generated servlet error:
Message.RecipientType.TO cannot be resolved to a type
An error occurred at line: 12 in the jsp file: /sendmail.jsp
Generated servlet error:
InternetAddress cannot be resolved to a type
[解决办法]
Session Transport Message MimeMessage InternetAddress 这些都不是类型,也就是没有成功导入MAIL的包(CLASS文件)
[解决办法]
应该是没有用户验证
transport.send(m);前面加上
transport.connect( "smtp.126.com ",”用户名”,”密码”)
如果再不行就把
pro.put( "mail.smtp.host ", "smtp.126.com ");
改为
pro.put( "mail.smtp.auth ", "true ");
[解决办法]
smtp邮件服务器需要身份验证.
将mail.smtp.auth 赋值为true.
[解决办法]
那是发送失败的标志,有可能是用户名密码 错误
有可能你发的信超出了服务器限制
反正程序是没问题了,看一下参数是否正确.
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html