错误在通过JSP发送电子邮件(SMTP EXIM)

问题描述:

Properties props = new Properties(); 
props.put("mail.smtp.host", "mail.xxx.net"); 
props.put("mail.smtp.port","25"); 
props.put("mail.smtp.ssl.enable", "false"); 
props.put("mail.debug", "true"); 
props.put("mail.smtp.debug", "true"); 
props.put ("mail.smtp.starttls.enable", "true"); 
props.put("mail.transport.protocol", "smtp"); 
props.put("mail.smtp.socketFactory.port", "587"); 
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
props.put("mail.smtp.socketFactory.fallback", "false"); 
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
Authenticator auth = new javax.mail.Authenticator() { 
    protected PasswordAuthentication getPasswordAuthentication(){ 
     return new PasswordAuthentication("[email protected]", "xxx"); 
    } 
}; 
Session s = Session.getInstance(props,auth); 
MimeMessage message = new MimeMessage(s); 
InternetAddress from = new InternetAddress("[email protected]"); 
message.setFrom(from); 
InternetAddress to = new InternetAddress("[email protected]"); 
message.addRecipient(Message.RecipientType.TO, to); 
message.setSubject("Test from JavaMail."); 
//message.setText("Hello from JavaMail!"); 
message.setContent("Hello from JavaMail!", "text/plain"); 
message.saveChanges(); 
Transport transport = s.getTransport("smtp"); 
transport.connect("mail.xxx.net","[email protected]","xxx"); 
transport.sendMessage(message,message.getAllRecipients()); 
transport.close(); 

但我得到了一个错误:错误在通过JSP发送电子邮件(SMTP EXIM)

The server encountered an internal error() that prevented it from fulfilling this request.

堆栈跟踪:

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: mail.xxx.net, port: 25; 
    nested exception is: 
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? 
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 

根源:

javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: mail.xxx.net, port: 25; 
    nested exception is: 
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? 
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911) 
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840) 
    org.apache.jsp.index_jsp._jspService(index_jsp.java:87) 
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 

当我排除代码SSL和TSL

& DEBUG结果

DEBUG: JavaMail version 1.4.5 
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers 
DEBUG: Tables of loaded providers 
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]} 
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]} 
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth false 
DEBUG SMTP: trying to connect to host "mail.xxx.net", port 25, isSSL false 


Mar 23, 2013 8:52:13 AM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [jsp] in context with path [/KKS] threw exception [javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: mail.xxx.net, port: 25; 
nested exception is: 
java.net.ConnectException: Connection timed out: connect] with root cause 
java.net.ConnectException: Connection timed out: connect 
+0

为什么要在属性'props.put(“mail.smtp.socketFactory.class”,“javax.net.ssl.SSLSocketFactory”)中添加SSLSocketFactory?'当您连接到SMTP端口25时?端口25通常是普通的套接字,不支持SSL。 – 2013-03-22 17:25:17

+0

你的意思是说我可以禁用 props.put(“mail.smtp.socketFactory.port”,“587”); props.put(“mail.smtp.socketFactory.class”,“javax.net.ssl.SSLSocketFactory”); props.put(“mail.smtp.socketFactory.fallback”,“false”); – Danish 2013-03-22 17:27:11

+0

如果我禁用我得到以下错误:javax.net.ssl.SSLException:无法识别的SSL消息,明文连接?]与根本原因 javax.net.ssl.SSLException:无法识别的SSL消息,明文连接? – Danish 2013-03-22 17:29:24

正如其他人所说,摆脱套接字工厂的东西。

你会发现很多其他的帮助,在JavaMail FAQ

是您的SMTP服务器设置为允许匿名连接?删除SSL后,您将auth设置为false,因此您的客户端不会将登录名/密码传递到SMTP服务器。这很好,只要你的SMTP服务器允许匿名连接。

另外,您的网络/ SMTP服务器是否支持IP6?如果您的客户端支持IP6,则更高版本的Java默认为IP6,但许多SMTP服务器都配置为IP4。请参阅此文章Sending email using JSP了解如何让您的JVM强​​制实施IP4。这需要在JVM实例化时设置。