日期:2014-05-18  浏览次数:20912 次

提问:关于JAVAMAIL发送时提示不能连接SMTP服务器的问题
源代码如下:
<html>
<head>
    <title> CH17   -   JavaMail2.jsp </title>
</head>
<body>
<h2> 利用JavaMail来传送电子邮件   -   HTML格式 </h2>

<%@   page   import= "java.util.* "   %>
<%@   page   import= "javax.mail.* "   %>
<%@   page   import= "javax.activation.* "   %>
<%@   page   import= "javax.mail.internet.* "   %>

<%@   page   contentType= "text/html;charset=GB2312 "   %>


<%
request.setCharacterEncoding( "GB2312 ");

String   host       =   "smtp.gmail.com ";
String   rec                   =   request.getParameter( "To ");
String   sen                     =   request.getParameter( "From ");
String   sub             =   request.getParameter( "Subject ");
String   mess         =   request.getParameter( "Message ");
String   username       =   "test ";      
String   password           =   "123456 ";      

    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,   null);
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();
//transport.send(msg);
%>
出错提示:
exception  

org.apache.jasper.JasperException:   Could   not   connect   to   SMTP   host:   smtp.gmail.com,   port:   25
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root   cause  

javax.servlet.ServletException:   Could   not   connect   to   SMTP   host:   smtp.gmail.com,   po