当前位置: 代码迷 >> Java Web开发 >> 提问:关于JAVAMAIL发送时提示不能连接SMTP服务器的有关问题
  详细解决方案

提问:关于JAVAMAIL发送时提示不能连接SMTP服务器的有关问题

热度:17   发布时间:2016-04-17 15:50:37.0
提问:关于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,   port:   25
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:843)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:776)
org.apache.jsp.JavaMail_jsp._jspService(JavaMail_jsp.java:159)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  相关解决方案