JSP源代码:
<%@ page import= "java.util.* " %>
<%@ page import= "javax.mail.* " %>
<%@ page import= "javax.activation.* " %>
<%@ page import= "javax.mail.internet.* " %>
<%@ page contentType= "text/html;charset=GB2312 " %>
<html>
<head>
<title> CH17 - JavaMail2.jsp </title>
</head>
<body>
<h2> 利用JavaMail来传送电子邮件 - HTML格式 </h2>
<%
request.setCharacterEncoding( "GB2312 ");
String host = "smtp.163.com ";
String rec = "jonathantest@163.com ";//request.getParameter( "To ");
String sen = "jonathantest@163.com ";//request.getParameter( "From ");
String sub = "Hello ";//request.getParameter( "Subject ");
String mess = "success ";//request.getParameter( "Message ");
String username = "jonathantest ";
String password = "aabbcc1122 ";
boolean sessionDebug = false;
Properties prop = System.getProperties();
prop.put( "mail.host ",host);
prop.put( "mail.transport.protocol ", "smtp ");
prop.put( "mail.smtp.auth ", "true ");
javax.mail.Session mailsess = Session.getDefaultInstance(prop);
mailsess.setDebug(sessionDebug);
Message msg = new MimeMessage(mailsess);
msg.setFrom(new InternetAddress(sen));
InternetAddress[] address= {new InternetAddress(rec)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(sub);
msg.setText(mess);
Transport transport = mailsess.getTransport( "smtp ");
transport.connect(host, username, password);
transport.sendMessage(msg,msg.getAllRecipients());
transport.close();
%>
</body>
</html>
出错提示:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Exception in JSP: /JavaMail.jsp:45
42: msg.setText(mess);
43:
44: Transport transport = mailsess.getTransport( "smtp ");
45: transport.connect(host, username, password);
46: transport.sendMessage(msg,msg.getAllRecipients());
47: transport.close();
48: //transport.send(msg);
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:506)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)